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