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 * --------------------------------------------------------------------------*/
23 #ifndef __CAPABILITY_H__
24 #define __CAPABILITY_H__
26 // Initialised the available capabilities.
28 extern void initCapabilities( void );
30 // Releases a capability
32 extern void releaseCapability( Capability* cap );
34 #ifdef RTS_SUPPORTS_THREADS
35 // Gives up the current capability IFF there is a higher-priority
36 // thread waiting for it. This happens in one of two ways:
38 // (a) we are passing the capability to another OS thread, so
39 // that it can run a bound Haskell thread, or
41 // (b) there is an OS thread waiting to return from a foreign call
43 // On return: *pCap is NULL if the capability was released. The
44 // current worker thread should then re-acquire it using
45 // waitForCapability().
47 extern void yieldCapability( Capability **pCap );
49 // Acquires a capability for doing some work.
51 // If the current OS thread is bound to a particular Haskell thread,
52 // then pThreadCond points to a condition variable for waking up this
53 // OS thread when its Haskell thread is ready to run.
55 // On return: pCap points to the capability.
56 extern void waitForCapability( Mutex* pMutex, Capability** pCap,
57 Condition *pThreadCond );
59 // Acquires a capability at a return point.
61 // OS threads waiting in this function get priority over those waiting
62 // in waitForWorkCapability().
64 // On return: pCap points to the capability.
65 extern void waitForReturnCapability(Mutex* pMutex, Capability** pCap);
67 // Signals that the next time a capability becomes free, it should
68 // be transfered to a particular OS thread, identified by the
69 // condition variable pTargetThreadCond.
71 extern void passCapability(Condition *pTargetThreadCond);
73 // Signals that the next time a capability becomes free, it should
74 // be transfered to an ordinary worker thread.
76 extern void passCapabilityToWorker( void );
78 extern nat rts_n_free_capabilities;
79 /* number of worker threads waiting for a return capability
81 extern nat rts_n_waiting_workers;
83 static inline rtsBool needToYieldToReturningWorker(void)
85 return rts_n_waiting_workers > 0;
88 static inline nat getFreeCapabilities (void)
90 return rts_n_free_capabilities;
93 static inline rtsBool noCapabilities (void)
95 return (rts_n_free_capabilities == 0);
98 static inline rtsBool allFreeCapabilities (void)
100 return (rts_n_free_capabilities == 1);
103 #else // !RTS_SUPPORTS_THREADS
105 // Grab a capability. (Only in the non-threaded RTS; in the threaded
106 // RTS one of the waitFor*Capability() functions must be used).
108 extern void grabCapability( Capability **pCap );
110 #endif // !RTS_SUPPORTS_THREADS
112 #endif /* __CAPABILITY_H__ */