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