1 /* -----------------------------------------------------------------------------
2 * $Id: Schedule.h,v 1.9 1999/11/02 15:06:02 simonmar Exp $
4 * (c) The GHC Team 1998-1999
6 * Prototypes for functions in Schedule.c
7 * (RTS internal scheduler interface)
9 * ---------------------------------------------------------------------------*/
11 /* initScheduler(), exitScheduler(), startTasks()
13 * Called from STG : no
14 * Locks assumed : none
16 void initScheduler( void );
17 void exitScheduler( void );
19 void startTasks( void );
22 /* awakenBlockedQueue()
24 * Takes a pointer to the beginning of a blocked TSO queue, and
25 * wakes up the entire queue.
27 * Called from STG : yes
28 * Locks assumed : none
30 void awakenBlockedQueue(StgTSO *tso);
34 * Takes a pointer to the beginning of a blocked TSO queue, and
35 * removes the first thread, placing it on the runnable queue.
37 * Called from STG : yes
38 * Locks assumed : none
40 StgTSO *unblockOne(StgTSO *tso);
44 * Raises an exception asynchronously in the specified thread.
46 * Called from STG : yes
47 * Locks assumed : none
49 void raiseAsync(StgTSO *tso, StgClosure *exception);
53 * Raises an exception asynchronously in the specified thread.
55 * Called from STG : NO
56 * Locks assumed : sched_mutex
58 void awaitEvent(rtsBool wait); /* In Select.c */
60 /* Context switch flag.
61 * Locks required : sched_mutex
63 extern nat context_switch;
65 extern nat ticks_since_select;
69 typedef StgRegTable Capability;
71 /* Free capability list.
72 * Locks required: sched_mutex.
75 extern Capability *free_capabilities;
76 extern nat n_free_capabilities;
78 extern Capability MainRegTable;
82 * Locks required : sched_mutex
84 extern StgTSO *run_queue_hd, *run_queue_tl;
85 extern StgTSO *blocked_queue_hd, *blocked_queue_tl;
88 extern void printThreadBlockage(StgTSO *tso);
92 extern pthread_mutex_t sched_mutex;
93 extern pthread_cond_t thread_ready_cond;
94 extern pthread_cond_t gc_pending_cond;
105 extern task_info *task_ids;
108 /* -----------------------------------------------------------------------------
109 * Some convenient macros...
112 #define END_TSO_QUEUE ((StgTSO *)(void*)&END_TSO_QUEUE_closure)
113 #define END_CAF_LIST ((StgCAF *)(void*)&END_TSO_QUEUE_closure)
115 /* Add a thread to the end of the run queue.
116 * NOTE: tso->link should be END_TSO_QUEUE before calling this macro.
118 #define APPEND_TO_RUN_QUEUE(tso) \
119 ASSERT(tso->link == END_TSO_QUEUE); \
120 if (run_queue_hd == END_TSO_QUEUE) { \
121 run_queue_hd = tso; \
123 run_queue_tl->link = tso; \
127 /* Push a thread on the beginning of the run queue. Used for
128 * newly awakened threads, so they get run as soon as possible.
130 #define PUSH_ON_RUN_QUEUE(tso) \
131 tso->link = run_queue_hd; \
132 run_queue_hd = tso; \
133 if (run_queue_tl == END_TSO_QUEUE) { \
134 run_queue_tl = tso; \
137 /* Pop the first thread off the runnable queue.
139 #define POP_RUN_QUEUE() \
140 ({ StgTSO *t = run_queue_hd; \
141 if (t != END_TSO_QUEUE) { \
142 run_queue_hd = t->link; \
143 t->link = END_TSO_QUEUE; \
144 if (run_queue_hd == END_TSO_QUEUE) { \
145 run_queue_tl = END_TSO_QUEUE; \
151 /* Add a thread to the end of the blocked queue.
153 #define APPEND_TO_BLOCKED_QUEUE(tso) \
154 ASSERT(tso->link == END_TSO_QUEUE); \
155 if (blocked_queue_hd == END_TSO_QUEUE) { \
156 blocked_queue_hd = tso; \
158 blocked_queue_tl->link = tso; \
160 blocked_queue_tl = tso;
162 /* Signal that a runnable thread has become available, in
163 * case there are any waiting tasks to execute it.
166 #define THREAD_RUNNABLE() \
167 if (free_capabilities != NULL) { \
168 pthread_cond_signal(&thread_ready_cond); \
172 #define THREAD_RUNNABLE() /* nothing */