+/* Push a thread on the beginning of the run queue. Used for
+ * newly awakened threads, so they get run as soon as possible.
+ */
+#define PUSH_ON_RUN_QUEUE(tso) \
+ tso->link = run_queue_hd; \
+ run_queue_hd = tso; \
+ if (run_queue_tl == END_TSO_QUEUE) { \
+ run_queue_tl = tso; \
+ }
+
+/* Pop the first thread off the runnable queue.
+ */
+#define POP_RUN_QUEUE(pt) \
+ do { StgTSO *__tmp_t = run_queue_hd; \
+ if (__tmp_t != END_TSO_QUEUE) { \
+ run_queue_hd = __tmp_t->link; \
+ __tmp_t->link = END_TSO_QUEUE; \
+ if (run_queue_hd == END_TSO_QUEUE) { \
+ run_queue_tl = END_TSO_QUEUE; \
+ } \
+ } \
+ pt = __tmp_t; \
+ } while(0)
+
+/* Add a thread to the end of the blocked queue.
+ */
+#define APPEND_TO_BLOCKED_QUEUE(tso) \
+ ASSERT(tso->link == END_TSO_QUEUE); \
+ if (blocked_queue_hd == END_TSO_QUEUE) { \
+ blocked_queue_hd = tso; \
+ } else { \
+ blocked_queue_tl->link = tso; \
+ } \
+ blocked_queue_tl = tso;
+
+/* Signal that a runnable thread has become available, in
+ * case there are any waiting tasks to execute it.
+ */
+#if defined(RTS_SUPPORTS_THREADS)
+#define THREAD_RUNNABLE() \
+ wakeBlockedWorkerThread(); \
+ context_switch = 1;
+#else
+#define THREAD_RUNNABLE() /* nothing */
+#endif
+
+/* Check whether various thread queues are empty
+ */
+#define EMPTY_QUEUE(q) (q == END_TSO_QUEUE)
+
+#define EMPTY_RUN_QUEUE() (EMPTY_QUEUE(run_queue_hd))
+#define EMPTY_BLOCKED_QUEUE() (EMPTY_QUEUE(blocked_queue_hd))
+#define EMPTY_SLEEPING_QUEUE() (EMPTY_QUEUE(sleeping_queue))
+
+#define EMPTY_THREAD_QUEUES() (EMPTY_RUN_QUEUE() && \
+ EMPTY_BLOCKED_QUEUE() && \
+ EMPTY_SLEEPING_QUEUE())
+
+#if defined(RTS_SUPPORTS_THREADS)
+/* If no task is waiting for a capability,
+ * and if there is work to be done
+ * or if we need to wait for IO or delay requests,
+ * spawn a new worker thread.
+ */
+void
+startSchedulerTaskIfNecessary(void);
+#endif
+
+#ifdef DEBUG
+extern void sched_belch(char *s, ...)
+ GNU_ATTRIBUTE(format (printf, 1, 2));
+#endif
+
+#endif /* __SCHEDULE_H__ */