[project @ 2003-09-24 10:32:12 by simonmar]
[ghc-base.git] / cbits / forkOS.c
1 /* 
2  * (c) The GHC Team 2003
3  *
4  * $Id: forkOS.c,v 1.2 2003/09/23 16:18:03 sof 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 /* For reasons not yet clear, the entire contents of process.h is protected 
38  * by __STRICT_ANSI__ not being defined.
39  */
40 #undef __STRICT_ANSI__
41 #include <process.h>
42
43 static unsigned __stdcall
44 forkOS_createThreadWrapper ( void * entry )
45 {
46     rts_lock();
47     rts_evalStableIO((HsStablePtr) entry, NULL);
48     rts_unlock();
49     return 0;
50 }
51
52 int
53 forkOS_createThread ( HsStablePtr entry )
54 {
55     unsigned long pId;
56     return (_beginthreadex ( NULL,  /* default security attributes */
57                            0,
58                            forkOS_createThreadWrapper,
59                            (void*)entry,
60                            0,
61                            (unsigned*)&pId) == 0);
62 }
63
64 #else
65 #endif