[project @ 2005-05-10 13:25:41 by simonmar]
[ghc-hetmet.git] / ghc / rts / Capability.h
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2001-2003
4  *
5  * Capabilities
6  *
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 
12  * register (BaseReg).
13  *
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.
17  *
18  * This header file contains the functions for working with capabilities.
19  * (the main, and only, consumer of this interface is the scheduler).
20  * 
21  * --------------------------------------------------------------------------*/
22
23 #ifndef __CAPABILITY_H__
24 #define __CAPABILITY_H__
25
26 // All the capabilities
27 extern Capability *capabilities;
28
29 // Initialised the available capabilities.
30 //
31 extern void initCapabilities( void );
32
33 // Releases a capability
34 //
35 extern void releaseCapability( Capability* cap );
36
37 // Signal that a thread has become runnable
38 //
39 extern void threadRunnable ( void );
40
41 // Return the capability that I own.
42 // 
43 extern Capability *myCapability (void);
44
45 extern void prodWorker ( void );
46
47 #ifdef RTS_SUPPORTS_THREADS
48 // Gives up the current capability IFF there is a higher-priority
49 // thread waiting for it.  This happens in one of two ways:
50 //
51 //   (a) we are passing the capability to another OS thread, so
52 //       that it can run a bound Haskell thread, or
53 //
54 //   (b) there is an OS thread waiting to return from a foreign call
55 //
56 // On return: *pCap is NULL if the capability was released.  The
57 // current worker thread should then re-acquire it using
58 // waitForCapability().
59 //
60 extern void yieldCapability( Capability **pCap );
61
62 // Acquires a capability for doing some work.
63 //
64 // If the current OS thread is bound to a particular Haskell thread,
65 // then pThreadCond points to a condition variable for waking up this
66 // OS thread when its Haskell thread is ready to run.
67 //
68 // On return: pCap points to the capability.
69 extern void waitForCapability( Mutex* pMutex, Capability** pCap, 
70                                Condition *pThreadCond );
71
72 // Acquires a capability at a return point.  
73 //
74 // OS threads waiting in this function get priority over those waiting
75 // in waitForWorkCapability().
76 //
77 // On return: pCap points to the capability.
78 extern void waitForReturnCapability(Mutex* pMutex, Capability** pCap);
79
80 // Signals that the next time a capability becomes free, it should
81 // be transfered to a particular OS thread, identified by the
82 // condition variable pTargetThreadCond.
83 //
84 extern void passCapability(Condition *pTargetThreadCond);
85
86 // Signals that the next time a capability becomes free, it should
87 // be transfered to an ordinary worker thread.
88 //
89 extern void passCapabilityToWorker( void );
90
91 extern nat rts_n_free_capabilities;  
92
93 extern Capability *free_capabilities;
94
95 /* number of worker threads waiting for a return capability
96  */
97 extern nat rts_n_waiting_workers;
98
99 static inline rtsBool needToYieldToReturningWorker(void)
100 {
101         return rts_n_waiting_workers > 0;
102 }
103
104 static inline nat getFreeCapabilities (void)
105 {
106   return rts_n_free_capabilities;
107 }
108
109 static inline rtsBool noCapabilities (void)
110 {
111   return (rts_n_free_capabilities == 0);
112 }
113
114 static inline rtsBool allFreeCapabilities (void)
115 {
116 #if defined(SMP)
117   return (rts_n_free_capabilities == RTS_DEREF(RtsFlags).ParFlags.nNodes);
118 #else
119   return (rts_n_free_capabilities == 1);
120 #endif
121 }
122
123 #else // !RTS_SUPPORTS_THREADS
124
125 // Grab a capability.  (Only in the non-threaded RTS; in the threaded
126 // RTS one of the waitFor*Capability() functions must be used).
127 //
128 extern void grabCapability( Capability **pCap );
129
130 #endif /* !RTS_SUPPORTS_THREADS */
131
132 #endif /* __CAPABILITY_H__ */