148f8594cae05960e5c48015d054917ad5c83e4e
[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   INTPTR* pointer;
61   int oid;
62   int isBufMode;
63 } REntry;
64
65 typedef struct MemoryQueueItem_t {
66   int type; // hashtable:0, vector:1, singleitem:2
67   int total;        //total non-retired
68   int status;       //NOTREADY, READY
69   struct MemoryQueueItem_t *next; 
70 } MemoryQueueItem;
71
72 typedef struct MemoryQueue_t {
73   MemoryQueueItem * head;
74   MemoryQueueItem * tail;  
75   REntry * binbuf[NUMBINS];
76   REntry * buf[NUMRENTRY];
77   int bufcount;
78 } MemoryQueue;
79
80 typedef struct BinItem_t {
81   int total;
82   int status;       //NOTREADY, READY
83   int type;         //READBIN:0, WRITEBIN:1
84   struct BinItem_t * next;
85 } BinItem;
86
87 typedef struct Hashtable_t {
88   MemoryQueueItem item;
89   struct BinElement_t* array[NUMBINS];
90   struct Queue*   unresolvedQueue;
91 } Hashtable;
92
93 typedef struct BinElement_t {
94   BinItem * head;
95   BinItem * tail;
96 } BinElement;
97
98 typedef struct WriteBinItem_t {
99   BinItem item;
100   REntry * val;
101 } WriteBinItem;
102
103 typedef struct ReadBinItem_t {
104   BinItem item;
105   REntry * array[NUMREAD];
106   int index;
107 } ReadBinItem;
108
109 typedef struct Vector_t {
110   MemoryQueueItem item;
111   REntry * array[NUMITEMS];
112   int index;
113 } Vector;
114
115 typedef struct SCC_t {
116   MemoryQueueItem item;
117   REntry * val;
118 } SCC;
119
120 int ADDRENTRY(MemoryQueue* q, REntry * r);
121 void RETIRERENTRY(MemoryQueue* Q, REntry * r);
122
123
124 // forward declaration of pointer type
125 typedef struct SESEcommon_t* SESEcommon_p;
126
127 // these fields are common to any SESE, and casting the
128 // generated SESE record to this can be used, because
129 // the common structure is always the first item in a
130 // customized SESE record
131 typedef struct SESEcommon_t {  
132
133   // the identifier for the class of sese's that
134   // are instances of one particular static code block
135   int classID;
136
137   // a parent waits on this semaphore when stalling on
138   // this child, the child gives it at its SESE exit
139   psemaphore stallSem;
140
141   
142   // the lock guards the following data SESE's
143   // use to coordinate with one another
144   pthread_mutex_t lock;
145
146   struct Queue*   forwardList;
147   volatile int             unresolvedDependencies;
148
149   pthread_cond_t  doneCond;
150   int             doneExecuting;
151
152   pthread_cond_t  runningChildrenCond;
153   int             numRunningChildren;
154
155   SESEcommon_p    parent;
156
157   psemaphore parentStallSem;
158   pthread_cond_t stallDone;
159
160   int numMemoryQueue;
161   int rentryIdx;
162   int unresolvedRentryIdx;
163   struct MemoryQueue_t** memoryQueueArray;
164   struct REntry_t* rentryArray[NUMRENTRY];
165   struct REntry_t* unresolvedRentryArray[NUMRENTRY];
166   int offsetsize;
167 } SESEcommon;
168
169 // a thread-local stack of SESEs and function to
170 // ensure it is initialized once per thread
171 /*
172 extern __thread struct Queue* seseCallStack;
173 extern __thread pthread_once_t mlpOnceObj;
174 void mlpInitOncePerThread();
175 */
176 extern __thread SESEcommon_p seseCaller;
177
178
179 // simple mechanical allocation and 
180 // deallocation of SESE records
181 void* mlpCreateSESErecord( int size );
182 void  mlpDestroySESErecord( void* seseRecord );
183 void* mlpAllocSESErecord( int size );
184
185 MemoryQueue** mlpCreateMemoryQueueArray(int numMemoryQueue);
186 REntry* mlpCreateFineREntry(int type, void* seseToIssue, void* dynID);
187 REntry* mlpCreateREntry(int type, void* seseToIssue);
188 MemoryQueue* createMemoryQueue();
189 void rehashMemoryQueue(SESEcommon_p seseParent);
190
191 #endif /* __MLP_RUNTIME__ */