changes.
[IRC.git] / Robust / src / Runtime / mlp_runtime.h
1 #ifndef __MLP_RUNTIME__
2 #define __MLP_RUNTIME__
3
4
5 #include <pthread.h>
6 #include "Queue.h"
7 #include "psemaphore.h"
8 #include "mlp_lock.h"
9
10 #ifndef FALSE
11 #define FALSE 0
12 #endif
13
14 #ifndef TRUE
15 #define TRUE 1
16 #endif
17
18 #define NUMBINS 64
19 #define NUMREAD 64
20 #define NUMITEMS 64
21 #define NUMRENTRY 256
22
23 #define READY 1
24 #define NOTREADY 0
25
26 #define READ 0
27 #define WRITE 1
28 #define PARENTREAD 2
29 #define PARENTWRITE 3
30 #define COARSE 4
31 #define PARENTCOARSE 5
32 #define SCCITEM 6
33
34 #define HASHTABLE 0
35 #define VECTOR 1
36 #define SINGLEITEM 2
37
38 #define READBIN 0
39 #define WRITEBIN 1
40
41 #define H_MASK (NUMBINS<<4)-1
42
43 #ifndef FALSE
44 #define FALSE 0
45 #endif
46
47 #ifndef TRUE
48 #define TRUE 1
49 #endif
50
51 typedef struct REntry_t{
52   int type; // fine read:0, fine write:1, parent read:2, parent write:3 coarse: 4, parent coarse:5, scc: 6
53   struct Hashtable_t* hashtable;
54   struct BinItem_t* binitem;
55   struct Vector_t* vector;
56   struct SCC_t* scc;
57   struct MemoryQueue_t* queue;
58   psemaphore parentStallSem;
59   void* seseRec;
60   void* dynID;
61 } REntry;
62
63 typedef struct MemoryQueueItem_t {
64   int type; // hashtable:0, vector:1, singleitem:2
65   int total;        //total non-retired
66   int status;       //NOTREADY, READY
67   struct MemoryQueueItem_t *next;
68 } MemoryQueueItem;
69
70 typedef struct MemoryQueue_t {
71   MemoryQueueItem * head;
72   MemoryQueueItem * tail;  
73 } MemoryQueue;
74
75 typedef struct BinItem_t {
76   int total;
77   int status;       //NOTREADY, READY
78   int type;         //READBIN:0, WRITEBIN:1
79   struct BinItem_t * next;
80 } BinItem;
81
82 typedef struct Hashtable_t {
83   MemoryQueueItem item;
84   struct BinElement_t* array[NUMBINS];
85 } Hashtable;
86
87 typedef struct BinElement_t {
88   BinItem * head;
89   BinItem * tail;
90 } BinElement;
91
92 typedef struct WriteBinItem_t {
93   BinItem item;
94   REntry * val;
95 } WriteBinItem;
96
97 typedef struct ReadBinItem_t {
98   BinItem item;
99   REntry * array[NUMREAD];
100   int index;
101 } ReadBinItem;
102
103 typedef struct Vector_t {
104   MemoryQueueItem item;
105   REntry * array[NUMITEMS];
106   int index;
107 } Vector;
108
109 typedef struct SCC_t {
110   MemoryQueueItem item;
111   REntry * val;
112 } SCC;
113
114 int ADDRENTRY(MemoryQueue* q, REntry * r);
115 void RETIRERENTRY(MemoryQueue* Q, REntry * r);
116
117
118 // forward declaration of pointer type
119 typedef struct SESEcommon_t* SESEcommon_p;
120
121 // these fields are common to any SESE, and casting the
122 // generated SESE record to this can be used, because
123 // the common structure is always the first item in a
124 // customized SESE record
125 typedef struct SESEcommon_t {  
126
127   // the identifier for the class of sese's that
128   // are instances of one particular static code block
129   int classID;
130
131   // a parent waits on this semaphore when stalling on
132   // this child, the child gives it at its SESE exit
133   psemaphore stallSem;
134
135   
136   // the lock guards the following data SESE's
137   // use to coordinate with one another
138   pthread_mutex_t lock;
139
140   struct Queue*   forwardList;
141   volatile int             unresolvedDependencies;
142
143   pthread_cond_t  doneCond;
144   int             doneExecuting;
145
146   pthread_cond_t  runningChildrenCond;
147   int             numRunningChildren;
148
149   SESEcommon_p    parent;
150
151   psemaphore parentStallSem;
152   pthread_cond_t stallDone;
153
154   int numMemoryQueue;
155   int rentryIdx;
156   struct MemoryQueue_t** memoryQueueArray;
157   struct REntry_t* rentryArray[NUMRENTRY];
158
159 } SESEcommon;
160
161 // a thread-local stack of SESEs and function to
162 // ensure it is initialized once per thread
163 /*
164 extern __thread struct Queue* seseCallStack;
165 extern __thread pthread_once_t mlpOnceObj;
166 void mlpInitOncePerThread();
167 */
168 extern __thread SESEcommon_p seseCaller;
169
170
171 // simple mechanical allocation and 
172 // deallocation of SESE records
173 void* mlpCreateSESErecord( int size );
174 void  mlpDestroySESErecord( void* seseRecord );
175 void* mlpAllocSESErecord( int size );
176
177 MemoryQueue** mlpCreateMemoryQueueArray(int numMemoryQueue);
178 REntry* mlpCreateFineREntry(int type, void* seseToIssue, void* dynID);
179 REntry* mlpCreateREntry(int type, void* seseToIssue);
180 MemoryQueue* createMemoryQueue();
181
182 #endif /* __MLP_RUNTIME__ */