1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2005
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * --------------------------------------------------------------------------*/
10 #ifndef __OSTHREADS_H__
11 #define __OSTHREADS_H__
13 #if defined(THREADED_RTS) /* to the end */
15 # if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
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 */
27 typedef pthread_cond_t Condition;
28 typedef pthread_mutex_t Mutex;
29 typedef pthread_t OSThreadId;
30 typedef pthread_key_t ThreadLocalKey;
32 #define OSThreadProcAttr /* nothing */
34 #define INIT_COND_VAR PTHREAD_COND_INITIALIZER
38 #define ACQUIRE_LOCK(mutex) \
39 debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
40 pthread_mutex_lock(mutex)
41 #define RELEASE_LOCK(mutex) \
42 debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
43 pthread_mutex_unlock(mutex)
44 #define ASSERT_LOCK_HELD(mutex) /* nothing */
46 #elif defined(DEBUG) && defined(linux_HOST_OS)
49 * On Linux, we can use extensions to determine whether we already
50 * hold a lock or not, which is useful for debugging.
52 #define ACQUIRE_LOCK(mutex) \
53 if (pthread_mutex_lock(mutex) == EDEADLK) { \
54 barf("multiple ACQUIRE_LOCK: %s %d", __FILE__,__LINE__); \
56 #define RELEASE_LOCK(mutex) \
57 if (pthread_mutex_unlock(mutex) != 0) { \
58 barf("RELEASE_LOCK: I do not own this lock: %s %d", __FILE__,__LINE__); \
61 #define ASSERT_LOCK_HELD(mutex) ASSERT(pthread_mutex_lock(mutex) == EDEADLK)
63 #define ASSERT_LOCK_NOTHELD(mutex) \
64 if (pthread_mutex_lock(mutex) != EDEADLK) { \
65 pthread_mutex_unlock(mutex); \
73 #define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
74 #define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
75 #define ASSERT_LOCK_HELD(mutex) /* nothing */
81 # elif defined(HAVE_WINDOWS_H)
85 /* We jump through a hoop here to get a CCall EnterCriticalSection
86 and LeaveCriticalSection, as that's what C-- wants. */
88 #define ACQUIRE_LOCK(mutex) foreign "stdcall" EnterCriticalSection(mutex)
89 #define RELEASE_LOCK(mutex) foreign "stdcall" LeaveCriticalSection(mutex)
90 #define ASSERT_LOCK_HELD(mutex) /* nothing */
96 typedef HANDLE Condition;
97 typedef DWORD OSThreadId;
98 // don't be tempted to use HANDLE as the OSThreadId: there can be
99 // many HANDLES to a given thread, so comparison would not work.
100 typedef DWORD ThreadLocalKey;
102 #define OSThreadProcAttr __stdcall
104 #define INIT_COND_VAR 0
106 // We have a choice for implementing Mutexes on Windows. Standard
107 // Mutexes are kernel objects that require kernel calls to
108 // acquire/release, whereas CriticalSections are spin-locks that block
109 // in the kernel after spinning for a configurable number of times.
110 // CriticalSections are *much* faster, so we use those. The Mutex
111 // implementation is left here for posterity.
112 #define USE_CRITICAL_SECTIONS 1
114 #if USE_CRITICAL_SECTIONS
116 typedef CRITICAL_SECTION Mutex;
120 #define ACQUIRE_LOCK(mutex) \
121 debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
122 EnterCriticalSection(mutex)
123 #define RELEASE_LOCK(mutex) \
124 debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
125 LeaveCriticalSection(mutex)
126 #define ASSERT_LOCK_HELD(mutex) /* nothing */
130 #define ACQUIRE_LOCK(mutex) EnterCriticalSection(mutex)
131 #define RELEASE_LOCK(mutex) LeaveCriticalSection(mutex)
133 // I don't know how to do this. TryEnterCriticalSection() doesn't do
135 #define ASSERT_LOCK_HELD(mutex) /* nothing */
141 typedef HANDLE Mutex;
143 // casting to (Mutex *) here required due to use in .cmm files where
144 // the argument has (void *) type.
145 #define ACQUIRE_LOCK(mutex) \
146 if (WaitForSingleObject(*((Mutex *)mutex),INFINITE) == WAIT_FAILED) { \
147 barf("WaitForSingleObject: %d", GetLastError()); \
150 #define RELEASE_LOCK(mutex) \
151 if (ReleaseMutex(*((Mutex *)mutex)) == 0) { \
152 barf("ReleaseMutex: %d", GetLastError()); \
155 #define ASSERT_LOCK_HELD(mutex) /* nothing */
158 #endif // CMINUSMINUS
161 # error "Threads not supported"
167 // General thread operations
169 extern OSThreadId osThreadId ( void );
170 extern void shutdownThread ( void );
171 extern void yieldThread ( void );
173 typedef void OSThreadProcAttr OSThreadProc(void *);
175 extern int createOSThread ( OSThreadId* tid,
176 OSThreadProc *startProc, void *param);
177 extern rtsBool osThreadIsAlive ( OSThreadId id );
180 // Condition Variables
182 extern void initCondition ( Condition* pCond );
183 extern void closeCondition ( Condition* pCond );
184 extern rtsBool broadcastCondition ( Condition* pCond );
185 extern rtsBool signalCondition ( Condition* pCond );
186 extern rtsBool waitCondition ( Condition* pCond,
192 extern void initMutex ( Mutex* pMut );
193 extern void closeMutex ( Mutex* pMut );
196 // Thread-local storage
198 void newThreadLocalKey (ThreadLocalKey *key);
199 void *getThreadLocalVar (ThreadLocalKey *key);
200 void setThreadLocalVar (ThreadLocalKey *key, void *value);
201 void freeThreadLocalKey (ThreadLocalKey *key);
203 #endif // !CMINUSMINUS
207 #define ACQUIRE_LOCK(l)
208 #define RELEASE_LOCK(l)
209 #define ASSERT_LOCK_HELD(l)
211 #endif /* defined(THREADED_RTS) */
213 #endif /* __OSTHREADS_H__ */