more changes...
[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
37 //Control bits for status of objects in Machine pile
38 #define OBJ_LOCKED_BUT_VERSION_MATCH    14
39 #define OBJ_UNLOCK_BUT_VERSION_MATCH    15
40 #define VERSION_NO_MATCH                16
41
42 //Max number of objects 
43 #define MAX_OBJECTS  20
44
45
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <pthread.h>
50 #include "clookup.h"
51 #include "queue.h"
52 #include "mcpileq.h"
53
54 #define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
55 #define TID_LEN 20
56 //bit designations for status field of objheader
57 #define DIRTY 0x01
58 #define NEW   0x02
59 #define LOCK  0x04
60 #define LOCAL  0x08
61
62 #ifdef COMPILER
63
64 #include "structdefs.h"
65
66 typedef struct objheader {
67         unsigned short version;
68         unsigned short rcount;
69 } objheader_t;
70
71 #define OID(x)\
72         ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___
73
74 #define STATUS(x)\
75         ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___
76
77 #define TYPE(x)\
78         ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
79
80 #else
81
82 typedef struct objheader {
83         unsigned int oid;
84         unsigned short type;
85         unsigned short version;
86         unsigned short rcount;
87         char status;
88 } objheader_t;
89
90 #define OID(x) x->oid
91 #define TYPE(x) x->type
92 #define STATUS(x) x->status
93
94 #endif
95
96
97 typedef struct objstr {
98         unsigned int size; //this many bytes are allocated after this header
99         void *top;
100         struct objstr *next;
101 } objstr_t;
102
103 typedef struct transrecord {
104   objstr_t *cache;
105   chashtable_t *lookupTable;
106 #ifdef COMPILER
107   struct ___Object___ * revertlist;
108 #endif
109 } transrecord_t;
110 // Structure that keeps track of responses from the participants
111 typedef struct thread_response {
112   char rcv_status;
113 } thread_response_t;
114
115 // Structure that holds  fixed data sizes to be sent along with TRANS_REQUEST
116 typedef struct fixed_data {
117   char control;
118   char trans_id[TID_LEN];       
119   int mcount;           // Machine count
120   short numread;                // Number of objects read
121   short nummod;         // Number of objects modified
122   int sum_bytes;        // Total bytes modified
123 } fixed_data_t;
124
125 // Structure that holds  variable data sizes per machine participant
126 typedef struct trans_req_data {
127   fixed_data_t f;
128   unsigned int *listmid;
129   char *objread;
130   unsigned int *oidmod;
131 } trans_req_data_t;
132
133 // Structure passed to dstmAcceptinfo() on server side to complete TRANS_COMMIT process 
134
135 typedef struct trans_commit_data{
136   unsigned int *objmod;
137   unsigned int *objlocked;
138   unsigned int *objnotfound;
139   void *modptr;
140   int nummod;
141   int numlocked;
142   int numnotfound;
143 } trans_commit_data_t;
144
145
146 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
147 //structure for passing multiple arguments to thread
148 typedef struct thread_data_array {
149   int thread_id;
150   int mid;    
151   int pilecount;
152   trans_req_data_t *buffer;
153   thread_response_t *recvmsg;//shared datastructure to keep track of the control message receiv
154   pthread_cond_t *threshold; //threshhold for waking up a thread
155   pthread_mutex_t *lock;    //lock the count variable
156   int *count;             //variable to count responses of TRANS_REQUEST protocol from all participants
157   char *replyctrl;      //shared ctrl message that stores the reply to be sent, filled by decideResp
158   char *replyretry;     //shared variable to find out if we need retry (TRANS_COMMIT case) 
159   transrecord_t *rec;   // To send modified objects
160 } thread_data_array_t;
161
162
163 //Structure for passing arguments to the local m/c thread
164 typedef struct local_thread_data_array {
165         thread_data_array_t *tdata;
166         trans_commit_data_t *transinfo; //Required for trans commit process
167 } local_thread_data_array_t;
168
169 // Structure to save information about an oid necesaary for the decideControl()
170 typedef struct objinfo {
171         unsigned int oid;
172         int poss_val; //Status of object(locked but version matches, version mismatch, oid not present in machine etc) 
173 }objinfo_t;
174
175 //Structure for members within prefetch tuples
176 typedef struct member {
177         short offset;
178         short index;
179         struct member *next;
180 }trans_member_t;
181
182 /*
183 //Structure that holds the compiler generated prefetch data
184 typedef struct compprefetchdata {
185 transrecord_t *record;
186 } compprefetchdata_t;
187 */
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 unsigned int objSize(objheader_t *object);
195 /* end object header */
196
197 /* Prototypes for object store */
198 objstr_t *objstrCreate(unsigned int size); //size in bytes
199 void objstrDelete(objstr_t *store); //traverse and free entire list
200 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
201 /* end object store */
202
203 /* Prototypes for server portion */
204 void *dstmListen();
205 void *dstmAccept(void *);
206 int readClientReq(trans_commit_data_t *, int);
207 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, int);
208 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
209 int decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, unsigned int *, int);
210 int transCommitProcess(trans_commit_data_t *, int);
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(char *);
218 void transInit();
219
220 void randomdelay(void);
221 transrecord_t *transStart();
222 objheader_t *transRead(transrecord_t *, unsigned int);
223 objheader_t *transCreateObj(transrecord_t *, unsigned short); //returns oid
224 int transCommit(transrecord_t *record); //return 0 if successful
225 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
226 void *handleLocalReq(void *);   //the C routine that the local m/c thread will execute 
227 int decideResponse(thread_data_array_t *);// Coordinator decides what response to send to the participant
228 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
229 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int);
230 int transAbortProcess(void *, unsigned int *, int, int, int);
231 int transComProcess(trans_commit_data_t *);
232 void prefetch(int, unsigned int *, short *, short*);
233 void *transPrefetch(void *);
234 void *mcqProcess(void *);
235 void checkPrefetchTuples(prefetchqelem_t *);
236 prefetchpile_t *foundLocal(prefetchqelem_t *);
237 prefetchpile_t *makePreGroups(prefetchqelem_t *, int *);
238 void checkPreCache(prefetchqelem_t *, int *, int, int, unsigned int, int, int, int);
239 int transPrefetchProcess(transrecord_t *, int **, short);
240 void sendPrefetchReq(prefetchpile_t*, int);
241 void getPrefetchResponse(int, int);
242 /* end transactions */
243 #endif