+#ifndef CAPABILITY_H
+#define CAPABILITY_H
+
+#include "RtsFlags.h"
+#include "Task.h"
+
+struct Capability_ {
+ // State required by the STG virtual machine when running Haskell
+ // code. During STG execution, the BaseReg register always points
+ // to the StgRegTable of the current Capability (&cap->r).
+ StgFunTable f;
+ StgRegTable r;
+
+ nat no; // capability number.
+
+ // The Task currently holding this Capability. This task has
+ // exclusive access to the contents of this Capability (apart from
+ // returning_tasks_hd/returning_tasks_tl).
+ // Locks required: cap->lock.
+ Task *running_task;
+
+ // true if this Capability is running Haskell code, used for
+ // catching unsafe call-ins.
+ rtsBool in_haskell;
+
+ // The run queue. The Task owning this Capability has exclusive
+ // access to its run queue, so can wake up threads without
+ // taking a lock, and the common path through the scheduler is
+ // also lock-free.
+ StgTSO *run_queue_hd;
+ StgTSO *run_queue_tl;
+
+ // Tasks currently making safe foreign calls. Doubly-linked.
+ // When returning, a task first acquires the Capability before
+ // removing itself from this list, so that the GC can find all
+ // the suspended TSOs easily. Hence, when migrating a Task from
+ // the returning_tasks list, we must also migrate its entry from
+ // this list.
+ Task *suspended_ccalling_tasks;
+
+ // One mutable list per generation, so we don't need to take any
+ // locks when updating an old-generation thunk. These
+ // mini-mut-lists are moved onto the respective gen->mut_list at
+ // each GC.
+ bdescr **mut_lists;
+
+#if defined(THREADED_RTS)
+ // Worker Tasks waiting in the wings. Singly-linked.
+ Task *spare_workers;
+
+ // This lock protects running_task and returning_tasks_{hd,tl}.
+ Mutex lock;
+
+ // Tasks waiting to return from a foreign call, or waiting to make
+ // a new call-in using this Capability (NULL if empty).
+ // NB. this field needs to be modified by tasks other than the
+ // running_task, so it requires cap->lock to modify. A task can
+ // check whether it is NULL without taking the lock, however.
+ Task *returning_tasks_hd; // Singly-linked, with head/tail
+ Task *returning_tasks_tl;
+#endif
+
+ // Per-capability STM-related data
+ StgTVarWaitQueue *free_tvar_wait_queues;
+ StgTRecChunk *free_trec_chunks;
+ StgTRecHeader *free_trec_headers;
+ nat transaction_tokens;
+}; // typedef Capability, defined in RtsAPI.h
+
+
+#if defined(THREADED_RTS)
+#define ASSERT_TASK_ID(task) ASSERT(task->id == osThreadId())
+#else
+#define ASSERT_TASK_ID(task) /*empty*/
+#endif
+
+// These properties should be true when a Task is holding a Capability
+#define ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task) \
+ ASSERT(cap->running_task != NULL && cap->running_task == task); \
+ ASSERT(task->cap == cap); \
+ ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task)
+
+// Sometimes a Task holds a Capability, but the Task is not associated
+// with that Capability (ie. task->cap != cap). This happens when
+// (a) a Task holds multiple Capabilities, and (b) when the current
+// Task is bound, its thread has just blocked, and it may have been
+// moved to another Capability.
+#define ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task) \
+ ASSERT(cap->run_queue_hd == END_TSO_QUEUE ? \
+ cap->run_queue_tl == END_TSO_QUEUE : 1); \
+ ASSERT(myTask() == task); \
+ ASSERT_TASK_ID(task);
+
+// Converts a *StgRegTable into a *Capability.
+//
+INLINE_HEADER Capability *
+regTableToCapability (StgRegTable *reg)
+{
+ return (Capability *)((void *)((unsigned char*)reg - sizeof(StgFunTable)));
+}
+
+// Initialise the available capabilities.
+//
+void initCapabilities (void);