get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/2195316/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 2195316,
    "url": "http://patchwork.ozlabs.org/api/patches/2195316/?format=api",
    "web_url": "http://patchwork.ozlabs.org/project/gcc/patch/f5380b1c-3074-4ecc-89c4-288835b72aaf@gmail.com/",
    "project": {
        "id": 17,
        "url": "http://patchwork.ozlabs.org/api/projects/17/?format=api",
        "name": "GNU Compiler Collection",
        "link_name": "gcc",
        "list_id": "gcc-patches.gcc.gnu.org",
        "list_email": "gcc-patches@gcc.gnu.org",
        "web_url": null,
        "scm_url": null,
        "webscm_url": null,
        "list_archive_url": "",
        "list_archive_url_format": "",
        "commit_url_format": ""
    },
    "msgid": "<f5380b1c-3074-4ecc-89c4-288835b72aaf@gmail.com>",
    "list_archive_url": null,
    "date": "2026-02-10T18:20:15",
    "name": "[9/13] Coarray shared memory library",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "c885d29fa84f7f534951bffb86394ca02ccf7b73",
    "submitter": {
        "id": 78264,
        "url": "http://patchwork.ozlabs.org/api/people/78264/?format=api",
        "name": "Jerry D",
        "email": "jvdelisle2@gmail.com"
    },
    "delegate": null,
    "mbox": "http://patchwork.ozlabs.org/project/gcc/patch/f5380b1c-3074-4ecc-89c4-288835b72aaf@gmail.com/mbox/",
    "series": [
        {
            "id": 491723,
            "url": "http://patchwork.ozlabs.org/api/series/491723/?format=api",
            "web_url": "http://patchwork.ozlabs.org/project/gcc/list/?series=491723",
            "date": "2026-02-10T18:17:09",
            "name": null,
            "version": 1,
            "mbox": "http://patchwork.ozlabs.org/series/491723/mbox/"
        }
    ],
    "comments": "http://patchwork.ozlabs.org/api/patches/2195316/comments/",
    "check": "pending",
    "checks": "http://patchwork.ozlabs.org/api/patches/2195316/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>",
        "X-Original-To": [
            "incoming@patchwork.ozlabs.org",
            "gcc-patches@gcc.gnu.org"
        ],
        "Delivered-To": [
            "patchwork-incoming@legolas.ozlabs.org",
            "gcc-patches@gcc.gnu.org"
        ],
        "Authentication-Results": [
            "legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20230601 header.b=UM3N/K/w;\n\tdkim-atps=neutral",
            "legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)",
            "sourceware.org;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20230601 header.b=UM3N/K/w",
            "sourceware.org;\n dmarc=pass (p=none dis=none) header.from=gmail.com",
            "sourceware.org; spf=pass smtp.mailfrom=gmail.com",
            "server2.sourceware.org;\n arc=none smtp.remote-ip=209.85.214.178"
        ],
        "Received": [
            "from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4f9VHp4J4lz1xwG\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 11 Feb 2026 05:21:10 +1100 (AEDT)",
            "from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 895884BA23C8\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 10 Feb 2026 18:21:08 +0000 (GMT)",
            "from mail-pl1-f178.google.com (mail-pl1-f178.google.com\n [209.85.214.178])\n by sourceware.org (Postfix) with ESMTPS id 2F3CD4BA543C\n for <gcc-patches@gcc.gnu.org>; Tue, 10 Feb 2026 18:20:18 +0000 (GMT)",
            "by mail-pl1-f178.google.com with SMTP id\n d9443c01a7336-2a9057b2ec3so5584625ad.2\n for <gcc-patches@gcc.gnu.org>; Tue, 10 Feb 2026 10:20:18 -0800 (PST)",
            "from [10.168.168.23] ([50.37.179.80])\n by smtp.gmail.com with ESMTPSA id\n d9443c01a7336-2a951c50206sm139986425ad.19.2026.02.10.10.20.16\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Tue, 10 Feb 2026 10:20:16 -0800 (PST)"
        ],
        "DKIM-Filter": [
            "OpenDKIM Filter v2.11.0 sourceware.org 895884BA23C8",
            "OpenDKIM Filter v2.11.0 sourceware.org 2F3CD4BA543C"
        ],
        "DMARC-Filter": "OpenDMARC Filter v1.4.2 sourceware.org 2F3CD4BA543C",
        "ARC-Filter": "OpenARC Filter v1.0.0 sourceware.org 2F3CD4BA543C",
        "ARC-Seal": "i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1770747618; cv=none;\n b=TujVO+Yjx7qxrZoU0oEWN2lOVBBbxGluJqtAL9rlm4cdz3zXrsOx+y03kQJmFpHUtr8GHjSRvv8USXFGjq6cF9LiuU3XAplGsGb5GNeLjN8aUEXA74/UVfSDUVpPVZUd3RfnbaCtAAFY2dy6eI4Ro3aLIa6RiojR0SrQmZZgcHc=",
        "ARC-Message-Signature": "i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1770747618; c=relaxed/simple;\n bh=g3E+j9ZolyBeDLKFiD/qFie12MKUqEKnCwwl2s+RGYw=;\n h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject;\n b=d0BGqtcvXiOnz/cR9jyaW1c96AZeYJwT16OQ204t+7RLSzE2EfNP7GmookElNx7jPlX+zeXWv08Bcr11GEXd7JWmnqf0BLMXx2AECILZtLRyfhj+zID+5uDBon6mhC/sGJLBI/goxXf6R36SS5j9SyWK03kkaolh9fIpxCcwDW0=",
        "ARC-Authentication-Results": "i=1; server2.sourceware.org",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=gmail.com; s=20230601; t=1770747617; x=1771352417; darn=gcc.gnu.org;\n h=autocrypt:subject:from:cc:to:content-language:user-agent\n :mime-version:date:message-id:from:to:cc:subject:date:message-id\n :reply-to; bh=g3E+j9ZolyBeDLKFiD/qFie12MKUqEKnCwwl2s+RGYw=;\n b=UM3N/K/wJ6FEqD2CVP1MDpNUJeBYoBKcJaiRR5GD2VoqcNaPsK667a/fd/8WRldWf4\n sSQfX7GWIh0IQbzudxc6CqxnuY6WB2DLKUfKD4AywdYXkL1wM4EMIg4hR8XfzG8IoQb5\n YcL891S4hkMbuuLomhLwj1eXVkEAbJZoH8rBWJIa2aO1Jy5mqhW5KwM7q8LGxi33mCbh\n BSpsdKV3N5naIWKNZVToozytWdvSc4Ea3SBwXH2sWMuWTpvNzEp2D2Ldew0g7ycI80On\n co2AV2SR3m17yVqQG4qermDT1W4VCB/BjVOMjojWwLy9eQXhzv5AEoDQPq5W8apolbsP\n H8sA==",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20230601; t=1770747617; x=1771352417;\n h=autocrypt:subject:from:cc:to:content-language:user-agent\n :mime-version:date:message-id:x-gm-gg:x-gm-message-state:from:to:cc\n :subject:date:message-id:reply-to;\n bh=g3E+j9ZolyBeDLKFiD/qFie12MKUqEKnCwwl2s+RGYw=;\n b=sV8jFdoRKV/jL1CNVMPnU8rehk9z2NbM0Jw12qvFgVGangw7u2wAm+lk0F1NKY564z\n te8/Ye44gX5eKtUAp/J3ASdG2LWElTzj5tn3E3AyPi3zOm17hJ6iVlMPgu41go3XUo61\n Pufsh69iTC89funFEtJbG/R+5HDnNwnPHTs95g9r0/CmtwoeripQ6pHGr8A02OpURjWB\n qmuITeyfZybwPGFe+kKwXAi9Wq3Ks78DVKWiQFsMM2OmD21DWH9s/9kUiNmqScnko05+\n GFC9ABpxO4OlTy7eqSN85EHh+VB1HcBiDxpWfZnj3JzXQAPSxAAco9JtQVXYhPKkSJpV\n KEYA==",
        "X-Forwarded-Encrypted": "i=1;\n AJvYcCXpPs5/7xLON4gKDgr3byr9w829RZ+kpAhQ/Eb/Cb46t1WxK9OBq/mJs8eMhyL0gj1dgOIShpX20tHThg==@gcc.gnu.org",
        "X-Gm-Message-State": "AOJu0YwdD/OPHi2L+0PA3R9aE/FryVplf4QeKAoaG0teXMZRMhbRdC0N\n FCCZZwt1UXfP4dxfH9ZLymVoRcZmeOfg2J9Grf2OTMmvbi4nCreILAwz2X2TsA==",
        "X-Gm-Gg": "AZuq6aLSB548jDtOXcIKYw0rBIXv56SRZjNMgncaxINeQJMWncT9BPk6qWZBagsNsIN\n pyL6Fh/9Xqiw9D1bfTCmDa0P8lg2lPjG9sM4jnv6DSfYaIN4UO/AjDij6OeqTA9XmmyNYY2rqN/\n bSfcMAULXlFT2xOs0KxO4DAU+jiEX6HHbboBqpb7asDOkrGsgMqyKEhvFZ5WFRcFGjY4PKJH3qI\n gAEpZutAw2jG9eeXSeTuUGHwuuTZ2U+Fl50XJAUyWRMzRAX/PJ/G62O1/4L8etMEZiBeudNQThy\n vwN6CMN0SXNXUdmmX5OxTBWzpIc1MvqtLQTV2jiPOVXJJ0HgrWbrUTDiKJHJIioL21tpwN5EJRX\n z/loPdB8pVNi364GyoGgRMcn0nUNdNVYxRYsvDvUHBBdFsEBVfyRJ6RsfdSA24w3FaQkmFqmyyD\n q3UgYlaLcdnch0QEVio2ltwGZFbhA=",
        "X-Received": "by 2002:a17:902:ebca:b0:2a0:a33f:3049 with SMTP id\n d9443c01a7336-2a951607aeemr134445905ad.4.1770747617161;\n Tue, 10 Feb 2026 10:20:17 -0800 (PST)",
        "Content-Type": "multipart/mixed; boundary=\"------------07kQPVyOjSqurUk45z5Ba36r\"",
        "Message-ID": "<f5380b1c-3074-4ecc-89c4-288835b72aaf@gmail.com>",
        "Date": "Tue, 10 Feb 2026 10:20:15 -0800",
        "MIME-Version": "1.0",
        "User-Agent": "Mozilla Thunderbird",
        "Content-Language": "en-US",
        "To": "gfortran <fortran@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>",
        "Cc": "Tobias Burnus <tburnus@baylibre.com>, Richard Biener <rguenther@suse.de>,\n Andre Vehreschild <vehre@gmx.de>, Jakub Jelinek <jakub@redhat.com>,\n Paul Richard Thomas <paul.richard.thomas@gmail.com>,\n FX Coudert <fxcoudert@gmail.com>, Toon Moene <toon@moene.org>",
        "From": "Jerry D <jvdelisle2@gmail.com>",
        "Subject": "[PATCH 9/13] Coarray shared memory library",
        "Autocrypt": "addr=jvdelisle2@gmail.com; keydata=\n xjMEY5TlkxYJKwYBBAHaRw8BAQdAyrkRDhmJhSTTlV/50gJLlvliU6/Lm5C9ViKV8T9y1GnN\n HkplcnJ5IEQgPGp2ZGVsaXNsZTJAZ21haWwuY29tPsKJBBMWCAAxFiEEOFR0TS0390uh8dRV\n uWXAJaWpwWoFAmOU5ZMCGwMECwkIBwUVCAkKCwUWAgMBAAAKCRC5ZcAlpanBalsJAP4wdCiH\n 2Of9oZv1QWgZ/AVdbWFM3Fv47/WZQHOXfoZ9HgD6AkXrKeJ+6usST7PEaDJjptaViT1fLiYY\n V/6XaOKSsgLOOARjlOWTEgorBgEEAZdVAQUBAQdAdA7PczYnl07vnOT9oP/wvvMDd4HP09Zl\n g3LzwXQJWT8DAQgHwngEGBYIACAWIQQ4VHRNLTf3S6Hx1FW5ZcAlpanBagUCY5TlkwIbDAAK\n CRC5ZcAlpanBasF/AQCa5WjlsVpLsEiggZyT18MOJNAdeRd7wkGDUrwedHrvawD/cE1H+/Ms\n L1ZwvQiLfGdx8crigQqWTQyos4kH8Wx82wc=",
        "X-BeenThere": "gcc-patches@gcc.gnu.org",
        "X-Mailman-Version": "2.1.30",
        "Precedence": "list",
        "List-Id": "Gcc-patches mailing list <gcc-patches.gcc.gnu.org>",
        "List-Unsubscribe": "<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>",
        "List-Archive": "<https://gcc.gnu.org/pipermail/gcc-patches/>",
        "List-Post": "<mailto:gcc-patches@gcc.gnu.org>",
        "List-Help": "<mailto:gcc-patches-request@gcc.gnu.org?subject=help>",
        "List-Subscribe": "<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>",
        "Errors-To": "gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"
    },
    "content": "Dear all, as requested,\n\nSee attached patch 9 of 13\n\nBest Regards,\n\nJerry",
    "diff": "commit f9c3f968aab22e1ff4782a721da30bcd839e614f\nAuthor: Andre Vehreschild <vehre@gcc.gnu.org>\nDate:   Fri Sep 5 14:40:24 2025 +0200\n\n    Fortran: Fix creating shared memory on macOS.\n    \n    On MacOS mmap() very often does not respect the provided base address\n    for the shared memory segment.  On the other hand the mutexes have to be\n    on the same (virtual) address for each process to function properly.\n    Therefore try a configurable number of times to get the same address for\n    the shared memory segment on MacOS.  If that fails the user is notified\n    and the program terminates.\n    \n    gcc/fortran/ChangeLog:\n    \n            * invoke.texi: Document new environment variable GFORTRAN_IMAGE_\n            RESTARTS_LIMIT.\n    \n    libgfortran/ChangeLog:\n    \n            * caf/shmem.c (_gfortran_caf_finalize): Ensure all memory is\n            freeed.\n            * caf/shmem/allocator.c (allocator_shared_malloc): Just assert\n            that an index is within its bounds.\n            * caf/shmem/shared_memory.c (shared_memory_init): When shared\n            memory can not be placed at desired address, exit the image with\n            a certain code to let the supervisor restart the image.\n            (shared_memory_cleanup): Only the supervisor must unlink the shm\n            object.\n            * caf/shmem/supervisor.c (GFORTRAN_ENV_IMAGE_RESTARTS_LIMITS):\n            New environment variable.\n            (get_image_restarts_limit): Get the limit on image restarts\n            (accumulates over all) form the environment variable or default\n            to 4000.\n            (ensure_shmem_initialization): Add error handling.\n            (startWorker): Start a single worker/image.\n            (kill_all_images): Kill all images.\n            (supervisor_main_loop): When a worker/image reports a shared\n            memory issue just try to restart it.\n            * caf/shmem/thread_support.c (initialize_shared_mutex): Mark\n            mutex robust on plattforms that support it.\n            (initialize_shared_errorcheck_mutex): Same.\n\ndiff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi\nindex 6e8e13a982b..c544037033a 100644\n--- a/gcc/fortran/invoke.texi\n+++ b/gcc/fortran/invoke.texi\n@@ -2350,6 +2350,15 @@ memory.\n \n Warning: Choosing a large shared memory size may produce large coredumps!\n \n+@env{GFORTRAN_IMAGE_RESTARTS_LIMIT}: On certain platforms, esp. MacOS, the\n+shared memory segment needs to be placed on the same (virtual) address in every\n+image or synchronisation primitives do not work as expected.  Unfortunately are\n+some OSes somewhat arbitrary on when they can do this.  When the OS is not able\n+to fullfill the request, then the image aborts itsself and is restarted by the\n+supervisor untill the OS complies.  This environment variable limits the total\n+number of restarts of all images having an issue with shared memory segment\n+placement.  The default value is 4000.\n+\n The shared memory coarray library internally uses some additional environment\n variables, which will be overwritten without notice or may result in failure to\n start.  These are: @code{GFORTRAN_IMAGE_NUM}, @code{GFORTRAN_SHMEM_PID} and\ndiff --git a/libgfortran/caf/shmem.c b/libgfortran/caf/shmem.c\nindex 266feab3e45..446e5f54483 100644\n--- a/libgfortran/caf/shmem.c\n+++ b/libgfortran/caf/shmem.c\n@@ -152,6 +152,7 @@ _gfortran_caf_finalize (void)\n   free_team_list (caf_teams_formed);\n   caf_teams_formed = NULL;\n \n+  shared_memory_cleanup (&local->sm);\n   free (local);\n \n   thread_support_cleanup ();\ndiff --git a/libgfortran/caf/shmem/allocator.c b/libgfortran/caf/shmem/allocator.c\nindex 2a22abb2a80..bd88f33e200 100644\n--- a/libgfortran/caf/shmem/allocator.c\n+++ b/libgfortran/caf/shmem/allocator.c\n@@ -101,6 +101,8 @@ allocator_shared_malloc (allocator *a, size_t size)\n   sz = next_power_of_two (size);\n   act_size = sz > sizeof (bucket) ? sz : sizeof (bucket);\n   bucket_list_index = __builtin_clzl (act_size);\n+  assert (bucket_list_index\n+\t  < (int) (sizeof (a->s->free_bucket_head) / sizeof (shared_mem_ptr)));\n \n   if (SHMPTR_IS_NULL (a->s->free_bucket_head[bucket_list_index]))\n     return shared_memory_get_mem_with_alignment (a->shm, act_size, MAX_ALIGN);\ndiff --git a/libgfortran/caf/shmem/shared_memory.c b/libgfortran/caf/shmem/shared_memory.c\nindex d0789a4bac6..0659e6ba023 100644\n--- a/libgfortran/caf/shmem/shared_memory.c\n+++ b/libgfortran/caf/shmem/shared_memory.c\n@@ -29,6 +29,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see\n #include \"libgfortran.h\"\n #include \"allocator.h\"\n #include \"shared_memory.h\"\n+#include \"supervisor.h\"\n \n #include <assert.h>\n #include <fcntl.h>\n@@ -194,7 +195,7 @@ shared_memory_init (shared_memory_act *mem, size_t size)\n   else\n     {\n #ifdef HAVE_MMAP\n-      mem->shm_fd = shm_open (shm_name, O_RDWR, 0);\n+      mem->shm_fd = shm_open (shm_name, O_RDWR, 0600);\n       if (mem->shm_fd == -1)\n \t{\n \t  perror (\"opening shared memory segment failed.\");\n@@ -212,7 +213,14 @@ shared_memory_init (shared_memory_act *mem, size_t size)\n #ifdef HAVE_MMAP\n   mem->glbl.base\n     = mmap (base_ptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, mem->shm_fd, 0);\n-  if (mem->glbl.base == MAP_FAILED)\n+  if (base_ptr && mem->glbl.base != base_ptr)\n+    {\n+      /* The supervisor will start us again.  */\n+      close (mem->shm_fd);\n+      free (local);\n+      exit (210);\n+    }\n+  else if (!base_ptr && !mem->glbl.base)\n     {\n       perror (\"mmap failed\");\n       exit (1);\n@@ -249,9 +257,6 @@ shared_memory_init (shared_memory_act *mem, size_t size)\n void\n shared_memory_cleanup (shared_memory_act *mem)\n {\n-  char shm_name[NAME_MAX];\n-\n-  snprintf (shm_name, NAME_MAX, \"/gfor-shm-%s\", shared_memory_get_env ());\n #ifdef HAVE_MMAP\n   int res = munmap (mem->glbl.base, mem->size);\n   if (res)\n@@ -263,11 +268,18 @@ shared_memory_cleanup (shared_memory_act *mem)\n     {\n       perror (\"closing shm file handle failed. Trying to continue...\");\n     }\n-  res = shm_unlink (shm_name);\n-  if (res == -1)\n+  if (this_image.image_num == -1)\n     {\n-      perror (\"shm_unlink failed\");\n-      exit (1);\n+      char shm_name[NAME_MAX];\n+\n+      snprintf (shm_name, NAME_MAX, \"/gfor-shm-%s\", shared_memory_get_env ());\n+      /* Only the supervisor is to delete the shm-file.  */\n+      res = shm_unlink (shm_name);\n+      if (res == -1)\n+\t{\n+\t  perror (\"shm_unlink failed\");\n+\t  exit (1);\n+\t}\n     }\n #elif defined(WIN32)\n   if (!UnmapViewOfFile (mem->glbl.base))\ndiff --git a/libgfortran/caf/shmem/supervisor.c b/libgfortran/caf/shmem/supervisor.c\nindex c39ffc6715c..780ab4a45c0 100644\n--- a/libgfortran/caf/shmem/supervisor.c\n+++ b/libgfortran/caf/shmem/supervisor.c\n@@ -43,6 +43,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see\n #define GFORTRAN_ENV_NUM_IMAGES \"GFORTRAN_NUM_IMAGES\"\n #define GFORTRAN_ENV_SHARED_MEMORY_SIZE \"GFORTRAN_SHARED_MEMORY_SIZE\"\n #define GFORTRAN_ENV_IMAGE_NUM \"GFORTRAN_IMAGE_NUM\"\n+#define GFORTRAN_ENV_IMAGE_RESTARTS_LIMITS \"GFORTRAN_IMAGE_RESTARTS_LIMIT\"\n \n image_local *local = NULL;\n \n@@ -68,6 +69,21 @@ get_image_num_from_envvar (void)\n   return nimages;\n }\n \n+/* Get the number of restarts allowed when the shared memory could not be placed\n+at the same location in each image.  This is mostly important for MacOS, because\n+this OS acts somewhat arbitrary/indeterministic.  */\n+\n+static unsigned\n+get_image_restarts_limit (void)\n+{\n+  char *limit_chars;\n+  unsigned limit = 4000;\n+  limit_chars = getenv (GFORTRAN_ENV_IMAGE_RESTARTS_LIMITS);\n+  if (limit_chars)\n+    limit = atoi (limit_chars);\n+  return limit;\n+}\n+\n /* Get the amount of memory for the shared memory block.  This is picked from\n    an environment variable.  If that is not there, pick a reasonable default.\n    Note that on a 64-bit system which allows overcommit, there is no penalty in\n@@ -157,6 +173,11 @@ ensure_shmem_initialization (void)\n     return;\n \n   local = malloc (sizeof (image_local));\n+  if (!local)\n+    {\n+      caf_runtime_error (\"can not initialize memory for local cache\");\n+      exit (1);\n+    }\n #if defined(_SC_PAGE_SIZE)\n   pagesize = sysconf (_SC_PAGE_SIZE);\n #elif defined(WIN32)\n@@ -234,6 +255,54 @@ ensure_shmem_initialization (void)\n extern char **environ;\n #endif\n \n+static bool\n+startWorker (image *im __attribute__ ((unused)),\n+\t     char ***argv __attribute__ ((unused)))\n+{\n+#ifdef HAVE_FORK\n+  caf_shmem_pid new_pid;\n+  if ((new_pid = fork ()))\n+    {\n+      im->supervisor->images[im->image_num]\n+\t= (image_tracker) {new_pid, IMAGE_OK};\n+      return false;\n+    }\n+  else\n+    {\n+      if (new_pid == -1)\n+\tcaf_runtime_error (\"error spawning child\\n\");\n+      static char **new_env;\n+      static char num_image[32];\n+      size_t n = 2; /* Add one env-var and one for the term NULL.  */\n+\n+      /* Count the number of entries in the current environment.  */\n+      for (char **e = environ; *e; ++e, ++n)\n+\t;\n+      new_env = (char **) malloc (sizeof (char *) * n);\n+      memcpy (new_env, environ, sizeof (char *) * (n - 2));\n+      snprintf (num_image, 32, \"%s=%d\", GFORTRAN_ENV_IMAGE_NUM, im->image_num);\n+      new_env[n - 2] = num_image;\n+      new_env[n - 1] = NULL;\n+      if (execve ((*argv)[0], *argv, new_env) == -1)\n+\t{\n+\t  perror (\"execve failed\");\n+\t}\n+      exit (255);\n+    }\n+#endif\n+  return true;\n+}\n+\n+#ifndef WIN32\n+static void\n+kill_all_images (supervisor *m)\n+{\n+  for (int j = 0; j < local->total_num_images; j++)\n+    if (m->images[j].status == IMAGE_OK)\n+      kill (m->images[j].pid, SIGKILL);\n+}\n+#endif\n+\n /* argc and argv may not be used on certain OSes.  Flag them unused therefore.\n  */\n int\n@@ -254,40 +323,19 @@ supervisor_main_loop (int *argc __attribute__ ((unused)),\n   GetCurrentDirectory (cdLen, currentDir);\n #else\n   int chstatus;\n+  unsigned restarts = 0, restarts_limit;\n+  restarts_limit = get_image_restarts_limit ();\n #endif\n \n   *exit_code = 0;\n   shared_memory_set_env (getpid ());\n-  m = this_image.supervisor;\n+  im.supervisor = m = this_image.supervisor;\n \n   for (im.image_num = 0; im.image_num < local->total_num_images; im.image_num++)\n     {\n #ifdef HAVE_FORK\n-      caf_shmem_pid new_pid;\n-      if ((new_pid = fork ()))\n-\t{\n-\t  if (new_pid == -1)\n-\t    caf_runtime_error (\"error spawning child\\n\");\n-\t  m->images[im.image_num] = (image_tracker) {new_pid, IMAGE_OK};\n-\t}\n-      else\n-\t{\n-\t  static char **new_env;\n-\t  static char num_image[32];\n-\t  size_t n = 2; /* Add one env-var and one for the term NULL.  */\n-\n-\t  /* Count the number of entries in the current environment.  */\n-\t  for (char **e = environ; *e; ++e, ++n)\n-\t    ;\n-\t  new_env = (char **) malloc (sizeof (char *) * n);\n-\t  memcpy (new_env, environ, sizeof (char *) * (n - 2));\n-\t  snprintf (num_image, 32, \"%s=%d\", GFORTRAN_ENV_IMAGE_NUM,\n-\t\t    im.image_num);\n-\t  new_env[n - 2] = num_image;\n-\t  new_env[n - 1] = NULL;\n-\t  execve ((*argv)[0], *argv, new_env);\n-\t  return 1;\n-\t}\n+      if (startWorker (&im, argv))\n+\treturn 1;\n #elif defined(WIN32)\n       LPTCH new_env;\n       size_t n = 0, es;\n@@ -345,6 +393,14 @@ supervisor_main_loop (int *argc __attribute__ ((unused)),\n #ifdef HAVE_FORK\n       caf_shmem_pid finished_pid = wait (&chstatus);\n       int j;\n+\n+      if (finished_pid == -1)\n+\t{\n+\t  /* Skip wait having an issue.  */\n+\t  perror (\"wait failed\");\n+\t  --i;\n+\t  continue;\n+\t}\n       if (WIFEXITED (chstatus) && !WEXITSTATUS (chstatus))\n \t{\n \t  for (j = 0;\n@@ -365,35 +421,56 @@ supervisor_main_loop (int *argc __attribute__ ((unused)),\n \t       j < local->total_num_images && m->images[j].pid != finished_pid;\n \t       j++)\n \t    ;\n-\t  dprintf (2, \"ERROR: Image %d(pid: %d) failed with %d.\\n\", j + 1,\n-\t\t   finished_pid, WTERMSIG (chstatus));\n-\t  if (j == local->total_num_images)\n+\t  if (WEXITSTATUS (chstatus) == 210)\n \t    {\n-\t      if (finished_pid == getpid ())\n+\t      --i;\n+\t      im.image_num = j;\n+\t      ++restarts;\n+\t      if (restarts > restarts_limit)\n \t\t{\n-\t\t  dprintf (2,\n-\t\t\t   \"WARNING: Supervisor process got signal %d. Killing \"\n-\t\t\t   \"childs and exiting.\\n\",\n-\t\t\t   WTERMSIG (chstatus));\n-\t\t  for (j = 0; j < local->total_num_images; j++)\n-\t\t    {\n-\t\t      if (m->images[j].status == IMAGE_OK)\n-\t\t\tkill (m->images[j].pid, SIGKILL);\n-\t\t    }\n+\t\t  kill_all_images (m);\n+\t\t  caf_runtime_error (\n+\t\t    \"After restarting images %d times, no common state on \"\n+\t\t    \"shared memory could be reached. Giving up...\",\n+\t\t    restarts);\n \t\t  exit (1);\n \t\t}\n-\t      dprintf (2,\n-\t\t       \"WARNING: Got signal %d for unknown process %d. \"\n-\t\t       \"Ignoring and trying to continue.\\n\",\n-\t\t       WTERMSIG (chstatus), finished_pid);\n+\t      if (startWorker (&im, argv))\n+\t\treturn 1;\n \t      continue;\n \t    }\n-\t  m->images[j].status = IMAGE_FAILED;\n-\t  atomic_fetch_add (&m->failed_images, 1);\n-\t  if (*exit_code < WTERMSIG (chstatus))\n-\t    *exit_code = WTERMSIG (chstatus);\n-\t  else if (*exit_code == 0)\n-\t    *exit_code = 1;\n+\t  else\n+\t    {\n+\t      dprintf (2,\n+\t\t       \"ERROR: Image %d(pid: %d) failed with signal %d, \"\n+\t\t       \"exitstatus %d.\\n\",\n+\t\t       j + 1, finished_pid, WTERMSIG (chstatus),\n+\t\t       WEXITSTATUS (chstatus));\n+\t      if (j == local->total_num_images)\n+\t\t{\n+\t\t  if (finished_pid == getpid ())\n+\t\t    {\n+\t\t      dprintf (\n+\t\t\t2,\n+\t\t\t\"WARNING: Supervisor process got signal %d. Killing \"\n+\t\t\t\"childs and exiting.\\n\",\n+\t\t\tWTERMSIG (chstatus));\n+\t\t      kill_all_images (m);\n+\t\t      exit (1);\n+\t\t    }\n+\t\t  dprintf (2,\n+\t\t\t   \"WARNING: Got signal %d for unknown process %d. \"\n+\t\t\t   \"Ignoring and trying to continue.\\n\",\n+\t\t\t   WTERMSIG (chstatus), finished_pid);\n+\t\t  continue;\n+\t\t}\n+\t      m->images[j].status = IMAGE_FAILED;\n+\t      atomic_fetch_add (&m->failed_images, 1);\n+\t      if (*exit_code < WTERMSIG (chstatus))\n+\t\t*exit_code = WTERMSIG (chstatus);\n+\t      else if (*exit_code == 0)\n+\t\t*exit_code = 1;\n+\t    }\n \t}\n       /* Trigger waiting sync images aka sync_table.  */\n       for (j = 0; j < local->total_num_images; j++)\ndiff --git a/libgfortran/caf/shmem/thread_support.c b/libgfortran/caf/shmem/thread_support.c\nindex e2c53627c2f..dcd8b00b788 100755\n--- a/libgfortran/caf/shmem/thread_support.c\n+++ b/libgfortran/caf/shmem/thread_support.c\n@@ -50,6 +50,9 @@ initialize_shared_mutex (caf_shmem_mutex *mutex)\n   pthread_mutexattr_t mattr;\n   ERRCHECK (pthread_mutexattr_init (&mattr));\n   ERRCHECK (pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED));\n+#ifdef PTHREAD_MUTEX_ROBUST\n+  ERRCHECK (pthread_mutexattr_setrobust (&mattr, PTHREAD_MUTEX_ROBUST));\n+#endif\n   ERRCHECK (pthread_mutex_init (mutex, &mattr));\n   ERRCHECK (pthread_mutexattr_destroy (&mattr));\n }\n@@ -61,6 +64,9 @@ initialize_shared_errorcheck_mutex (caf_shmem_mutex *mutex)\n   ERRCHECK (pthread_mutexattr_init (&mattr));\n   ERRCHECK (pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_ERRORCHECK));\n   ERRCHECK (pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED));\n+#ifdef PTHREAD_MUTEX_ROBUST\n+  ERRCHECK (pthread_mutexattr_setrobust (&mattr, PTHREAD_MUTEX_ROBUST));\n+#endif\n   ERRCHECK (pthread_mutex_init (mutex, &mattr));\n   ERRCHECK (pthread_mutexattr_destroy (&mattr));\n }\n@@ -248,7 +254,7 @@ bm_set_mask (volatile unsigned long mask[], const int size)\n     mask[i] = ~0UL >> (ULONGBITS - rem);\n }\n \n-__attribute_used__ static bool\n+__attribute__ ((used)) static bool\n bm_is_none (volatile unsigned long mask[], const int size)\n {\n   const int entries = size / ULONGBITS;\n",
    "prefixes": [
        "9/13"
    ]
}