rts/Linker.c: declare 'deRefStablePtr' as an exported 'rts' symbol
[ghc.git] / rts / Task.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 2001-2005
4 *
5 * Tasks
6 *
7 * For details on the high-level design, see
8 * http://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Scheduler
9 *
10 * -------------------------------------------------------------------------*/
11
12 #ifndef TASK_H
13 #define TASK_H
14
15 #include "GetTime.h"
16
17 #include "BeginPrivate.h"
18
19 /*
20 Definition of a Task
21 --------------------
22
23 A task is an OSThread that runs Haskell code. Every OSThread that
24 runs inside the RTS, whether as a worker created by the RTS or via
25 an in-call from C to Haskell, has an associated Task. The first
26 time an OS thread calls into Haskell it is allocated a Task, which
27 remains until the RTS is shut down.
28
29 There is a one-to-one relationship between OSThreads and Tasks.
30 The Task for an OSThread is kept in thread-local storage, and can
31 be retrieved at any time using myTask().
32
33 In the THREADED_RTS build, multiple Tasks may all be running
34 Haskell code simultaneously. A task relinquishes its Capability
35 when it is asked to evaluate an external (C) call.
36
37 Ownership of Task
38 -----------------
39
40 Task ownership is a little tricky. The default situation is that
41 the Task is an OS-thread-local structure that is owned by the OS
42 thread named in task->id. An OS thread not currently executing
43 Haskell code might call newBoundTask() at any time, which assumes
44 that it has access to the Task for the current OS thread.
45
46 The all_next and all_prev fields of a Task are owned by
47 all_tasks_mutex, which must also be taken if we want to create or
48 free a Task.
49
50 For an OS thread in Haskell, if (task->cap->running_task != task),
51 then the Task is owned by the owner of the parent data structure on
52 which it is sleeping; for example, if the task is sleeping on
53 spare_workers field of a Capability, then the owner of the
54 Capability has access to the Task.
55
56 When a task is migrated from sleeping on one Capability to another,
57 its task->cap field must be modified. When the task wakes up, it
58 will read the new value of task->cap to find out which Capability
59 it belongs to. Hence some synchronisation is required on
60 task->cap, and this is why we have task->lock.
61
62 If the Task is not currently owned by task->id, then the thread is
63 either
64
65 (a) waiting on the condition task->cond. The Task is either
66 (1) a bound Task, the TSO will be on a queue somewhere
67 (2) a worker task, on the spare_workers queue of task->cap.
68
69 (b) making a foreign call. The InCall will be on the
70 suspended_ccalls list.
71
72 We re-establish ownership in each case by respectively
73
74 (a) the task is currently blocked in yieldCapability().
75 This call will return when we have ownership of the Task and
76 a Capability. The Capability we get might not be the same
77 as the one we had when we called yieldCapability().
78
79 (b) we must call resumeThread(task), which will safely establish
80 ownership of the Task and a Capability.
81 */
82
83 // The InCall structure represents either a single in-call from C to
84 // Haskell, or a worker thread.
85 typedef struct InCall_ {
86 StgTSO * tso; // the bound TSO (or NULL for a worker)
87
88 StgTSO * suspended_tso; // the TSO is stashed here when we
89 // make a foreign call (NULL otherwise);
90
91 Capability *suspended_cap; // The capability that the
92 // suspended_tso is on, because
93 // we can't read this from the TSO
94 // without owning a Capability in the
95 // first place.
96
97 SchedulerStatus stat; // return status
98 StgClosure ** ret; // return value
99
100 struct Task_ *task;
101
102 // When a Haskell thread makes a foreign call that re-enters
103 // Haskell, we end up with another Task associated with the
104 // current thread. We have to remember the whole stack of InCalls
105 // associated with the current Task so that we can correctly
106 // save & restore the InCall on entry to and exit from Haskell.
107 struct InCall_ *prev_stack;
108
109 // Links InCalls onto suspended_ccalls, spare_incalls
110 struct InCall_ *prev;
111 struct InCall_ *next;
112 } InCall;
113
114 typedef struct Task_ {
115 #if defined(THREADED_RTS)
116 OSThreadId id; // The OS Thread ID of this task
117
118 Condition cond; // used for sleeping & waking up this task
119 Mutex lock; // lock for the condition variable
120
121 // this flag tells the task whether it should wait on task->cond
122 // or just continue immediately. It's a workaround for the fact
123 // that signalling a condition variable doesn't do anything if the
124 // thread is already running, but we want it to be sticky.
125 rtsBool wakeup;
126 #endif
127
128 // This points to the Capability that the Task "belongs" to. If
129 // the Task owns a Capability, then task->cap points to it. If
130 // the task does not own a Capability, then either (a) if the task
131 // is a worker, then task->cap points to the Capability it belongs
132 // to, or (b) it is returning from a foreign call, then task->cap
133 // points to the Capability with the returning_worker queue that this
134 // this Task is on.
135 //
136 // When a task goes to sleep, it may be migrated to a different
137 // Capability. Hence, we always check task->cap on wakeup. To
138 // syncrhonise between the migrater and the migratee, task->lock
139 // must be held when modifying task->cap.
140 struct Capability_ *cap;
141
142 // The current top-of-stack InCall
143 struct InCall_ *incall;
144
145 nat n_spare_incalls;
146 struct InCall_ *spare_incalls;
147
148 rtsBool worker; // == rtsTrue if this is a worker Task
149 rtsBool stopped; // this task has stopped or exited Haskell
150
151 // So that we can detect when a finalizer illegally calls back into Haskell
152 rtsBool running_finalizers;
153
154 // Links tasks on the returning_tasks queue of a Capability, and
155 // on spare_workers.
156 struct Task_ *next;
157
158 // Links tasks on the all_tasks list; need ACQUIRE_LOCK(&all_tasks_mutex)
159 struct Task_ *all_next;
160 struct Task_ *all_prev;
161
162 } Task;
163
164 INLINE_HEADER rtsBool
165 isBoundTask (Task *task)
166 {
167 return (task->incall->tso != NULL);
168 }
169
170 // Linked list of all tasks.
171 //
172 extern Task *all_tasks;
173
174 // The all_tasks list is protected by the all_tasks_mutex
175 #if defined(THREADED_RTS)
176 extern Mutex all_tasks_mutex;
177 #endif
178
179 // Start and stop the task manager.
180 // Requires: sched_mutex.
181 //
182 void initTaskManager (void);
183 nat freeTaskManager (void);
184
185 // Create a new Task for a bound thread. This Task must be released
186 // by calling boundTaskExiting. The Task is cached in
187 // thread-local storage and will remain even after boundTaskExiting()
188 // has been called; to free the memory, see freeMyTask().
189 //
190 Task *newBoundTask (void);
191
192 // The current task is a bound task that is exiting.
193 //
194 void boundTaskExiting (Task *task);
195
196 // Free a Task if one was previously allocated by newBoundTask().
197 // This is not necessary unless the thread that called newBoundTask()
198 // will be exiting, or if this thread has finished calling Haskell
199 // functions.
200 //
201 void freeMyTask(void);
202
203 // Notify the task manager that a task has stopped. This is used
204 // mainly for stats-gathering purposes.
205 // Requires: sched_mutex.
206 //
207 #if defined(THREADED_RTS)
208 // In the non-threaded RTS, tasks never stop.
209 void workerTaskStop (Task *task);
210 #endif
211
212 // Put the task back on the free list, mark it stopped. Used by
213 // forkProcess().
214 //
215 void discardTasksExcept (Task *keep);
216
217 // Get the Task associated with the current OS thread (or NULL if none).
218 //
219 INLINE_HEADER Task *myTask (void);
220
221 #if defined(THREADED_RTS)
222
223 // Workers are attached to the supplied Capability. This Capability
224 // should not currently have a running_task, because the new task
225 // will become the running_task for that Capability.
226 // Requires: sched_mutex.
227 //
228 void startWorkerTask (Capability *cap);
229
230 // Interrupts a worker task that is performing an FFI call. The thread
231 // should not be destroyed.
232 //
233 void interruptWorkerTask (Task *task);
234
235 #endif /* THREADED_RTS */
236
237 // For stats
238 extern nat taskCount;
239 extern nat workerCount;
240 extern nat peakWorkerCount;
241
242 // -----------------------------------------------------------------------------
243 // INLINE functions... private from here on down:
244
245 // A thread-local-storage key that we can use to get access to the
246 // current thread's Task structure.
247 #if defined(THREADED_RTS)
248 #if ((defined(linux_HOST_OS) && \
249 (defined(i386_HOST_ARCH) || defined(x86_64_HOST_ARCH))) || \
250 (defined(mingw32_HOST_OS) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 4)) && \
251 (!defined(llvm_CC_FLAVOR))
252 #define MYTASK_USE_TLV
253 extern __thread Task *my_task;
254 #else
255 extern ThreadLocalKey currentTaskKey;
256 #endif
257 #else
258 extern Task *my_task;
259 #endif
260
261 //
262 // myTask() uses thread-local storage to find the Task associated with
263 // the current OS thread. If the current OS thread has multiple
264 // Tasks, because it has re-entered the RTS, then the task->prev_stack
265 // field is used to store the previous Task.
266 //
267 INLINE_HEADER Task *
268 myTask (void)
269 {
270 #if defined(THREADED_RTS) && !defined(MYTASK_USE_TLV)
271 return getThreadLocalVar(&currentTaskKey);
272 #else
273 return my_task;
274 #endif
275 }
276
277 INLINE_HEADER void
278 setMyTask (Task *task)
279 {
280 #if defined(THREADED_RTS) && !defined(MYTASK_USE_TLV)
281 setThreadLocalVar(&currentTaskKey,task);
282 #else
283 my_task = task;
284 #endif
285 }
286
287 // Tasks are identified by their OS thread ID, which can be serialised
288 // to StgWord64, as defined below.
289 typedef StgWord64 TaskId;
290
291 // Get a unique serialisable representation for a task id.
292 //
293 // It's only unique within the process. For example if they are emitted in a
294 // log file then it is suitable to work out which log entries are releated.
295 //
296 // This is needed because OSThreadId is an opaque type
297 // and in practice on some platforms it is a pointer type.
298 //
299 #if defined(THREADED_RTS)
300 INLINE_HEADER TaskId serialiseTaskId (OSThreadId taskID) {
301 #if defined(freebsd_HOST_OS) || defined(darwin_HOST_OS)
302 // Here OSThreadId is a pthread_t and pthread_t is a pointer, but within
303 // the process we can still use that pointer value as a unique id.
304 return (TaskId) (size_t) taskID;
305 #else
306 // On Windows, Linux and others it's an integral type to start with.
307 return (TaskId) taskID;
308 #endif
309 }
310 #endif
311
312 //
313 // Get a serialisable Id for the Task's OS thread
314 // Needed mainly for logging since the OSThreadId is an opaque type
315 INLINE_HEADER TaskId
316 serialisableTaskId (Task *task
317 #if !defined(THREADED_RTS)
318 STG_UNUSED
319 #endif
320 )
321 {
322 #if defined(THREADED_RTS)
323 return serialiseTaskId(task->id);
324 #else
325 return (TaskId) (size_t) task;
326 #endif
327 }
328
329 #include "EndPrivate.h"
330
331 #endif /* TASK_H */
332
333 // Local Variables:
334 // mode: C
335 // fill-column: 80
336 // indent-tabs-mode: nil
337 // c-basic-offset: 4
338 // buffer-file-coding-system: utf-8-unix
339 // End: