1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2001-2005
7 * For details on the high-level design, see
8 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Scheduler
10 * -------------------------------------------------------------------------*/
17 #include "BeginPrivate.h"
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.
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().
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.
40 The OS thread named in the Task structure has exclusive access to
41 the structure, as long as it is the running_task of its Capability.
42 That is, if (task->cap->running_task == task), then task->id owns
43 the Task. Otherwise the Task is owned by the owner of the parent
44 data structure on which it is sleeping; for example, if the task is
45 sleeping on spare_workers field of a Capability, then the owner of the
46 Capability has access to the Task.
48 When a task is migrated from sleeping on one Capability to another,
49 its task->cap field must be modified. When the task wakes up, it
50 will read the new value of task->cap to find out which Capability
51 it belongs to. Hence some synchronisation is required on
52 task->cap, and this is why we have task->lock.
54 If the Task is not currently owned by task->id, then the thread is
57 (a) waiting on the condition task->cond. The Task is either
58 (1) a bound Task, the TSO will be on a queue somewhere
59 (2) a worker task, on the spare_workers queue of task->cap.
61 (b) making a foreign call. The InCall will be on the
62 suspended_ccalls list.
64 We re-establish ownership in each case by respectively
66 (a) the task is currently blocked in yieldCapability().
67 This call will return when we have ownership of the Task and
68 a Capability. The Capability we get might not be the same
69 as the one we had when we called yieldCapability().
71 (b) we must call resumeThread(task), which will safely establish
72 ownership of the Task and a Capability.
75 // The InCall structure represents either a single in-call from C to
76 // Haskell, or a worker thread.
77 typedef struct InCall_ {
78 StgTSO * tso; // the bound TSO (or NULL for a worker)
80 StgTSO * suspended_tso; // the TSO is stashed here when we
81 // make a foreign call (NULL otherwise);
83 Capability *suspended_cap; // The capability that the
84 // suspended_tso is on, because
85 // we can't read this from the TSO
86 // without owning a Capability in the
89 SchedulerStatus stat; // return status
90 StgClosure ** ret; // return value
94 // When a Haskell thread makes a foreign call that re-enters
95 // Haskell, we end up with another Task associated with the
96 // current thread. We have to remember the whole stack of InCalls
97 // associated with the current Task so that we can correctly
98 // save & restore the InCall on entry to and exit from Haskell.
99 struct InCall_ *prev_stack;
101 // Links InCalls onto suspended_ccalls, spare_incalls
102 struct InCall_ *prev;
103 struct InCall_ *next;
106 typedef struct Task_ {
107 #if defined(THREADED_RTS)
108 OSThreadId id; // The OS Thread ID of this task
110 Condition cond; // used for sleeping & waking up this task
111 Mutex lock; // lock for the condition variable
113 // this flag tells the task whether it should wait on task->cond
114 // or just continue immediately. It's a workaround for the fact
115 // that signalling a condition variable doesn't do anything if the
116 // thread is already running, but we want it to be sticky.
120 // This points to the Capability that the Task "belongs" to. If
121 // the Task owns a Capability, then task->cap points to it. If
122 // the task does not own a Capability, then either (a) if the task
123 // is a worker, then task->cap points to the Capability it belongs
124 // to, or (b) it is returning from a foreign call, then task->cap
125 // points to the Capability with the returning_worker queue that this
128 // When a task goes to sleep, it may be migrated to a different
129 // Capability. Hence, we always check task->cap on wakeup. To
130 // syncrhonise between the migrater and the migratee, task->lock
131 // must be held when modifying task->cap.
132 struct Capability_ *cap;
134 // The current top-of-stack InCall
135 struct InCall_ *incall;
138 struct InCall_ *spare_incalls;
140 rtsBool worker; // == rtsTrue if this is a worker Task
141 rtsBool stopped; // this task has stopped or exited Haskell
143 // So that we can detect when a finalizer illegally calls back into Haskell
144 rtsBool running_finalizers;
146 // Stats that we collect about this task
147 // ToDo: we probably want to put this in a separate TaskStats
148 // structure, so we can share it between multiple Tasks. We don't
149 // really want separate stats for each call in a nested chain of
150 // foreign->haskell->foreign->haskell calls, but we'll get a
151 // separate Task for each of the haskell calls.
152 Ticks elapsedtimestart;
159 // Links tasks on the returning_tasks queue of a Capability, and
163 // Links tasks on the all_tasks list
164 struct Task_ *all_link;
168 INLINE_HEADER rtsBool
169 isBoundTask (Task *task)
171 return (task->incall->tso != NULL);
175 // Linked list of all tasks.
177 extern Task *all_tasks;
179 // Start and stop the task manager.
180 // Requires: sched_mutex.
182 void initTaskManager (void);
183 nat freeTaskManager (void);
185 // Create a new Task for a bound thread
186 // Requires: sched_mutex.
188 Task *newBoundTask (void);
190 // The current task is a bound task that is exiting.
191 // Requires: sched_mutex.
193 void boundTaskExiting (Task *task);
195 // Notify the task manager that a task has stopped. This is used
196 // mainly for stats-gathering purposes.
197 // Requires: sched_mutex.
199 #if defined(THREADED_RTS)
200 // In the non-threaded RTS, tasks never stop.
201 void workerTaskStop (Task *task);
204 // Record the time spent in this Task.
205 // This is called by workerTaskStop() but not by boundTaskExiting(),
206 // because it would impose an extra overhead on call-in.
208 void taskTimeStamp (Task *task);
210 // Put the task back on the free list, mark it stopped. Used by
213 void discardTasksExcept (Task *keep);
215 // Get the Task associated with the current OS thread (or NULL if none).
217 INLINE_HEADER Task *myTask (void);
219 #if defined(THREADED_RTS)
221 // Workers are attached to the supplied Capability. This Capability
222 // should not currently have a running_task, because the new task
223 // will become the running_task for that Capability.
224 // Requires: sched_mutex.
226 void startWorkerTask (Capability *cap);
228 #endif /* THREADED_RTS */
230 // -----------------------------------------------------------------------------
231 // INLINE functions... private from here on down:
233 // A thread-local-storage key that we can use to get access to the
234 // current thread's Task structure.
235 #if defined(THREADED_RTS)
236 #if defined(linux_HOST_OS) && \
237 (defined(i386_HOST_ARCH) || defined(x86_64_HOST_ARCH))
238 #define MYTASK_USE_TLV
239 extern __thread Task *my_task;
241 extern ThreadLocalKey currentTaskKey;
244 extern Task *my_task;
248 // myTask() uses thread-local storage to find the Task associated with
249 // the current OS thread. If the current OS thread has multiple
250 // Tasks, because it has re-entered the RTS, then the task->prev_stack
251 // field is used to store the previous Task.
256 #if defined(THREADED_RTS) && !defined(MYTASK_USE_TLV)
257 return getThreadLocalVar(¤tTaskKey);
264 setMyTask (Task *task)
266 #if defined(THREADED_RTS) && !defined(MYTASK_USE_TLV)
267 setThreadLocalVar(¤tTaskKey,task);
273 #include "EndPrivate.h"