fd57f56606bb0211e01db2c9847929494875735b
[ghc-hetmet.git] / includes / OSThreads.h
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 #ifndef __OSTHREADS_H__
11 #define __OSTHREADS_H__
12
13 #if defined(THREADED_RTS) /* to the end */
14
15 # if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
16
17 #if CMINUSMINUS
18
19 #define ACQUIRE_LOCK(mutex) foreign "C" pthread_mutex_lock(mutex)
20 #define RELEASE_LOCK(mutex) foreign "C" pthread_mutex_unlock(mutex)
21 #define ASSERT_LOCK_HELD(mutex) /* nothing */
22
23 #else
24
25 #include <pthread.h>
26 #include <errno.h>
27
28 typedef pthread_cond_t  Condition;
29 typedef pthread_mutex_t Mutex;
30 typedef pthread_t       OSThreadId;
31 typedef pthread_key_t   ThreadLocalKey;
32
33 #define OSThreadProcAttr /* nothing */
34
35 #define INIT_COND_VAR       PTHREAD_COND_INITIALIZER
36
37 #ifdef LOCK_DEBUG
38 #define LOCK_DEBUG_BELCH(what, mutex) \
39   debugBelch("%s(0x%p) %s %d\n", what, mutex, __FILE__, __LINE__)
40 #else
41 #define LOCK_DEBUG_BELCH(what, mutex) /* nothing */
42 #endif
43
44 /* Always check the result of lock and unlock. */
45 #define ACQUIRE_LOCK(mutex) \
46   LOCK_DEBUG_BELCH("ACQUIRE_LOCK", mutex); \
47   if (pthread_mutex_lock(mutex) == EDEADLK) { \
48     barf("multiple ACQUIRE_LOCK: %s %d", __FILE__,__LINE__); \
49   }
50
51 #define RELEASE_LOCK(mutex) \
52   LOCK_DEBUG_BELCH("RELEASE_LOCK", mutex); \
53   if (pthread_mutex_unlock(mutex) != 0) { \
54     barf("RELEASE_LOCK: I do not own this lock: %s %d", __FILE__,__LINE__); \
55   }
56
57 #define ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
58
59 #endif // CMINUSMINUS
60
61 # elif defined(HAVE_WINDOWS_H)
62
63 #if CMINUSMINUS
64
65 /* We jump through a hoop here to get a CCall EnterCriticalSection
66    and LeaveCriticalSection, as that's what C-- wants. */
67
68 #define ACQUIRE_LOCK(mutex) foreign "stdcall" EnterCriticalSection(mutex)
69 #define RELEASE_LOCK(mutex) foreign "stdcall" LeaveCriticalSection(mutex)
70 #define ASSERT_LOCK_HELD(mutex) /* nothing */
71
72 #else
73
74 #include <windows.h>
75
76 typedef HANDLE Condition;
77 typedef DWORD OSThreadId;
78 // don't be tempted to use HANDLE as the OSThreadId: there can be 
79 // many HANDLES to a given thread, so comparison would not work.
80 typedef DWORD ThreadLocalKey;
81
82 #define OSThreadProcAttr __stdcall
83
84 #define INIT_COND_VAR  0
85
86 // We have a choice for implementing Mutexes on Windows.  Standard
87 // Mutexes are kernel objects that require kernel calls to
88 // acquire/release, whereas CriticalSections are spin-locks that block
89 // in the kernel after spinning for a configurable number of times.
90 // CriticalSections are *much* faster, so we use those.  The Mutex
91 // implementation is left here for posterity.
92 #define USE_CRITICAL_SECTIONS 1
93
94 #if USE_CRITICAL_SECTIONS
95
96 typedef CRITICAL_SECTION Mutex;
97
98 #ifdef LOCK_DEBUG
99
100 #define ACQUIRE_LOCK(mutex) \
101   debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
102   EnterCriticalSection(mutex)
103 #define RELEASE_LOCK(mutex) \
104   debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
105   LeaveCriticalSection(mutex)
106 #define ASSERT_LOCK_HELD(mutex) /* nothing */
107
108 #else
109
110 #define ACQUIRE_LOCK(mutex)  EnterCriticalSection(mutex)
111 #define RELEASE_LOCK(mutex)  LeaveCriticalSection(mutex)
112
113 // I don't know how to do this.  TryEnterCriticalSection() doesn't do
114 // the right thing.
115 #define ASSERT_LOCK_HELD(mutex) /* nothing */
116
117 #endif
118
119 #else
120
121 typedef HANDLE Mutex;
122
123 // casting to (Mutex *) here required due to use in .cmm files where
124 // the argument has (void *) type.
125 #define ACQUIRE_LOCK(mutex)                                     \
126     if (WaitForSingleObject(*((Mutex *)mutex),INFINITE) == WAIT_FAILED) { \
127         barf("WaitForSingleObject: %d", GetLastError());        \
128     }
129
130 #define RELEASE_LOCK(mutex)                             \
131     if (ReleaseMutex(*((Mutex *)mutex)) == 0) {         \
132         barf("ReleaseMutex: %d", GetLastError());       \
133     }
134
135 #define ASSERT_LOCK_HELD(mutex) /* nothing */
136 #endif
137
138 #endif // CMINUSMINUS
139
140 # else
141 #  error "Threads not supported"
142 # endif
143
144
145 #ifndef CMINUSMINUS
146 //
147 // General thread operations
148 //
149 extern OSThreadId osThreadId      ( void );
150 extern void shutdownThread        ( void );
151 extern void yieldThread           ( void );
152
153 typedef void OSThreadProcAttr OSThreadProc(void *);
154
155 extern int  createOSThread        ( OSThreadId* tid, 
156                                     OSThreadProc *startProc, void *param);
157 extern rtsBool osThreadIsAlive    ( OSThreadId id );
158
159 //
160 // Condition Variables
161 //
162 extern void initCondition         ( Condition* pCond );
163 extern void closeCondition        ( Condition* pCond );
164 extern rtsBool broadcastCondition ( Condition* pCond );
165 extern rtsBool signalCondition    ( Condition* pCond );
166 extern rtsBool waitCondition      ( Condition* pCond, 
167                                     Mutex* pMut );
168
169 //
170 // Mutexes
171 //
172 extern void initMutex             ( Mutex* pMut );
173 extern void closeMutex            ( Mutex* pMut );
174
175 //
176 // Thread-local storage
177 //
178 void  newThreadLocalKey (ThreadLocalKey *key);
179 void *getThreadLocalVar (ThreadLocalKey *key);
180 void  setThreadLocalVar (ThreadLocalKey *key, void *value);
181 void  freeThreadLocalKey (ThreadLocalKey *key);
182
183 #endif // !CMINUSMINUS
184
185 #else
186
187 #define ACQUIRE_LOCK(l)
188 #define RELEASE_LOCK(l)
189 #define ASSERT_LOCK_HELD(l)
190
191 #endif /* defined(THREADED_RTS) */
192
193 #endif /* __OSTHREADS_H__ */