[project @ 2003-09-21 22:20:57 by wolfgang]
[ghc-base.git] / cbits / forkOS.c
1 /* 
2  * (c) The GHC Team 2003
3  *
4  * $Id: forkOS.c,v 1.1 2003/09/21 22:20:57 wolfgang Exp $
5  *
6  * Helper function for Control.Concurrent.forkOS
7  */
8
9 #include "HsBase.h"
10 #include "RtsAPI.h"
11
12 #if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
13 #include <pthread.h>
14
15 static void *
16 forkOS_createThreadWrapper ( void * entry )
17 {
18     rts_lock();
19     rts_evalStableIO((HsStablePtr) entry, NULL);
20     rts_unlock();
21     return NULL;
22 }
23
24 int
25 forkOS_createThread ( HsStablePtr entry )
26 {
27     pthread_t tid;
28     int result = pthread_create(&tid, NULL,
29                                 forkOS_createThreadWrapper, (void*)entry);
30     if(!result)
31         pthread_detach(tid);
32     return result;
33 }
34
35 #elif defined(HAVE_WINDOWS_H)
36 #include <windows.h>
37
38 static unsigned __stdcall
39 forkOS_createThreadWrapper ( void * entry )
40 {
41     rts_lock();
42     rts_evalStableIO((HsStablePtr) entry, NULL);
43     rts_unlock();
44     return 0;
45 }
46
47 int
48 forkOS_createThread ( HsStablePtr entry )
49 {
50     return (_beginthreadex ( NULL,  /* default security attributes */
51                            0,
52                            forkOS_createThreadWrapper,
53                            (void*)entry,
54                            0,
55                            (unsigned*)pId) == 0);
56 }
57
58 #else
59 #endif