a065f7a408e1d279c059bfe2832f140c1caaed2b
[ghc-hetmet.git] / ghc / includes / OSThreads.h
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2001
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(RTS_SUPPORTS_THREADS) /* to the end */
14
15 # if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
16 #  include <pthread.h>
17 typedef pthread_cond_t  Condition;
18 typedef pthread_mutex_t Mutex;
19 typedef pthread_t       OSThreadId;
20
21 #define INIT_MUTEX_VAR      PTHREAD_MUTEX_INITIALIZER
22 #define INIT_COND_VAR       PTHREAD_COND_INITIALIZER
23
24 #ifdef LOCK_DEBUG
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)
31 #else
32 #define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
33 #define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
34 #endif
35
36 # elif defined(HAVE_WINDOWS_H)
37 #include <windows.h>
38
39 typedef HANDLE Condition;
40 typedef HANDLE Mutex;
41 typedef DWORD OSThreadId;
42
43 #define INIT_MUTEX_VAR 0
44 #define INIT_COND_VAR  0
45
46 INLINE_HEADER void
47 ACQUIRE_LOCK(Mutex *mutex)
48 {
49     if (WaitForSingleObject(*mutex,INFINITE) == WAIT_FAILED) {
50         barf("WaitForSingleObject: %d", GetLastError());
51     }
52 }
53
54 INLINE_HEADER void
55 RELEASE_LOCK(Mutex *mutex)
56 {
57     if (ReleaseMutex(*mutex) == 0) {
58         barf("ReleaseMutex: %d", GetLastError());
59     }
60 }
61
62 # else
63 #  error "Threads not supported"
64 # endif
65
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, 
71                                     Mutex* pMut );
72
73 extern void initMutex             ( Mutex* pMut );
74
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) );
80 #else
81
82 #define ACQUIRE_LOCK(l)
83 #define RELEASE_LOCK(l)
84
85 #endif /* defined(RTS_SUPPORTS_THREADS) */
86
87 #endif /* __OSTHREADS_H__ */