| 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 overflowed; |
|---|
| 23 | StgWord converted; |
|---|
| 24 | StgWord gcd; |
|---|
| 25 | StgWord fizzled; |
|---|
| 26 | } SparkCounters; |
|---|
| 27 | |
|---|
| 28 | #if defined(THREADED_RTS) |
|---|
| 29 | |
|---|
| 30 | typedef WSDeque SparkPool; |
|---|
| 31 | |
|---|
| 32 | // Initialisation |
|---|
| 33 | SparkPool *allocSparkPool (void); |
|---|
| 34 | |
|---|
| 35 | // Take a spark from the "write" end of the pool. Can be called |
|---|
| 36 | // by the pool owner only. |
|---|
| 37 | INLINE_HEADER StgClosure* reclaimSpark(SparkPool *pool); |
|---|
| 38 | |
|---|
| 39 | // Returns True if the spark pool is empty (can give a false positive |
|---|
| 40 | // if the pool is almost empty). |
|---|
| 41 | INLINE_HEADER rtsBool looksEmpty(SparkPool* deque); |
|---|
| 42 | |
|---|
| 43 | INLINE_HEADER StgClosure * tryStealSpark (SparkPool *pool); |
|---|
| 44 | INLINE_HEADER rtsBool fizzledSpark (StgClosure *); |
|---|
| 45 | |
|---|
| 46 | void freeSparkPool (SparkPool *pool); |
|---|
| 47 | void createSparkThread (Capability *cap); |
|---|
| 48 | void traverseSparkQueue(evac_fn evac, void *user, Capability *cap); |
|---|
| 49 | void pruneSparkQueue (Capability *cap); |
|---|
| 50 | |
|---|
| 51 | INLINE_HEADER void discardSparks (SparkPool *pool); |
|---|
| 52 | INLINE_HEADER long sparkPoolSize (SparkPool *pool); |
|---|
| 53 | |
|---|
| 54 | /* ----------------------------------------------------------------------------- |
|---|
| 55 | * PRIVATE below here |
|---|
| 56 | * -------------------------------------------------------------------------- */ |
|---|
| 57 | |
|---|
| 58 | INLINE_HEADER StgClosure* reclaimSpark(SparkPool *pool) |
|---|
| 59 | { |
|---|
| 60 | return popWSDeque(pool); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | INLINE_HEADER rtsBool looksEmpty(SparkPool* deque) |
|---|
| 64 | { |
|---|
| 65 | return looksEmptyWSDeque(deque); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | INLINE_HEADER long sparkPoolSize (SparkPool *pool) |
|---|
| 69 | { |
|---|
| 70 | return dequeElements(pool); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | INLINE_HEADER void discardSparks (SparkPool *pool) |
|---|
| 74 | { |
|---|
| 75 | discardElements(pool); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /* ---------------------------------------------------------------------------- |
|---|
| 79 | * |
|---|
| 80 | * tryStealSpark: try to steal a spark from a Capability. |
|---|
| 81 | * |
|---|
| 82 | * Returns either: |
|---|
| 83 | * (a) a useful spark; |
|---|
| 84 | * (b) a fizzled spark (use fizzledSpark to check); |
|---|
| 85 | * (c) or NULL if the pool was empty, and can occasionally return NULL |
|---|
| 86 | * if there was a race with another thread stealing from the same |
|---|
| 87 | * pool. In this case, try again later. |
|---|
| 88 | * |
|---|
| 89 | -------------------------------------------------------------------------- */ |
|---|
| 90 | |
|---|
| 91 | INLINE_HEADER StgClosure * tryStealSpark (SparkPool *pool) |
|---|
| 92 | { |
|---|
| 93 | return stealWSDeque_(pool); |
|---|
| 94 | // use the no-loopy version, stealWSDeque_(), since if we get a |
|---|
| 95 | // spurious NULL here the caller may want to try stealing from |
|---|
| 96 | // other pools before trying again. |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | INLINE_HEADER rtsBool fizzledSpark (StgClosure *spark) |
|---|
| 100 | { |
|---|
| 101 | return (GET_CLOSURE_TAG(spark) != 0 || !closure_SHOULD_SPARK(spark)); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | #endif // THREADED_RTS |
|---|
| 105 | |
|---|
| 106 | #include "EndPrivate.h" |
|---|
| 107 | |
|---|
| 108 | #endif /* SPARKS_H */ |
|---|