[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gum / Sparks.lc
1 /****************************************************************
2 *                                                               *
3 *       Spark Management Routines (Sparks.lc)                   *
4 *                                                               *
5 *  Contains the spark-management routines used by GUM           *
6 *  (c) The Parade/AQUA Projects, Glasgow University, 1995       *
7 *      Kevin Hammond, 27 February 1995                          *
8 *                                                               *
9 *****************************************************************/
10
11
12 \begin{code}
13 #if defined(PAR) || defined(GRAN) /* whole file */
14
15 #include "rtsdefs.h"
16 \end{code}
17
18 This uses GranSim-style sparkqs rather than old-style sparks as used
19 in the threaded world.  The problem with the latter is that they
20 contain insufficient information (we also need to know whether a spark
21 is local/global etc.).  Problem: at the moment GUMM uses threaded-style
22 sparks (presumably).  ToDo: Fix this...
23
24 \begin{code}
25 P_ 
26 FindLocalSpark(forexport)
27 rtsBool forexport;
28 {
29 #ifdef PAR
30     P_ spark;
31
32     while (PendingSparksHd[REQUIRED_POOL] < PendingSparksTl[REQUIRED_POOL]) {
33         spark = *PendingSparksHd[REQUIRED_POOL]++;
34         if (SHOULD_SPARK(spark))
35             return spark;
36     }
37     while (PendingSparksHd[ADVISORY_POOL] < PendingSparksTl[ADVISORY_POOL]) {
38         spark = *PendingSparksHd[ADVISORY_POOL]++;
39         if (SHOULD_SPARK(spark))
40             return spark;
41     }
42     return NULL;
43
44 #else
45
46     fprintf(stderr,"FindLocalSpark: under GRAN!\n");
47     abort();
48
49 # if 0
50     sparkq spark, prev, next, thespark;
51
52     int pool, poolcount;
53
54     thespark = NULL;
55
56     for (poolcount = 0, pool = REQUIRED_POOL;
57       thespark == NULL && poolcount < 2;
58       ++poolcount, pool = ADVISORY_POOL) {
59         for (prev = NULL, spark = PendingSparksHd[pool];
60           spark != NULL && thespark == NULL; spark = next) {
61             next = SPARK_NEXT(spark);
62
63             if (SHOULD_SPARK(SPARK_NODE(spark))) {
64                 /* Don't Steal local sparks */
65                 if (forexport && !SPARK_GLOBAL(spark)) {
66                     prev = spark;
67                     continue;
68                 }
69                 SPARK_NEXT(spark) = NULL;
70                 thespark = spark;
71             } else {
72                 DisposeSpark(spark);
73             }
74
75             if (spark == PendingSparksHd[pool])
76                 PendingSparksHd[pool] = next;
77
78             if (prev != NULL)
79                 SPARK_NEXT(prev) = next;
80         }
81
82         if (PendingSparksHd[pool] == NULL)
83             PendingSparksTl[pool] = NULL;
84     }
85     return (thespark == NULL ? NULL : thespark);
86 # endif /* 0 */
87
88 #endif
89 }
90
91 #ifdef PAR
92 void
93 DisposeSpark(spark)
94 P_ spark;
95 {
96     /* Do nothing */
97 }
98
99 #else
100 # ifndef GRAN
101 void
102 DisposeSpark(spark)
103 sparkq spark;
104 {
105   if(spark!=NULL)
106     free(spark);
107 }
108 # endif
109 #endif
110
111 rtsBool
112 Spark(closure, required)
113 P_ closure;
114 rtsBool required;
115 {
116 #ifdef PAR
117     I_ pool = required ? REQUIRED_POOL : ADVISORY_POOL;
118
119     if (SHOULD_SPARK(closure) && PendingSparksTl[pool] < PendingSparksLim[pool]) {
120         *PendingSparksTl[pool]++ = closure;
121     }
122 #endif
123     return rtsTrue;
124 }
125
126 #endif /* PAR or GRAN -- whole file */
127 \end{code}