1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * --------------------------------------------------------------------------*/
10 #ifndef __OSTHREADS_H__
11 #define __OSTHREADS_H__
13 #if defined(RTS_SUPPORTS_THREADS) /* to the end */
15 # if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
17 typedef pthread_cond_t Condition;
18 typedef pthread_mutex_t Mutex;
19 typedef pthread_t OSThreadId;
21 #define INIT_MUTEX_VAR PTHREAD_MUTEX_INITIALIZER
22 #define INIT_COND_VAR PTHREAD_COND_INITIALIZER
25 #define ACQUIRE_LOCK(mutex) \
26 debugBelch("ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
27 pthread_mutex_lock(mutex)
28 #define RELEASE_LOCK(mutex) \
29 debugBelch("RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); \
30 pthread_mutex_unlock(mutex)
32 #define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
33 #define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
36 # elif defined(HAVE_WINDOWS_H)
39 typedef HANDLE Condition;
41 typedef DWORD OSThreadId;
43 #define INIT_MUTEX_VAR 0
44 #define INIT_COND_VAR 0
47 ACQUIRE_LOCK(Mutex *mutex)
49 if (WaitForSingleObject(*mutex,INFINITE) == WAIT_FAILED) {
50 barf("WaitForSingleObject: %d", GetLastError());
55 RELEASE_LOCK(Mutex *mutex)
57 if (ReleaseMutex(*mutex) == 0) {
58 barf("ReleaseMutex: %d", GetLastError());
63 # error "Threads not supported"
66 extern void initCondition ( Condition* pCond );
67 extern void closeCondition ( Condition* pCond );
68 extern rtsBool broadcastCondition ( Condition* pCond );
69 extern rtsBool signalCondition ( Condition* pCond );
70 extern rtsBool waitCondition ( Condition* pCond,
73 extern void initMutex ( Mutex* pMut );
75 extern OSThreadId osThreadId ( void );
76 extern void shutdownThread ( void );
77 extern void yieldThread ( void );
78 extern int createOSThread ( OSThreadId* tid,
79 void (*startProc)(void) );
82 #define ACQUIRE_LOCK(l)
83 #define RELEASE_LOCK(l)
85 #endif /* defined(RTS_SUPPORTS_THREADS) */
87 #endif /* __OSTHREADS_H__ */