Use a struct for the set of spark counters
[ghc.git] / rts / Sparks.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2000-2009
4 *
5 * Sparking support for GRAN, PAR and THREADED_RTS versions of the RTS.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #ifndef SPARKS_H
10 #define SPARKS_H
11
12 #include "WSDeque.h"
13
14 #include "BeginPrivate.h"
15
16 /* typedef for SparkPool in RtsTypes.h */
17
18 /* Stats on spark creation/conversion */
19 typedef struct {
20 StgWord created;
21 StgWord dud;
22 StgWord converted;
23 StgWord gcd;
24 StgWord fizzled;
25 } SparkCounters;
26
27 #if defined(THREADED_RTS)
28
29 typedef WSDeque SparkPool;
30
31 // Initialisation
32 void initSparkPools (void);
33
34 // Take a spark from the "write" end of the pool. Can be called
35 // by the pool owner only.
36 INLINE_HEADER StgClosure* reclaimSpark(SparkPool *pool);
37
38 // Returns True if the spark pool is empty (can give a false positive
39 // if the pool is almost empty).
40 INLINE_HEADER rtsBool looksEmpty(SparkPool* deque);
41
42 INLINE_HEADER StgClosure * tryStealSpark (SparkPool *cap);
43 INLINE_HEADER rtsBool fizzledSpark (StgClosure *);
44
45 void freeSparkPool (SparkPool *pool);
46 void createSparkThread (Capability *cap);
47 void traverseSparkQueue(evac_fn evac, void *user, Capability *cap);
48 void pruneSparkQueue (Capability *cap);
49
50 INLINE_HEADER void discardSparks (SparkPool *pool);
51 INLINE_HEADER long sparkPoolSize (SparkPool *pool);
52
53 /* -----------------------------------------------------------------------------
54 * PRIVATE below here
55 * -------------------------------------------------------------------------- */
56
57 INLINE_HEADER StgClosure* reclaimSpark(SparkPool *pool)
58 {
59 return popWSDeque(pool);
60 }
61
62 INLINE_HEADER rtsBool looksEmpty(SparkPool* deque)
63 {
64 return looksEmptyWSDeque(deque);
65 }
66
67 INLINE_HEADER long sparkPoolSize (SparkPool *pool)
68 {
69 return dequeElements(pool);
70 }
71
72 INLINE_HEADER void discardSparks (SparkPool *pool)
73 {
74 discardElements(pool);
75 }
76
77 /* ----------------------------------------------------------------------------
78 *
79 * tryStealSpark: try to steal a spark from a Capability.
80 *
81 * Returns either:
82 * (a) a useful spark;
83 * (b) a fizzled spark (use fizzledSpark to check);
84 * (c) or NULL if the pool was empty, and can occasionally return NULL
85 * if there was a race with another thread stealing from the same
86 * pool. In this case, try again later.
87 *
88 -------------------------------------------------------------------------- */
89
90 INLINE_HEADER StgClosure * tryStealSpark (SparkPool *pool)
91 {
92 return stealWSDeque_(pool);
93 // use the no-loopy version, stealWSDeque_(), since if we get a
94 // spurious NULL here the caller may want to try stealing from
95 // other pools before trying again.
96 }
97
98 INLINE_HEADER rtsBool fizzledSpark (StgClosure *spark)
99 {
100 return (GET_CLOSURE_TAG(spark) != 0 || !closure_SHOULD_SPARK(spark));
101 }
102
103 #endif // THREADED_RTS
104
105 #include "EndPrivate.h"
106
107 #endif /* SPARKS_H */