Description: Add fork handler
 This patches fixes hangs of OpenBLAS when called from a program using OpenMP.
 Note that this will only work if OpenBLAS is compiled with pthreads
 (i.e. no OpenMP).
Origin: upstream, https://github.com/xianyi/OpenBLAS/issues/294#issuecomment-33536895
Bug: https://github.com/xianyi/OpenBLAS/issues/294
Bug-Debian: http://bugs.debian.org/739331
Last-Update: 2014-02-17
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/driver/others/memory.c
+++ b/driver/others/memory.c
@@ -1220,6 +1220,14 @@ static void gotoblas_memory_init(void) {
 }
 #endif
 
+static void openblas_fork_handler()
+{
+  int err;
+  err = pthread_atfork (BLASFUNC(blas_thread_shutdown), blas_thread_init, blas_thread_init);
+  if(err != 0)
+    fprintf(stderr, "OpenBLAS cannot install fork handler. You may meet hang after fork.\n");
+}
+
 /* Initialization for all function; this function should be called before main */
 
 static int gotoblas_initialized = 0;
@@ -1228,6 +1236,8 @@ void CONSTRUCTOR gotoblas_init(void) {
 
   if (gotoblas_initialized) return;
 
+  openblas_fork_handler();
+
 #ifdef PROFILE
    moncontrol (0);
 #endif
