1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2001-2005
7 * -------------------------------------------------------------------------*/
20 A task is an OSThread that runs Haskell code. Every OSThread
21 created by the RTS for the purposes of running Haskell code is a
22 Task, and OS threads that enter the Haskell RTS for the purposes of
23 making a call-in are also Tasks.
25 In the THREADED_RTS build, multiple Tasks may all be running
26 Haskell code simultaneously. A task relinquishes its Capability
27 when it is asked to evaluate an external (C) call.
29 In general, there may be multiple Tasks associated with a given OS
30 thread. A second Task is created when one Task makes a foreign
31 call from Haskell, and subsequently calls back in to Haskell,
32 creating a new bound thread.
34 A particular Task structure can belong to more than one OS thread
35 over its lifetime. This is to avoid creating an unbounded number
36 of Task structures. The stats just accumulate.
41 The OS thread named in the Task structure has exclusive access to
42 the structure, as long as it is the running_task of its Capability.
43 That is, if (task->cap->running_task == task), then task->id owns
44 the Task. Otherwise the Task is owned by the owner of the parent
45 data structure on which it is sleeping; for example, if the task is
46 sleeping on spare_workers field of a Capability, then the owner of the
47 Capability has access to the Task.
49 When a task is migrated from sleeping on one Capability to another,
50 its task->cap field must be modified. When the task wakes up, it
51 will read the new value of task->cap to find out which Capability
52 it belongs to. Hence some synchronisation is required on
53 task->cap, and this is why we have task->lock.
55 If the Task is not currently owned by task->id, then the thread is
58 (a) waiting on the condition task->cond. The Task is either
59 (1) a bound Task, the TSO will be on a queue somewhere
60 (2) a worker task, on the spare_workers queue of task->cap.
62 (b) making a foreign call. The Task will be on the
63 suspended_ccalling_tasks list.
65 We re-establish ownership in each case by respectively
67 (a) the task is currently blocked in yieldCapability().
68 This call will return when we have ownership of the Task and
69 a Capability. The Capability we get might not be the same
70 as the one we had when we called yieldCapability().
72 (b) we must call resumeThread(task), which will safely establish
73 ownership of the Task and a Capability.
76 typedef struct Task_ {
77 #if defined(THREADED_RTS)
78 OSThreadId id; // The OS Thread ID of this task
81 // This points to the Capability that the Task "belongs" to. If
82 // the Task owns a Capability, then task->cap points to it. If
83 // the task does not own a Capability, then either (a) if the task
84 // is a worker, then task->cap points to the Capability it belongs
85 // to, or (b) it is returning from a foreign call, then task->cap
86 // points to the Capability with the returning_worker queue that this
89 // When a task goes to sleep, it may be migrated to a different
90 // Capability. Hence, we always check task->cap on wakeup. To
91 // syncrhonise between the migrater and the migratee, task->lock
92 // must be held when modifying task->cap.
93 struct Capability_ *cap;
95 rtsBool stopped; // this task has stopped or exited Haskell
96 StgTSO * suspended_tso; // the TSO is stashed here when we
97 // make a foreign call (NULL otherwise);
99 // The following 3 fields are used by bound threads:
100 StgTSO * tso; // the bound TSO (or NULL)
101 SchedulerStatus stat; // return status
102 StgClosure ** ret; // return value
104 #if defined(THREADED_RTS)
105 Condition cond; // used for sleeping & waking up this task
106 Mutex lock; // lock for the condition variable
108 // this flag tells the task whether it should wait on task->cond
109 // or just continue immediately. It's a workaround for the fact
110 // that signalling a condition variable doesn't do anything if the
111 // thread is already running, but we want it to be sticky.
115 // Stats that we collect about this task
116 // ToDo: we probably want to put this in a separate TaskStats
117 // structure, so we can share it between multiple Tasks. We don't
118 // really want separate stats for each call in a nested chain of
119 // foreign->haskell->foreign->haskell calls, but we'll get a
120 // separate Task for each of the haskell calls.
121 Ticks elapsedtimestart;
128 // Links tasks onto various lists. (ToDo: do we need double
133 // Links tasks on the returning_tasks queue of a Capability.
134 struct Task_ *return_link;
136 // Links tasks on the all_tasks list
137 struct Task_ *all_link;
139 // When a Haskell thread makes a foreign call that re-enters
140 // Haskell, we end up with another Task associated with the
141 // current thread. We have to remember the whole stack of Tasks
142 // associated with the current thread so that we can correctly
143 // save & restore the thread-local current task pointer.
144 struct Task_ *prev_stack;
147 INLINE_HEADER rtsBool
148 isBoundTask (Task *task)
150 return (task->tso != NULL);
154 // Linked list of all tasks.
156 extern Task *all_tasks;
158 // Start and stop the task manager.
159 // Requires: sched_mutex.
161 void initTaskManager (void);
162 nat freeTaskManager (void);
164 // Create a new Task for a bound thread
165 // Requires: sched_mutex.
167 Task *newBoundTask (void);
169 // The current task is a bound task that is exiting.
170 // Requires: sched_mutex.
172 void boundTaskExiting (Task *task);
174 // This must be called when a new Task is associated with the current
175 // thread. It sets up the thread-local current task pointer so that
176 // myTask() can work.
177 INLINE_HEADER void taskEnter (Task *task);
179 // Notify the task manager that a task has stopped. This is used
180 // mainly for stats-gathering purposes.
181 // Requires: sched_mutex.
183 #if defined(THREADED_RTS)
184 // In the non-threaded RTS, tasks never stop.
185 void workerTaskStop (Task *task);
188 // Record the time spent in this Task.
189 // This is called by workerTaskStop() but not by boundTaskExiting(),
190 // because it would impose an extra overhead on call-in.
192 void taskTimeStamp (Task *task);
194 // Put the task back on the free list, mark it stopped. Used by
197 void discardTask (Task *task);
199 // Get the Task associated with the current OS thread (or NULL if none).
201 INLINE_HEADER Task *myTask (void);
203 #if defined(THREADED_RTS)
205 // Workers are attached to the supplied Capability. This Capability
206 // should not currently have a running_task, because the new task
207 // will become the running_task for that Capability.
208 // Requires: sched_mutex.
210 void startWorkerTask (struct Capability_ *cap,
211 void OSThreadProcAttr (*taskStart)(Task *task));
213 #endif /* THREADED_RTS */
215 // -----------------------------------------------------------------------------
216 // INLINE functions... private from here on down:
218 // A thread-local-storage key that we can use to get access to the
219 // current thread's Task structure.
220 #if defined(THREADED_RTS)
221 extern ThreadLocalKey currentTaskKey;
223 extern Task *my_task;
227 // myTask() uses thread-local storage to find the Task associated with
228 // the current OS thread. If the current OS thread has multiple
229 // Tasks, because it has re-entered the RTS, then the task->prev_stack
230 // field is used to store the previous Task.
235 #if defined(THREADED_RTS)
236 return getThreadLocalVar(¤tTaskKey);
243 setMyTask (Task *task)
245 #if defined(THREADED_RTS)
246 setThreadLocalVar(¤tTaskKey,task);
252 // This must be called when a new Task is associated with the current
253 // thread. It sets up the thread-local current task pointer so that
254 // myTask() can work.
256 taskEnter (Task *task)
258 // save the current value, just in case this Task has been created
259 // as a result of re-entering the RTS (defaults to NULL):
260 task->prev_stack = myTask();