1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001
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 * --------------------------------------------------------------------------*/
22 #ifndef __CAPABILITY_H__
23 #define __CAPABILITY_H__
25 /* ToDo: assume that RtsFlags.h has been included at usage sites of Capability.h? */
28 extern Capability MainCapability;
31 extern void initCapabilities(void);
32 extern void grabCapability(Capability** pCap);
33 extern void releaseCapability(Capability* cap);
35 extern nat rts_n_free_capabilities;
36 #if defined(RTS_SUPPORTS_THREADS)
37 /* number of worker threads waiting for a return capability
39 extern nat rts_n_waiting_workers;
41 extern void grabReturnCapability(Mutex* pMutex, Capability** pCap);
42 extern void yieldToReturningWorker(Mutex* pMutex, Capability** pCap, Condition *pThreadCond);
43 extern void waitForWorkCapability(Mutex* pMutex, Capability** pCap, Condition *pThreadCond);
44 extern void passCapability(Mutex* pMutex, Capability* cap, Condition *pTargetThreadCond);
45 extern void passCapabilityToWorker(Mutex* pMutex, Capability* cap);
47 static inline rtsBool needToYieldToReturningWorker(void)
49 return rts_n_waiting_workers > 0;
52 static inline nat getFreeCapabilities (void)
54 return rts_n_free_capabilities;
57 static inline rtsBool noCapabilities (void)
59 return (rts_n_free_capabilities == 0);
62 static inline rtsBool allFreeCapabilities (void)
65 return (rts_n_free_capabilities == RtsFlags.ParFlags.nNodes);
67 return (rts_n_free_capabilities == 1);
71 #endif /* RTS_SUPPORTS_THREADS */
73 #endif /* __CAPABILITY_H__ */