Gather timing stats for a Task when it completes.
[ghc.git] / rts / posix / OSThreads.c
1 /* ---------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2001-2005
4 *
5 * Accessing OS threads functionality in a (mostly) OS-independent
6 * manner.
7 *
8 * --------------------------------------------------------------------------*/
9
10 #if defined(DEBUG) && defined(__linux__)
11 /* We want GNU extensions in DEBUG mode for mutex error checking */
12 #define _GNU_SOURCE
13 #endif
14
15 #include "Rts.h"
16 #if defined(THREADED_RTS)
17 #include "OSThreads.h"
18 #include "RtsUtils.h"
19 #include "Task.h"
20
21 #if HAVE_STRING_H
22 #include <string.h>
23 #endif
24
25 #if !defined(HAVE_PTHREAD_H)
26 #error pthreads.h is required for the threaded RTS on Posix platforms
27 #endif
28
29 /*
30 * This (allegedly) OS threads independent layer was initially
31 * abstracted away from code that used Pthreads, so the functions
32 * provided here are mostly just wrappers to the Pthreads API.
33 *
34 */
35
36 void
37 initCondition( Condition* pCond )
38 {
39 pthread_cond_init(pCond, NULL);
40 return;
41 }
42
43 void
44 closeCondition( Condition* pCond )
45 {
46 pthread_cond_destroy(pCond);
47 return;
48 }
49
50 rtsBool
51 broadcastCondition ( Condition* pCond )
52 {
53 return (pthread_cond_broadcast(pCond) == 0);
54 }
55
56 rtsBool
57 signalCondition ( Condition* pCond )
58 {
59 return (pthread_cond_signal(pCond) == 0);
60 }
61
62 rtsBool
63 waitCondition ( Condition* pCond, Mutex* pMut )
64 {
65 return (pthread_cond_wait(pCond,pMut) == 0);
66 }
67
68 void
69 yieldThread()
70 {
71 sched_yield();
72 return;
73 }
74
75 void
76 shutdownThread()
77 {
78 pthread_exit(NULL);
79 }
80
81 int
82 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
83 {
84 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
85 if(!result)
86 pthread_detach(*pId);
87 return result;
88 }
89
90 OSThreadId
91 osThreadId()
92 {
93 return pthread_self();
94 }
95
96 void
97 initMutex(Mutex* pMut)
98 {
99 #if defined(DEBUG) && defined(linux_HOST_OS)
100 pthread_mutexattr_t attr;
101 pthread_mutexattr_init(&attr);
102 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK_NP);
103 pthread_mutex_init(pMut,&attr);
104 #else
105 pthread_mutex_init(pMut,NULL);
106 #endif
107 return;
108 }
109
110 void
111 newThreadLocalKey (ThreadLocalKey *key)
112 {
113 int r;
114 if ((r = pthread_key_create(key, NULL)) != 0) {
115 barf("newThreadLocalKey: %s", strerror(r));
116 }
117 }
118
119 void *
120 getThreadLocalVar (ThreadLocalKey *key)
121 {
122 return pthread_getspecific(*key);
123 // Note: a return value of NULL can indicate that either the key
124 // is not valid, or the key is valid and the data value has not
125 // yet been set. We need to use the latter case, so we cannot
126 // detect errors here.
127 }
128
129 void
130 setThreadLocalVar (ThreadLocalKey *key, void *value)
131 {
132 int r;
133 if ((r = pthread_setspecific(*key,value)) != 0) {
134 barf("setThreadLocalVar: %s", strerror(r));
135 }
136 }
137
138 static void *
139 forkOS_createThreadWrapper ( void * entry )
140 {
141 Capability *cap;
142 cap = rts_lock();
143 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
144 taskTimeStamp(myTask());
145 rts_unlock(cap);
146 return NULL;
147 }
148
149 int
150 forkOS_createThread ( HsStablePtr entry )
151 {
152 pthread_t tid;
153 int result = pthread_create(&tid, NULL,
154 forkOS_createThreadWrapper, (void*)entry);
155 if(!result)
156 pthread_detach(tid);
157 return result;
158 }
159
160 #else /* !defined(THREADED_RTS) */
161
162 int
163 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
164 {
165 return -1;
166 }
167
168 #endif /* !defined(THREADED_RTS) */