1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2006
7 * The notion of a capability is used when operating in multi-threaded
8 * environments (which the THREADED_RTS build of the RTS does), 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 THREADED_RTS build will there be multiple capabilities,
15 * in the non-threaded builds there is one global capability, namely
18 * This header file contains the functions for working with capabilities.
19 * (the main, and only, consumer of this interface is the scheduler).
21 * --------------------------------------------------------------------------*/
31 // State required by the STG virtual machine when running Haskell
32 // code. During STG execution, the BaseReg register always points
33 // to the StgRegTable of the current Capability (&cap->r).
37 nat no; // capability number.
39 // The Task currently holding this Capability. This task has
40 // exclusive access to the contents of this Capability (apart from
41 // returning_tasks_hd/returning_tasks_tl).
42 // Locks required: cap->lock.
45 // true if this Capability is running Haskell code, used for
46 // catching unsafe call-ins.
49 // The run queue. The Task owning this Capability has exclusive
50 // access to its run queue, so can wake up threads without
51 // taking a lock, and the common path through the scheduler is
56 // Tasks currently making safe foreign calls. Doubly-linked.
57 // When returning, a task first acquires the Capability before
58 // removing itself from this list, so that the GC can find all
59 // the suspended TSOs easily. Hence, when migrating a Task from
60 // the returning_tasks list, we must also migrate its entry from
62 Task *suspended_ccalling_tasks;
64 // One mutable list per generation, so we don't need to take any
65 // locks when updating an old-generation thunk. These
66 // mini-mut-lists are moved onto the respective gen->mut_list at
70 // Context switch flag. We used to have one global flag, now one
71 // per capability. Locks required : none (conflicts are harmless)
74 #if defined(THREADED_RTS)
75 // Worker Tasks waiting in the wings. Singly-linked.
78 // This lock protects running_task, returning_tasks_{hd,tl}, wakeup_queue.
81 // Tasks waiting to return from a foreign call, or waiting to make
82 // a new call-in using this Capability (NULL if empty).
83 // NB. this field needs to be modified by tasks other than the
84 // running_task, so it requires cap->lock to modify. A task can
85 // check whether it is NULL without taking the lock, however.
86 Task *returning_tasks_hd; // Singly-linked, with head/tail
87 Task *returning_tasks_tl;
89 // A list of threads to append to this Capability's run queue at
90 // the earliest opportunity. These are threads that have been
91 // woken up by another Capability.
92 StgTSO *wakeup_queue_hd;
93 StgTSO *wakeup_queue_tl;
96 // Per-capability STM-related data
97 StgTVarWatchQueue *free_tvar_watch_queues;
98 StgInvariantCheckQueue *free_invariant_check_queues;
99 StgTRecChunk *free_trec_chunks;
100 StgTRecHeader *free_trec_headers;
101 nat transaction_tokens;
105 }; // typedef Capability, defined in RtsAPI.h
108 #if defined(THREADED_RTS)
109 #define ASSERT_TASK_ID(task) ASSERT(task->id == osThreadId())
111 #define ASSERT_TASK_ID(task) /*empty*/
114 // These properties should be true when a Task is holding a Capability
115 #define ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task) \
116 ASSERT(cap->running_task != NULL && cap->running_task == task); \
117 ASSERT(task->cap == cap); \
118 ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task)
120 // Sometimes a Task holds a Capability, but the Task is not associated
121 // with that Capability (ie. task->cap != cap). This happens when
122 // (a) a Task holds multiple Capabilities, and (b) when the current
123 // Task is bound, its thread has just blocked, and it may have been
124 // moved to another Capability.
125 #define ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task) \
126 ASSERT(cap->run_queue_hd == END_TSO_QUEUE ? \
127 cap->run_queue_tl == END_TSO_QUEUE : 1); \
128 ASSERT(myTask() == task); \
129 ASSERT_TASK_ID(task);
131 // Converts a *StgRegTable into a *Capability.
133 INLINE_HEADER Capability *
134 regTableToCapability (StgRegTable *reg)
136 return (Capability *)((void *)((unsigned char*)reg - sizeof(StgFunTable)));
139 // Initialise the available capabilities.
141 void initCapabilities (void);
143 // Release a capability. This is called by a Task that is exiting
144 // Haskell to make a foreign call, or in various other cases when we
145 // want to relinquish a Capability that we currently hold.
147 // ASSUMES: cap->running_task is the current Task.
149 #if defined(THREADED_RTS)
150 void releaseCapability (Capability* cap);
151 void releaseCapability_ (Capability* cap); // assumes cap->lock is held
153 // releaseCapability() is empty in non-threaded RTS
154 INLINE_HEADER void releaseCapability (Capability* cap STG_UNUSED) {};
155 INLINE_HEADER void releaseCapability_ (Capability* cap STG_UNUSED) {};
159 // one global capability
160 extern Capability MainCapability;
163 // Array of all the capabilities
165 extern nat n_capabilities;
166 extern Capability *capabilities;
168 // The Capability that was last free. Used as a good guess for where
169 // to assign new threads.
171 extern Capability *last_free_capability;
173 // GC indicator, in scope for the scheduler
174 extern volatile StgWord waiting_for_gc;
176 // Acquires a capability at a return point. If *cap is non-NULL, then
177 // this is taken as a preference for the Capability we wish to
180 // OS threads waiting in this function get priority over those waiting
181 // in waitForCapability().
183 // On return, *cap is non-NULL, and points to the Capability acquired.
185 void waitForReturnCapability (Capability **cap/*in/out*/, Task *task);
187 INLINE_HEADER void recordMutableCap (StgClosure *p, Capability *cap, nat gen);
189 #if defined(THREADED_RTS)
191 // Gives up the current capability IFF there is a higher-priority
192 // thread waiting for it. This happens in one of two ways:
194 // (a) we are passing the capability to another OS thread, so
195 // that it can run a bound Haskell thread, or
197 // (b) there is an OS thread waiting to return from a foreign call
199 // On return: *pCap is NULL if the capability was released. The
200 // current task should then re-acquire it using waitForCapability().
202 void yieldCapability (Capability** pCap, Task *task);
204 // Acquires a capability for doing some work.
206 // On return: pCap points to the capability.
208 void waitForCapability (Task *task, Mutex *mutex, Capability **pCap);
210 // Wakes up a thread on a Capability (probably a different Capability
211 // from the one held by the current Task).
213 void wakeupThreadOnCapability (Capability *my_cap, Capability *other_cap,
216 // Wakes up a worker thread on just one Capability, used when we
217 // need to service some global event.
219 void prodOneCapability (void);
221 // Similar to prodOneCapability(), but prods all of them.
223 void prodAllCapabilities (void);
225 // Waits for a capability to drain of runnable threads and workers,
226 // and then acquires it. Used at shutdown time.
228 void shutdownCapability (Capability *cap, Task *task, rtsBool wait_foreign);
230 // Attempt to gain control of a Capability if it is free.
232 rtsBool tryGrabCapability (Capability *cap, Task *task);
234 #else // !THREADED_RTS
236 // Grab a capability. (Only in the non-threaded RTS; in the threaded
237 // RTS one of the waitFor*Capability() functions must be used).
239 extern void grabCapability (Capability **pCap);
241 #endif /* !THREADED_RTS */
243 // cause all capabilities to context switch as soon as possible.
244 void setContextSwitches(void);
246 // Free a capability on exit
247 void freeCapability (Capability *cap);
250 void markSomeCapabilities (evac_fn evac, void *user, nat i0, nat delta);
251 void markCapabilities (evac_fn evac, void *user);
252 void traverseSparkQueues (evac_fn evac, void *user);
254 /* -----------------------------------------------------------------------------
255 * INLINE functions... private below here
256 * -------------------------------------------------------------------------- */
259 recordMutableCap (StgClosure *p, Capability *cap, nat gen)
263 // We must own this Capability in order to modify its mutable list.
264 ASSERT(cap->running_task == myTask());
265 bd = cap->mut_lists[gen];
266 if (bd->free >= bd->start + BLOCK_SIZE_W) {
268 new_bd = allocBlock_lock();
271 cap->mut_lists[gen] = bd;
273 *bd->free++ = (StgWord)p;
276 #endif /* CAPABILITY_H */