Skip to content

Commit d31f62c

Browse files
authored
Merge pull request #1450 from embray/cygwin/forking
Fix issues with forking on Cygwin
2 parents 95f2cea + 8f5f614 commit d31f62c

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

Makefile.system

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ endif
304304
ifeq ($(OSNAME), CYGWIN_NT)
305305
NEED_PIC = 0
306306
NO_EXPRECISION = 1
307+
OS_CYGWIN_NT = 1
307308
endif
308309

309310
ifneq ($(OSNAME), WINNT)

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7474
#include "common.h"
7575
#include <errno.h>
7676

77-
#ifdef OS_WINDOWS
77+
#if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
7878
#define ALLOC_WINDOWS
7979
#ifndef MEM_LARGE_PAGES
8080
#define MEM_LARGE_PAGES 0x20000000
@@ -88,7 +88,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8888
#include <stdio.h>
8989
#include <fcntl.h>
9090

91-
#ifndef OS_WINDOWS
91+
#if !defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)
9292
#include <sys/mman.h>
9393
#ifndef NO_SYSV_IPC
9494
#include <sys/shm.h>
@@ -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)

utest/Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ OBJS=utest_main.o test_amax.o
1212
#test_rot.o test_swap.o test_axpy.o test_dotu.o test_rotmg.o test_dsdot.o test_fork.o
1313

1414
ifneq ($(NO_LAPACK), 1)
15-
OBJS += test_potrs.o
15+
#OBJS += test_potrs.o
16+
endif
17+
18+
ifndef OS_WINDOWS
19+
OBJS += test_fork.o
20+
else
21+
ifdef OS_CYGWIN_NT
22+
OBJS += test_fork.o
23+
endif
1624
endif
1725

1826
all : run_test

utest/test_fork.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
3232
**********************************************************************************/
3333

34-
#ifndef OS_WINDOWS
35-
#include "common_utest.h"
34+
#include "openblas_utest.h"
3635
#include <sys/wait.h>
3736
#include <cblas.h>
3837

@@ -54,11 +53,11 @@ void check_dgemm(double *a, double *b, double *result, double *expected, int n)
5453
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n,
5554
1.0, a, n, b, n, 0.0, result, n);
5655
for(i = 0; i < n * n; ++i) {
57-
CU_ASSERT_DOUBLE_EQUAL(expected[i], result[i], CHECK_EPS);
56+
ASSERT_DBL_NEAR_TOL(expected[i], result[i], DOUBLE_EPS);
5857
}
5958
}
6059

61-
void test_fork_safety(void)
60+
CTEST(fork, safety)
6261
{
6362
int n = 1000;
6463
int i;
@@ -89,7 +88,7 @@ void test_fork_safety(void)
8988

9089
fork_pid = fork();
9190
if (fork_pid == -1) {
92-
CU_FAIL("Failed to fork process.");
91+
CTEST_ERR("Failed to fork process.");
9392
} else if (fork_pid == 0) {
9493
// Compute a DGEMM product in the child process to check that the
9594
// thread pool as been properly been reinitialized after the fork.
@@ -99,7 +98,7 @@ void test_fork_safety(void)
9998
// recursively
10099
fork_pid_nested = fork();
101100
if (fork_pid_nested == -1) {
102-
CU_FAIL("Failed to fork process.");
101+
CTEST_ERR("Failed to fork process.");
103102
exit(1);
104103
} else if (fork_pid_nested == 0) {
105104
check_dgemm(a, b, d, c, n);
@@ -108,17 +107,16 @@ void test_fork_safety(void)
108107
check_dgemm(a, b, d, c, n);
109108
int child_status = 0;
110109
pid_t wait_pid = wait(&child_status);
111-
CU_ASSERT(wait_pid == fork_pid_nested);
112-
CU_ASSERT(WEXITSTATUS (child_status) == 0);
110+
ASSERT_EQUAL(wait_pid, fork_pid_nested);
111+
ASSERT_EQUAL(0, WEXITSTATUS (child_status));
113112
exit(0);
114113
}
115114
} else {
116115
check_dgemm(a, b, d, c, n);
117116
// Wait for the child to finish and check the exit code.
118117
int child_status = 0;
119118
pid_t wait_pid = wait(&child_status);
120-
CU_ASSERT(wait_pid == fork_pid);
121-
CU_ASSERT(WEXITSTATUS (child_status) == 0);
119+
ASSERT_EQUAL(wait_pid, fork_pid);
120+
ASSERT_EQUAL(0, WEXITSTATUS (child_status));
122121
}
123122
}
124-
#endif

0 commit comments

Comments
 (0)