[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / rts / 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 #if defined(RTS_SUPPORTS_THREADS) /* to the end */
13
14 # if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
15 #  include <pthread.h>
16 typedef pthread_cond_t  Condition;
17 typedef pthread_mutex_t Mutex;
18 typedef pthread_t       OSThreadId;
19
20 #define INIT_MUTEX_VAR      PTHREAD_MUTEX_INITIALIZER
21 #define INIT_COND_VAR       PTHREAD_COND_INITIALIZER
22
23 #ifdef LOCK_DEBUG
24 #define ACQUIRE_LOCK(mutex) fprintf(stderr, "ACQUIRE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); fflush(stderr); pthread_mutex_lock(mutex)
25 #define RELEASE_LOCK(mutex) fprintf(stderr, "RELEASE_LOCK(0x%p) %s %d\n", mutex,__FILE__,__LINE__); fflush(stderr); pthread_mutex_unlock(mutex)
26 #else
27 #define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
28 #define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
29 #endif
30
31 # elif defined(HAVE_WINDOWS_H)
32 #include <windows.h>
33
34 typedef HANDLE Condition;
35 typedef HANDLE Mutex;
36 typedef DWORD OSThreadId;
37
38 #define INIT_MUTEX_VAR 0
39 #define INIT_COND_VAR  0
40
41 static inline void
42 ACQUIRE_LOCK(Mutex *mutex)
43 {
44     if (WaitForSingleObject(*mutex,INFINITE) == WAIT_FAILED) {
45         barf("WaitForSingleObject: %d", GetLastError());
46     }
47 }
48
49 static inline void
50 RELEASE_LOCK(Mutex *mutex)
51 {
52     if (ReleaseMutex(*mutex) == 0) {
53         barf("ReleaseMutex: %d", GetLastError());
54     }
55 }
56
57 # else
58 #  error "Threads not supported"
59 # endif
60
61 extern void initCondition         ( Condition* pCond );
62 extern void closeCondition        ( Condition* pCond );
63 extern rtsBool broadcastCondition ( Condition* pCond );
64 extern rtsBool signalCondition    ( Condition* pCond );
65 extern rtsBool waitCondition      ( Condition* pCond, 
66                                     Mutex* pMut );
67
68 extern void initMutex             ( Mutex* pMut );
69
70 extern OSThreadId osThreadId      ( void );
71 extern void shutdownThread        ( void );
72 extern void yieldThread           ( void );
73 extern int  createOSThread        ( OSThreadId* tid,
74                                     void (*startProc)(void) );
75 #else
76
77 #define ACQUIRE_LOCK(l)
78 #define RELEASE_LOCK(l)
79
80 #endif /* defined(RTS_SUPPORTS_THREADS) */
81
82 #endif /* __OSTHREADS_H__ */