Bug fixes
[IRC.git] / Robust / src / Runtime / DSTM / interface / dstm.h
1 #ifndef _DSTM_H_
2 #define _DSTM_H_
3
4 #ifdef MAC
5 #define MSG_NOSIGNAL 0
6 #endif
7
8 #define GET_NTUPLES(x)  ((int *)(x + sizeof(prefetchqelem_t)))
9 #define GET_PTR_OID(x)  ((unsigned int *)(x + sizeof(prefetchqelem_t) + sizeof(int)))
10 #define GET_PTR_EOFF(x,n) ((short *)(x + sizeof(prefetchqelem_t) + sizeof(int) + (n*sizeof(unsigned int))))
11 #define GET_PTR_ARRYFLD(x,n) ((short *)(x + sizeof(prefetchqelem_t) + sizeof(int) + (n*sizeof(unsigned int)) + (n*sizeof(short))))
12
13
14 //Coordinator Messages
15 #define READ_REQUEST            1
16 #define READ_MULT_REQUEST       2
17 #define MOVE_REQUEST            3
18 #define MOVE_MULT_REQUEST       4
19 #define TRANS_REQUEST           5
20 #define TRANS_ABORT             6
21 #define TRANS_COMMIT            7
22 #define TRANS_PREFETCH          8
23 #define TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING    9
24
25 //Participant Messages
26 #define OBJECT_FOUND                    10
27 #define OBJECT_NOT_FOUND                11
28 #define OBJECTS_FOUND                   12
29 #define OBJECTS_NOT_FOUND               13
30 #define TRANS_AGREE                     17
31 #define TRANS_DISAGREE                  18
32 #define TRANS_AGREE_BUT_MISSING_OBJECTS 19
33 #define TRANS_SOFT_ABORT                20
34 #define TRANS_SUCESSFUL                 21
35 #define TRANS_PREFETCH_RESPONSE         22
36 #define START_REMOTE_THREAD             23
37 #define THREAD_NOTIFY_REQUEST           24
38 #define THREAD_NOTIFY_RESPONSE          25
39 #define TRANS_UNSUCESSFUL               26
40
41 //Control bits for status of objects in Machine pile
42 #define OBJ_LOCKED_BUT_VERSION_MATCH    14
43 #define OBJ_UNLOCK_BUT_VERSION_MATCH    15
44 #define VERSION_NO_MATCH                16
45
46 //Max number of objects 
47 #define MAX_OBJECTS  20
48
49
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <pthread.h>
54 #include "clookup.h"
55 #include "queue.h"
56 #include "mcpileq.h"
57 #include "threadnotify.h"
58
59
60 #define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
61 #define TID_LEN 20
62 //bit designations for status field of objheader
63 #define DIRTY 0x01
64 #define NEW   0x02
65 #define LOCK  0x04
66 #define LOCAL  0x08
67
68 #ifdef COMPILER
69
70 #include "structdefs.h"
71
72 typedef struct objheader {
73         threadlist_t *notifylist;
74         unsigned short version;
75         unsigned short rcount;
76 } objheader_t;
77
78 #define OID(x)\
79     (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
80
81 #define COMPOID(x)\
82     (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___))
83
84 #define STATUS(x)\
85          *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
86
87 #define TYPE(x)\
88         ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
89
90 #define GETSIZE(size, x) {\
91   int type=TYPE(x);\
92   if (type<NUMCLASSES) {\
93     size=classsize[type];\
94   } else {\
95     size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject);\
96   }\
97 }
98
99 #else
100
101 typedef struct objheader {
102         threadlist_t *notifylist;
103         unsigned int oid;
104         unsigned short type;
105         unsigned short version;
106         unsigned short rcount;
107         char status;
108 } objheader_t;
109
110 #define OID(x) x->oid
111 #define TYPE(x) x->type
112 #define STATUS(x) x->status
113 #define GETSIZE(size, x) size=classsize[TYPE(x)]
114 #endif
115
116
117 typedef struct objstr {
118         unsigned int size; //this many bytes are allocated after this header
119         void *top;
120         struct objstr *next;
121 } objstr_t;
122
123 typedef struct transrecord {
124   objstr_t *cache;
125   chashtable_t *lookupTable;
126 #ifdef COMPILER
127   struct ___Object___ * revertlist;
128 #endif
129 } transrecord_t;
130 // Structure is a shared structure that keeps track of responses from the participants
131 typedef struct thread_response {
132   char rcv_status;
133 } thread_response_t;
134
135 // Structure that holds  fixed data to be sent along with TRANS_REQUEST
136 typedef struct fixed_data {
137   char control;                 /* control message */
138   char trans_id[TID_LEN];       /* transaction id */
139   int mcount;                   /* participant count */
140   short numread;                /* no of objects read */
141   short nummod;                 /* no of objects modified */
142   short numcreated;             /* no of objects created */
143   int sum_bytes;                /* total bytes of modified objects in a transaction */
144 } fixed_data_t;
145
146 /* Structure that holds trans request information for each participant */
147 typedef struct trans_req_data {
148   fixed_data_t f;               /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
149   unsigned int *listmid;        /* Pointer to array holding list of participants */
150   char *objread;                /* Pointer to array holding oid and version number of objects that are only read */ 
151   unsigned int *oidmod;         /* Pointer to array holding oids of objects that are modified */
152   unsigned int *oidcreated;     /* Pointer to array holding oids of objects that are newly created */
153 } trans_req_data_t;             
154
155 /* Structure that holds information of objects that are not found in the participant
156  * and objs locked within a transaction during commit process */
157 typedef struct trans_commit_data{
158   unsigned int *objlocked;      /* Pointer to array holding oids of objects locked inside a transaction */
159   unsigned int *objnotfound;    /* Pointer to array holding oids of objects not found on the participant machine */
160   void *modptr;                 /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
161   int numlocked;                /* no of objects locked */
162   int numnotfound;              /* no of objects not found */
163 } trans_commit_data_t;
164
165
166 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
167 /* Structure for passing multiple arguments to a thread
168  * spawned to process each transaction on a machine */
169 typedef struct thread_data_array {
170   int thread_id;        
171   int mid;    
172   trans_req_data_t *buffer;     /* Holds trans request information sent to participants */  
173   thread_response_t *recvmsg;   /* Shared datastructure to keep track of the participants response to a trans request */
174   pthread_cond_t *threshold;    /* Condition var to waking up a thread */
175   pthread_mutex_t *lock;        /* Lock for counting participants response */
176   int *count;                   /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
177   char *replyctrl;              /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
178   char *replyretry;             /* Shared variable that keep track if coordinator needs retry */
179   transrecord_t *rec;           /* To send modified objects */
180 } thread_data_array_t;
181
182
183 //Structure for passing arguments to the local m/c thread
184 typedef struct local_thread_data_array {
185         thread_data_array_t *tdata;     /* Holds all the arguments send to a thread that is spawned when transaction commits */ 
186         trans_commit_data_t *transinfo; /* Holds information of objects locked and not found in the participant */ 
187 } local_thread_data_array_t;
188
189 /* Initialize main object store and lookup tables, start server thread. */
190 int dstmInit(void);
191
192 /* Prototypes for object header */
193 unsigned int getNewOID(void);
194 /* end object header */
195
196 /* Prototypes for object store */
197 objstr_t *objstrCreate(unsigned int size); //size in bytes
198 void objstrDelete(objstr_t *store); //traverse and free entire list
199 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
200 /* end object store */
201
202 /* Prototypes for server portion */
203 void *dstmListen();
204 void *dstmAccept(void *);
205 int readClientReq(trans_commit_data_t *, int);
206 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
207 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
208 char decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
209 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
210 void processReqNotify(unsigned int numoid, unsigned int *oid, unsigned short *version, unsigned int mid, unsigned int threadid);
211 /* end server portion */
212
213 /* Prototypes for transactions */
214 /* Function called at beginning. Passes in the first parameter. */
215 /* Returns 1 if this thread should run the main process */
216
217 int dstmStartup(const char *);
218 void transInit();
219 int processConfigFile();
220 void addHost(unsigned int);
221 void mapObjMethod(unsigned short);
222
223 void randomdelay();
224 transrecord_t *transStart();
225 objheader_t *transRead(transrecord_t *, unsigned int);
226 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid
227 int transCommit(transrecord_t *record); //return 0 if successful
228 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
229 void decideResponse(thread_data_array_t *);// Coordinator decides what response to send to the participant
230 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
231 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int);
232  
233 void *handleLocalReq(void *);
234 int transComProcess(local_thread_data_array_t *);
235 int transAbortProcess(local_thread_data_array_t *);
236 void transAbort(transrecord_t *trans);
237
238 void prefetch(int, unsigned int *, unsigned short *, short*);
239 void *transPrefetch(void *);
240 void *mcqProcess(void *);
241 void checkPrefetchTuples(prefetchqelem_t *);
242 prefetchpile_t *foundLocal(prefetchqelem_t *);
243 prefetchpile_t *makePreGroups(prefetchqelem_t *, int *);
244 void checkPreCache(prefetchqelem_t *, int *, unsigned int, int);
245 int transPrefetchProcess(transrecord_t *, int **, short);
246 void sendPrefetchReq(prefetchpile_t*);
247 void getPrefetchResponse(int, int);
248 unsigned short getObjType(unsigned int oid);
249 int startRemoteThread(unsigned int oid, unsigned int mid);
250 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
251 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
252 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
253 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
254
255 /* end transactions */
256 #endif