https://gcc.gnu.org/g:4a9c76b78cd4a3bc8107efe23a3bd7de205eb901
commit r16-7873-g4a9c76b78cd4a3bc8107efe23a3bd7de205eb901 Author: Jerry DeLisle <[email protected]> Date: Mon Mar 2 20:02:58 2026 -0800 Fortran: Fix failures on windows and hpux systems [PR124330] Co-authored-by: John David Anglin <[email protected]> PR fortran/124330 libgfortran/ChangeLog: * caf/shmem/shared_memory.c: Fix filenames for WIN32 includes. (shared_memory_set_env): Use putenv() for HPUX and as a fallback where setenv () is not available. (NAME_MAX): Replace with SHM_NAME_MAX. (SHM_NAME_MAX): Use this to avoid duplicating NAME_MAX used elsewhere. * caf/shmem/supervisor.c (get_image_num_from_envvar): Add a fallback for HPUX. Add additional comment to explain why the number of cores is used in lieu of GFORTRAN_NUM_IMAGES. Diff: --- libgfortran/caf/shmem/shared_memory.c | 39 +++++++++++++++++++++-------------- libgfortran/caf/shmem/supervisor.c | 9 +++++++- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/libgfortran/caf/shmem/shared_memory.c b/libgfortran/caf/shmem/shared_memory.c index 0659e6ba0234..69c3db6aca21 100644 --- a/libgfortran/caf/shmem/shared_memory.c +++ b/libgfortran/caf/shmem/shared_memory.c @@ -38,8 +38,8 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #ifdef HAVE_SYS_MMAN_H #include <sys/mman.h> #elif defined(WIN32) -#include <Windows.h> -#include <Memoryapi.h> +#include <windows.h> +#include <memoryapi.h> #endif #include <unistd.h> @@ -62,16 +62,23 @@ static const char *ENV_BASE = "GFORTRAN_SHMEM_BASE"; void shared_memory_set_env (pid_t pid) { -#define bufsize 20 - char buffer[bufsize]; - - snprintf (buffer, bufsize, "%d", pid); -#ifdef HAVE_SETENV - setenv (ENV_PPID, buffer, 1); +#if defined(HAVE_SETENV) + char val[20]; + snprintf (val, 20, "%d", pid); + setenv (ENV_PPID, val, 1); +#elif defined(WIN32) + char val[20]; + snprintf (val, 20, "%d", pid); + SetEnvironmentVariable (ENV_PPID, val); #else - SetEnvironmentVariable (ENV_PPID, buffer); + char buffer[28]; + int res; + + /* HP-UX / Legacy Fallback using putenv */ + res = snprintf (buffer, 28, "%s=%d", "ENV_PPID", (int)pid); + if (res != -1) + putenv (buffer); #endif -#undef bufsize } char * @@ -115,7 +122,7 @@ shared_memory_prepare (shared_memory_act *) asm volatile ("" ::: "memory"); } -#define NAME_MAX 255 +#define SHM_NAME_MAX 255 /* Initialize the memory with one page, the shared metadata of the shared memory is stored at the beginning. */ @@ -123,7 +130,7 @@ shared_memory_prepare (shared_memory_act *) void shared_memory_init (shared_memory_act *mem, size_t size) { - char shm_name[NAME_MAX]; + char shm_name[SHM_NAME_MAX]; const char *env_val = getenv (ENV_PPID), *base = getenv (ENV_BASE); pid_t ppid = getpid (); void *base_ptr; @@ -133,7 +140,7 @@ shared_memory_init (shared_memory_act *mem, size_t size) int n = sscanf (env_val, "%d", &ppid); assert (n == 1); } - snprintf (shm_name, NAME_MAX, "/gfor-shm-%d", ppid); + snprintf (shm_name, SHM_NAME_MAX, "/gfor-shm-%d", ppid); if (base) { int n = sscanf (base, "%p", &base_ptr); @@ -270,9 +277,9 @@ shared_memory_cleanup (shared_memory_act *mem) } if (this_image.image_num == -1) { - char shm_name[NAME_MAX]; + char shm_name[SHM_NAME_MAX]; - snprintf (shm_name, NAME_MAX, "/gfor-shm-%s", shared_memory_get_env ()); + snprintf (shm_name, SHM_NAME_MAX, "/gfor-shm-%s", shared_memory_get_env ()); /* Only the supervisor is to delete the shm-file. */ res = shm_unlink (shm_name); if (res == -1) @@ -289,4 +296,4 @@ shared_memory_cleanup (shared_memory_act *mem) CloseHandle (mem->shm_fd); #endif } -#undef NAME_MAX +#undef SHM_NAME_MAX diff --git a/libgfortran/caf/shmem/supervisor.c b/libgfortran/caf/shmem/supervisor.c index 50a00155334a..cdb318d5910e 100644 --- a/libgfortran/caf/shmem/supervisor.c +++ b/libgfortran/caf/shmem/supervisor.c @@ -39,7 +39,9 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #if !defined(_SC_PAGE_SIZE) && defined(WIN32) #include <windows.h> #endif - +#if defined(__hpux__) +#include <sys/mpctl.h> +#endif #define GFORTRAN_ENV_NUM_IMAGES "GFORTRAN_NUM_IMAGES" #define GFORTRAN_ENV_SHARED_MEMORY_SIZE "GFORTRAN_SHARED_MEMORY_SIZE" #define GFORTRAN_ENV_IMAGE_NUM "GFORTRAN_IMAGE_NUM" @@ -57,11 +59,16 @@ get_image_num_from_envvar (void) char *num_images_char; int nimages; num_images_char = getenv (GFORTRAN_ENV_NUM_IMAGES); + + /* It is expected that the user has set the GFORTRAN_NUM_IMAGES + environment variable. If not, we fall back to the number of cores. */ if (!num_images_char) #ifdef _SC_NPROCESSORS_ONLN return sysconf (_SC_NPROCESSORS_ONLN); #elif defined(WIN32) num_images_char = getenv ("NUMBER_OF_PROCESSORS"); +#elif defined (__hpux__) + return mpctl (MPC_GETNUMSPUS, 0, 0); #else #error "Unsupported system: No known way to get number of cores!" #endif
