From f5fc109fbd73b2feadaca5e9e74d085cc5fe5144 Mon Sep 17 00:00:00 2001 From: "Erik M. Bray" Date: Tue, 6 Feb 2018 11:10:45 +0100 Subject: [PATCH] Perform blas_thread_shutdown with pthread_atfork() on Cygwin Even if we're directly using the win32 threading driver and not pthreads, pthread_atfork still works fine to register a pre-fork handler, and is necessary to restore the threading server to a pre-initialized state. --- driver/others/blas_server_win32.c | 20 +++++++++++++++++++- driver/others/memory.c | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/driver/others/blas_server_win32.c b/driver/others/blas_server_win32.c index cde8ca793..02a25ac39 100644 --- a/driver/others/blas_server_win32.c +++ b/driver/others/blas_server_win32.c @@ -40,6 +40,14 @@ #include #include "common.h" +#if defined(OS_CYGWIN_NT) && !defined(unlikely) +#ifdef __GNUC__ +#define unlikely(x) __builtin_expect(!!(x), 0) +#else +#define unlikely(x) (x) +#endif +#endif + /* This is a thread implementation for Win32 lazy implementation */ /* Thread server common infomation */ @@ -53,7 +61,7 @@ typedef struct{ } blas_pool_t; -/* We need this grobal for cheking if initialization is finished. */ +/* We need this global for cheking if initialization is finished. */ int blas_server_avail = 0; /* Local Variables */ @@ -340,6 +348,11 @@ int blas_thread_init(void){ int exec_blas_async(BLASLONG pos, blas_queue_t *queue){ +#if defined(SMP_SERVER) && defined(OS_CYGWIN_NT) + // Handle lazy re-init of the thread-pool after a POSIX fork + if (unlikely(blas_server_avail == 0)) blas_thread_init(); +#endif + blas_queue_t *current; current = queue; @@ -405,6 +418,11 @@ int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){ /* Execute Threads */ int exec_blas(BLASLONG num, blas_queue_t *queue){ +#if defined(SMP_SERVER) && defined(OS_CYGWIN_NT) + // Handle lazy re-init of the thread-pool after a POSIX fork + if (unlikely(blas_server_avail == 0)) blas_thread_init(); +#endif + #ifndef ALL_THREADED int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG); #endif diff --git a/driver/others/memory.c b/driver/others/memory.c index 1d5b70003..b37ec2ff9 100644 --- a/driver/others/memory.c +++ b/driver/others/memory.c @@ -323,7 +323,7 @@ void openblas_fork_handler() // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035 // In the mean time build with USE_OPENMP=0 or link against another // implementation of OpenMP. -#if !(defined(OS_WINDOWS) || defined(OS_ANDROID)) && defined(SMP_SERVER) +#if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER) int err; err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL); if(err != 0)