[project @ 2005-10-26 15:22:08 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 #include "RtsFlags.h"
27 #include "Task.h"
28
29 struct Capability_ {
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).
33     StgFunTable f;
34     StgRegTable r;
35
36     nat no;  // capability number.
37
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.
42     Task *running_task;
43
44     // true if this Capability is running Haskell code, used for
45     // catching unsafe call-ins.
46     rtsBool in_haskell;
47
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
51     // also lock-free.
52     StgTSO *run_queue_hd;
53     StgTSO *run_queue_tl;
54
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
60     // this list.
61     Task *suspended_ccalling_tasks;
62
63 #if defined(THREADED_RTS)
64     // Worker Tasks waiting in the wings.  Singly-linked.
65     Task *spare_workers;
66
67     // This lock protects running_task and returning_tasks_{hd,tl}.
68     Mutex lock;
69
70     // Tasks waiting to return from a foreign call, or waiting to make
71     // a new call-in using this Capability (NULL if empty).
72     // NB. this field needs to be modified by tasks other than the
73     // running_task, so it requires cap->lock to modify.  A task can
74     // check whether it is NULL without taking the lock, however.
75     Task *returning_tasks_hd; // Singly-linked, with head/tail
76     Task *returning_tasks_tl;
77 #endif
78 }; // typedef Capability, defined in RtsAPI.h
79
80 // Converts a *StgRegTable into a *Capability.
81 //
82 INLINE_HEADER Capability *
83 regTableToCapability (StgRegTable *reg)
84 {
85     return (Capability *)((void *)((unsigned char*)reg - sizeof(StgFunTable)));
86 }
87
88 // Initialise the available capabilities.
89 //
90 void initCapabilities (void);
91
92 // Release a capability.  This is called by a Task that is exiting
93 // Haskell to make a foreign call, or in various other cases when we
94 // want to relinquish a Capability that we currently hold.
95 //
96 // ASSUMES: cap->running_task is the current Task.
97 //
98 #if defined(THREADED_RTS)
99 void releaseCapability  (Capability* cap);
100 void releaseCapability_ (Capability* cap); // assumes cap->lock is held
101 #else
102 // releaseCapability() is empty in non-threaded RTS
103 INLINE_HEADER void releaseCapability  (Capability* cap STG_UNUSED) {};
104 INLINE_HEADER void releaseCapability_ (Capability* cap STG_UNUSED) {};
105 #endif
106
107 #if !IN_STG_CODE && !defined(SMP)
108 // for non-SMP, we have one global capability
109 extern Capability MainCapability; 
110 #endif
111
112 // Array of all the capabilities
113 //
114 extern nat n_capabilities;
115 extern Capability *capabilities;
116
117 // The Capability that was last free.  Used as a good guess for where
118 // to assign new threads.
119 //
120 extern Capability *last_free_capability;
121
122 // Acquires a capability at a return point.  If *cap is non-NULL, then
123 // this is taken as a preference for the Capability we wish to
124 // acquire.
125 //
126 // OS threads waiting in this function get priority over those waiting
127 // in waitForCapability().
128 //
129 // On return, *cap is non-NULL, and points to the Capability acquired.
130 //
131 void waitForReturnCapability (Capability **cap/*in/out*/, Task *task);
132
133 #if defined(THREADED_RTS)
134
135 // Gives up the current capability IFF there is a higher-priority
136 // thread waiting for it.  This happens in one of two ways:
137 //
138 //   (a) we are passing the capability to another OS thread, so
139 //       that it can run a bound Haskell thread, or
140 //
141 //   (b) there is an OS thread waiting to return from a foreign call
142 //
143 // On return: *pCap is NULL if the capability was released.  The
144 // current task should then re-acquire it using waitForCapability().
145 //
146 void yieldCapability (Capability** pCap, Task *task);
147
148 // Acquires a capability for doing some work.
149 //
150 // On return: pCap points to the capability.
151 //
152 void waitForCapability (Task *task, Mutex *mutex, Capability **pCap);
153
154 // Wakes up a worker thread on just one Capability, used when we
155 // need to service some global event.
156 //
157 void prodOneCapability (void);
158
159 // Similar to prodOneCapability(), but prods all of them.
160 //
161 void prodAllCapabilities (void);
162
163 // Waits for a capability to drain of runnable threads and workers,
164 // and then acquires it.  Used at shutdown time.
165 //
166 void shutdownCapability (Capability *cap, Task *task);
167
168 #else // !THREADED_RTS
169
170 // Grab a capability.  (Only in the non-threaded RTS; in the threaded
171 // RTS one of the waitFor*Capability() functions must be used).
172 //
173 extern void grabCapability (Capability **pCap);
174
175 #endif /* !THREADED_RTS */
176
177 #endif /* CAPABILITY_H */