[project @ 2003-09-23 15:38:35 by simonmar]
[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 #define ACQUIRE_LOCK(mutex)   WaitForSingleObject(mutex,INFINITE)
42 #define RELEASE_LOCK(mutex)   ReleaseMutex(mutex)
43 # else
44 #  error "Threads not supported"
45 # endif
46
47 extern void initCondition         ( Condition* pCond );
48 extern void closeCondition        ( Condition* pCond );
49 extern rtsBool broadcastCondition ( Condition* pCond );
50 extern rtsBool signalCondition    ( Condition* pCond );
51 extern rtsBool waitCondition      ( Condition* pCond, 
52                                     Mutex* pMut );
53
54 extern void initMutex             ( Mutex* pMut );
55
56 extern OSThreadId osThreadId      ( void );
57 extern void shutdownThread        ( void );
58 extern void yieldThread           ( void );
59 extern int  createOSThread        ( OSThreadId* tid,
60                                     void (*startProc)(void) );
61 #else
62
63 #define ACQUIRE_LOCK(l)
64 #define RELEASE_LOCK(l)
65
66 #endif /* defined(RTS_SUPPORTS_THREADS) */
67
68 #endif /* __OSTHREADS_H__ */