Add closeMutex and use it on clean up
[ghc-hetmet.git] / rts / posix / OSThreads.c
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2001-2005
4  *
5  * Accessing OS threads functionality in a (mostly) OS-independent
6  * manner. 
7  *
8  * --------------------------------------------------------------------------*/
9
10 #if defined(DEBUG) && defined(__linux__)
11 /* We want GNU extensions in DEBUG mode for mutex error checking */
12 #define _GNU_SOURCE
13 #endif
14
15 #include "Rts.h"
16 #if defined(THREADED_RTS)
17 #include "OSThreads.h"
18 #include "RtsUtils.h"
19 #include "Task.h"
20
21 #if HAVE_STRING_H
22 #include <string.h>
23 #endif
24
25 #if !defined(HAVE_PTHREAD_H)
26 #error pthreads.h is required for the threaded RTS on Posix platforms
27 #endif
28
29 /*
30  * This (allegedly) OS threads independent layer was initially
31  * abstracted away from code that used Pthreads, so the functions
32  * provided here are mostly just wrappers to the Pthreads API.
33  *
34  */
35
36 void
37 initCondition( Condition* pCond )
38 {
39   pthread_cond_init(pCond, NULL);
40   return;
41 }
42
43 void
44 closeCondition( Condition* pCond )
45 {
46   pthread_cond_destroy(pCond);
47   return;
48 }
49
50 rtsBool
51 broadcastCondition ( Condition* pCond )
52 {
53   return (pthread_cond_broadcast(pCond) == 0);
54 }
55
56 rtsBool
57 signalCondition ( Condition* pCond )
58 {
59   return (pthread_cond_signal(pCond) == 0);
60 }
61
62 rtsBool
63 waitCondition ( Condition* pCond, Mutex* pMut )
64 {
65   return (pthread_cond_wait(pCond,pMut) == 0);
66 }
67
68 void
69 yieldThread()
70 {
71   sched_yield();
72   return;
73 }
74
75 void
76 shutdownThread()
77 {
78   pthread_exit(NULL);
79 }
80
81 int
82 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
83 {
84   int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
85   if(!result)
86     pthread_detach(*pId);
87   return result;
88 }
89
90 OSThreadId
91 osThreadId()
92 {
93   return pthread_self();
94 }
95
96 void
97 initMutex(Mutex* pMut)
98 {
99 #if defined(DEBUG) && defined(linux_HOST_OS)
100     pthread_mutexattr_t attr;
101     pthread_mutexattr_init(&attr);
102     pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK_NP);
103     pthread_mutex_init(pMut,&attr);
104 #else
105     pthread_mutex_init(pMut,NULL);
106 #endif
107     return;
108 }
109 void
110 closeMutex(Mutex* pMut)
111 {
112     pthread_mutex_destroy(pMut);
113 }
114
115 void
116 newThreadLocalKey (ThreadLocalKey *key)
117 {
118     int r;
119     if ((r = pthread_key_create(key, NULL)) != 0) {
120         barf("newThreadLocalKey: %s", strerror(r));
121     }
122 }
123
124 void *
125 getThreadLocalVar (ThreadLocalKey *key)
126 {
127     return pthread_getspecific(*key);
128     // Note: a return value of NULL can indicate that either the key
129     // is not valid, or the key is valid and the data value has not
130     // yet been set.  We need to use the latter case, so we cannot
131     // detect errors here.
132 }
133
134 void
135 setThreadLocalVar (ThreadLocalKey *key, void *value)
136 {
137     int r;
138     if ((r = pthread_setspecific(*key,value)) != 0) {
139         barf("setThreadLocalVar: %s", strerror(r));
140     }
141 }
142
143 static void *
144 forkOS_createThreadWrapper ( void * entry )
145 {
146     Capability *cap;
147     cap = rts_lock();
148     cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
149     taskTimeStamp(myTask());
150     rts_unlock(cap);
151     return NULL;
152 }
153
154 int
155 forkOS_createThread ( HsStablePtr entry )
156 {
157     pthread_t tid;
158     int result = pthread_create(&tid, NULL,
159                                 forkOS_createThreadWrapper, (void*)entry);
160     if(!result)
161         pthread_detach(tid);
162     return result;
163 }
164
165 #else /* !defined(THREADED_RTS) */
166
167 int
168 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
169 {
170     return -1;
171 }
172
173 #endif /* !defined(THREADED_RTS) */