Work stealing for sparks
[ghc-hetmet.git] / rts / Sparks.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2000-2006
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 #if defined(PARALLEL_HASKELL)
13 #error Sparks.c using new internal structure, needs major overhaul!
14 #endif
15
16 /* typedef for SparkPool in RtsTypes.h */
17
18 #if defined(THREADED_RTS)
19
20 /* INVARIANTS, in this order: bottom/top consistent, reasonable size,
21    topBound consistent, space pointer, space accessible to us */
22 #define ASSERT_SPARK_POOL_INVARIANTS(p)         \
23   ASSERT((p)->bottom >= (p)->top);              \
24   ASSERT((p)->size > 0);                        \
25   ASSERT((p)->size > (p)->bottom - (p)->top);   \
26   ASSERT((p)->topBound <= (p)->top);            \
27   ASSERT((p)->elements != NULL);                \
28   ASSERT(*((p)->elements) || 1);                \
29   ASSERT(*((p)->elements - 1  + ((p)->size)) || 1);
30
31 // missing in old interface. Currently called by initSparkPools
32 // internally.
33 SparkPool* initPool(StgWord size);
34
35 // special case: accessing our own pool, at the write end
36 // otherwise, we can always steal from our pool as the others do...
37 StgClosure* reclaimSpark(Capability *cap);
38
39 rtsBool looksEmpty(SparkPool* deque);
40
41 // rest: same as old interface
42 StgClosure * findSpark         (Capability *cap);
43 void         initSparkPools    (void);
44 void         freeSparkPool     (SparkPool *pool);
45 void         createSparkThread (Capability *cap, StgClosure *p);
46 void         pruneSparkQueues  (void);
47 void         traverseSparkQueue(evac_fn evac, void *user, Capability *cap);
48
49 INLINE_HEADER void     discardSparks  (SparkPool *pool);
50 INLINE_HEADER nat      sparkPoolSize  (SparkPool *pool);
51
52 INLINE_HEADER void     discardSparksCap  (Capability *cap);
53 INLINE_HEADER nat      sparkPoolSizeCap  (Capability *cap);
54 INLINE_HEADER rtsBool  emptySparkPoolCap (Capability *cap);
55 #endif
56
57 /* -----------------------------------------------------------------------------
58  * PRIVATE below here
59  * -------------------------------------------------------------------------- */
60
61 #if defined(PARALLEL_HASKELL) || defined(THREADED_RTS)
62
63 INLINE_HEADER rtsBool  
64 emptySparkPool (SparkPool *pool) 
65 { return looksEmpty(pool); }
66
67 INLINE_HEADER rtsBool
68 emptySparkPoolCap (Capability *cap) 
69 { return looksEmpty(cap->sparks); }
70
71 INLINE_HEADER nat
72 sparkPoolSize (SparkPool *pool) 
73 {
74   return (pool->bottom - pool->top);
75 }
76
77 INLINE_HEADER nat
78 sparkPoolSizeCap (Capability *cap) 
79 { return sparkPoolSize(cap->sparks); }
80
81 INLINE_HEADER void
82 discardSparks (SparkPool *pool)
83 {
84     pool->top = pool->bottom = 0;
85 }
86
87 INLINE_HEADER void
88 discardSparksCap (Capability *cap) 
89 { return discardSparks(cap->sparks); }
90
91 #endif
92
93 #endif /* SPARKS_H */