1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2003
7 * The notion of a capability is used when operating in multi-threaded
8 * environments (which the SMP and Threads builds of the RTS do), to
9 * hold all the state an OS thread/task needs to run Haskell code:
10 * its STG registers, a pointer to its TSO, a nursery etc. During
11 * STG execution, a pointer to the capabilitity is kept in a
14 * Only in an SMP build will there be multiple capabilities, the threaded
15 * RTS and other non-threaded builds, there is one global capability,
16 * namely MainRegTable.
18 * This header file contains the functions for working with capabilities.
19 * (the main, and only, consumer of this interface is the scheduler).
21 * --------------------------------------------------------------------------*/
30 // State required by the STG virtual machine when running Haskell
31 // code. During STG execution, the BaseReg register always points
32 // to the StgRegTable of the current Capability (&cap->r).
36 nat no; // capability number.
38 // The Task currently holding this Capability. This task has
39 // exclusive access to the contents of this Capability (apart from
40 // returning_tasks_hd/returning_tasks_tl).
41 // Locks required: cap->lock.
44 // true if this Capability is running Haskell code, used for
45 // catching unsafe call-ins.
48 // The run queue. The Task owning this Capability has exclusive
49 // access to its run queue, so can wake up threads without
50 // taking a lock, and the common path through the scheduler is
55 // Tasks currently making safe foreign calls. Doubly-linked.
56 // When returning, a task first acquires the Capability before
57 // removing itself from this list, so that the GC can find all
58 // the suspended TSOs easily. Hence, when migrating a Task from
59 // the returning_tasks list, we must also migrate its entry from
61 Task *suspended_ccalling_tasks;
63 // One mutable list per generation, so we don't need to take any
64 // locks when updating an old-generation thunk. These
65 // mini-mut-lists are moved onto the respective gen->mut_list at
69 #if defined(THREADED_RTS)
70 // Worker Tasks waiting in the wings. Singly-linked.
73 // This lock protects running_task and returning_tasks_{hd,tl}.
76 // Tasks waiting to return from a foreign call, or waiting to make
77 // a new call-in using this Capability (NULL if empty).
78 // NB. this field needs to be modified by tasks other than the
79 // running_task, so it requires cap->lock to modify. A task can
80 // check whether it is NULL without taking the lock, however.
81 Task *returning_tasks_hd; // Singly-linked, with head/tail
82 Task *returning_tasks_tl;
84 }; // typedef Capability, defined in RtsAPI.h
87 #if defined(THREADED_RTS)
88 #define ASSERT_TASK_ID(task) ASSERT(task->id == osThreadId())
90 #define ASSERT_TASK_ID(task) /*empty*/
93 // These properties should be true when a Task is holding a Capability
94 #define ASSERT_CAPABILITY_INVARIANTS(cap,task) \
95 ASSERT(cap->running_task != NULL && cap->running_task == task); \
96 ASSERT(task->cap == cap); \
97 ASSERT(cap->run_queue_hd == END_TSO_QUEUE ? \
98 cap->run_queue_tl == END_TSO_QUEUE : 1); \
99 ASSERT(myTask() == task); \
100 ASSERT_TASK_ID(task);
102 // Converts a *StgRegTable into a *Capability.
104 INLINE_HEADER Capability *
105 regTableToCapability (StgRegTable *reg)
107 return (Capability *)((void *)((unsigned char*)reg - sizeof(StgFunTable)));
110 // Initialise the available capabilities.
112 void initCapabilities (void);
114 // Release a capability. This is called by a Task that is exiting
115 // Haskell to make a foreign call, or in various other cases when we
116 // want to relinquish a Capability that we currently hold.
118 // ASSUMES: cap->running_task is the current Task.
120 #if defined(THREADED_RTS)
121 void releaseCapability (Capability* cap);
122 void releaseCapability_ (Capability* cap); // assumes cap->lock is held
124 // releaseCapability() is empty in non-threaded RTS
125 INLINE_HEADER void releaseCapability (Capability* cap STG_UNUSED) {};
126 INLINE_HEADER void releaseCapability_ (Capability* cap STG_UNUSED) {};
129 #if !IN_STG_CODE && !defined(SMP)
130 // for non-SMP, we have one global capability
131 extern Capability MainCapability;
134 // Array of all the capabilities
136 extern nat n_capabilities;
137 extern Capability *capabilities;
139 // The Capability that was last free. Used as a good guess for where
140 // to assign new threads.
142 extern Capability *last_free_capability;
144 // Acquires a capability at a return point. If *cap is non-NULL, then
145 // this is taken as a preference for the Capability we wish to
148 // OS threads waiting in this function get priority over those waiting
149 // in waitForCapability().
151 // On return, *cap is non-NULL, and points to the Capability acquired.
153 void waitForReturnCapability (Capability **cap/*in/out*/, Task *task);
155 INLINE_HEADER void recordMutableCap (StgClosure *p, Capability *cap, nat gen);
157 #if defined(THREADED_RTS)
159 // Gives up the current capability IFF there is a higher-priority
160 // thread waiting for it. This happens in one of two ways:
162 // (a) we are passing the capability to another OS thread, so
163 // that it can run a bound Haskell thread, or
165 // (b) there is an OS thread waiting to return from a foreign call
167 // On return: *pCap is NULL if the capability was released. The
168 // current task should then re-acquire it using waitForCapability().
170 void yieldCapability (Capability** pCap, Task *task);
172 // Acquires a capability for doing some work.
174 // On return: pCap points to the capability.
176 void waitForCapability (Task *task, Mutex *mutex, Capability **pCap);
178 // Wakes up a worker thread on just one Capability, used when we
179 // need to service some global event.
181 void prodOneCapability (void);
183 // Similar to prodOneCapability(), but prods all of them.
185 void prodAllCapabilities (void);
187 // Waits for a capability to drain of runnable threads and workers,
188 // and then acquires it. Used at shutdown time.
190 void shutdownCapability (Capability *cap, Task *task);
192 // Attempt to gain control of a Capability if it is free.
194 rtsBool tryGrabCapability (Capability *cap, Task *task);
196 #else // !THREADED_RTS
198 // Grab a capability. (Only in the non-threaded RTS; in the threaded
199 // RTS one of the waitFor*Capability() functions must be used).
201 extern void grabCapability (Capability **pCap);
203 #endif /* !THREADED_RTS */
205 /* -----------------------------------------------------------------------------
206 * INLINE functions... private below here
207 * -------------------------------------------------------------------------- */
210 recordMutableCap (StgClosure *p, Capability *cap, nat gen)
214 bd = cap->mut_lists[gen];
215 if (bd->free >= bd->start + BLOCK_SIZE_W) {
217 new_bd = allocBlock_lock();
220 cap->mut_lists[gen] = bd;
222 *bd->free++ = (StgWord)p;
225 #endif /* CAPABILITY_H */