1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2001-
5 * The task manager subsystem. Tasks execute STG code, with this
6 * module providing the API which the Scheduler uses to control their
7 * creation and destruction.
9 * Two kinds of RTS builds uses 'tasks' - the SMP and the
10 * 'native thread-friendly' builds.
12 * The SMP build lets multiple tasks concurrently execute STG code,
13 * all sharing vital internal RTS data structures in a controlled manner
14 * (see details elsewhere...ToDo: fill in ref!)
16 * The 'threads' build has at any one time only one task executing STG
17 * code, other tasks are either busy executing code outside the RTS
18 * (e.g., a C call) or waiting for their turn to (again) evaluate some
19 * STG code. A task relinquishes its RTS token when it is asked to
20 * evaluate an external (C) call.
22 * -------------------------------------------------------------------------*/
24 #if defined(RTS_SUPPORTS_THREADS) /* to the end */
26 #include "OSThreads.h"
32 /* There's not all that much code that is shared between the
33 * SMP and threads version of the 'task manager.' A sign
34 * that the code ought to be structured differently..(Maybe ToDo).
38 * The following Task Manager-local variables are assumed to be
39 * accessed with the RTS lock in hand.
42 static TaskInfo* taskTable;
44 /* upper bound / the number of tasks created. */
46 /* number of tasks currently created */
51 startTaskManager( nat maxCount, void (*taskStart)(void) )
54 static int initialized = 0;
60 /* allocate table holding task metadata */
63 taskTable = stgMallocBytes(maxCount * sizeof(TaskInfo),
64 "startTaskManager:tasks");
66 /* and eagerly create them all. */
67 for (i = 0; i < maxCount; i++) {
77 startTask ( void (*taskStart)(void) )
82 r = createOSThread(&tid,taskStart);
84 barf("startTask: Can't create new task");
87 taskTable[taskCount].id = tid;
88 taskTable[taskCount].mut_time = 0.0;
89 taskTable[taskCount].mut_etime = 0.0;
90 taskTable[taskCount].gc_time = 0.0;
91 taskTable[taskCount].gc_etime = 0.0;
92 taskTable[taskCount].elapsedtimestart = stat_getElapsedTime();
94 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: Started task: %ld\n",tid););
102 OSThreadId tid = osThreadId();
104 /* Don't want to use pthread_cancel, since we'd have to install
105 * these silly exception handlers (pthread_cleanup_{push,pop}) around
109 /* Cancel all our tasks */
110 for (i = 0; i < RtsFlags.ParFlags.nNodes; i++) {
111 pthread_cancel(taskTable[i].id);
114 /* Wait for all the tasks to terminate */
115 for (i = 0; i < maxCount; i++) {
116 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: waiting for task %ld\n",
118 pthread_join(taskTable[i].id, NULL);
122 /* Send 'em all a SIGHUP. That should shut 'em up. */
123 await_death = maxCount - 1;
124 for (i = 0; i < maxCount; i++) {
125 /* don't cancel the thread running this piece of code. */
126 if ( taskTable[i].id != tid ) {
127 pthread_kill(taskTable[i].id,SIGTERM);
130 while (await_death > 0) {
138 /************ THREADS version *****************/
141 startTaskManager( nat maxCount,
142 void (*taskStart)(void) STG_UNUSED )
144 /* In the threaded case, maxCount is used to limit the
145 the creation of worker tasks. Tasks are created lazily, i.e.,
146 when the current task gives up the token on executing
154 startTask ( void (*taskStart)(void) )
159 /* If more than one worker thread is known to be blocked waiting
160 on thread_ready_cond, don't create a new one.
162 if ( rts_n_waiting_tasks > 0) {
163 IF_DEBUG(scheduler,fprintf(stderr,
164 "scheduler: startTask: %d tasks waiting, not creating new one.\n",
165 rts_n_waiting_tasks););
166 // the task will run as soon as a capability is available,
167 // so there's no need to wake it.
171 /* If the task limit has been reached, just return. */
172 if (maxTasks > 0 && taskCount == maxTasks) {
173 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: startTask: task limit (%d) reached, not creating new one.\n",maxTasks));
178 r = createOSThread(&tid,taskStart);
180 barf("startTask: Can't create new task");
184 IF_DEBUG(scheduler,fprintf(stderr,"scheduler: startTask: new task %ld (total_count: %d; waiting: %d)\n", tid, taskCount, rts_n_waiting_tasks););
198 #endif /* RTS_SUPPORTS_THREADS */