Skip to content

Commit f5fc109

Browse files
committed
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.
1 parent ce2028b commit f5fc109

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

driver/others/blas_server_win32.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
#include <stdlib.h>
4141
#include "common.h"
4242

43+
#if defined(OS_CYGWIN_NT) && !defined(unlikely)
44+
#ifdef __GNUC__
45+
#define unlikely(x) __builtin_expect(!!(x), 0)
46+
#else
47+
#define unlikely(x) (x)
48+
#endif
49+
#endif
50+
4351
/* This is a thread implementation for Win32 lazy implementation */
4452

4553
/* Thread server common infomation */
@@ -53,7 +61,7 @@ typedef struct{
5361

5462
} blas_pool_t;
5563

56-
/* We need this grobal for cheking if initialization is finished. */
64+
/* We need this global for cheking if initialization is finished. */
5765
int blas_server_avail = 0;
5866

5967
/* Local Variables */
@@ -340,6 +348,11 @@ int blas_thread_init(void){
340348

341349
int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
342350

351+
#if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
352+
// Handle lazy re-init of the thread-pool after a POSIX fork
353+
if (unlikely(blas_server_avail == 0)) blas_thread_init();
354+
#endif
355+
343356
blas_queue_t *current;
344357

345358
current = queue;
@@ -405,6 +418,11 @@ int exec_blas_async_wait(BLASLONG num, blas_queue_t *queue){
405418
/* Execute Threads */
406419
int exec_blas(BLASLONG num, blas_queue_t *queue){
407420

421+
#if defined(SMP_SERVER) && defined(OS_CYGWIN_NT)
422+
// Handle lazy re-init of the thread-pool after a POSIX fork
423+
if (unlikely(blas_server_avail == 0)) blas_thread_init();
424+
#endif
425+
408426
#ifndef ALL_THREADED
409427
int (*routine)(blas_arg_t *, void *, void *, double *, double *, BLASLONG);
410428
#endif

driver/others/memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void openblas_fork_handler()
323323
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
324324
// In the mean time build with USE_OPENMP=0 or link against another
325325
// implementation of OpenMP.
326-
#if !(defined(OS_WINDOWS) || defined(OS_ANDROID)) && defined(SMP_SERVER)
326+
#if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
327327
int err;
328328
err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL);
329329
if(err != 0)

0 commit comments

Comments
 (0)