changes
[IRC.git] / Robust / src / Runtime / STM / sandbox.c
1 #include "sandbox.h"
2 #include "tm.h"
3 #include <stdio.h>
4 #include "methodheaders.h"
5 #include "runtime.h"
6 __thread int transaction_check_counter;
7 __thread jmp_buf aborttrans;
8 __thread int abortenabled;
9 __thread int * counter_reset_pointer;
10 #ifdef DELAYCOMP
11 #include "delaycomp.h"
12 #endif
13
14 void checkObjects() {
15   if (abortenabled&&checktrans()) {
16     printf("Loop Abort\n");
17     transaction_check_counter=(*counter_reset_pointer=HIGH_CHECK_FREQUENCY);
18 #ifdef TRANSSTATS
19     numTransAbort++;
20 #endif
21     freenewobjs();
22     objstrReset();
23     t_chashreset();
24 #ifdef READSET
25     rd_t_chashreset();
26 #endif
27 #ifdef DELAYCOMP
28     dc_t_chashreset();
29     ptrstack.count=0;
30     primstack.count=0;
31     branchstack.count=0;
32 #ifdef STMARRAY
33     arraystack.count=0;
34 #endif
35 #endif
36     _longjmp(aborttrans, 1);
37   }
38   transaction_check_counter=*counter_reset_pointer;
39 }
40
41 #ifdef D___System______Assert____Z
42 CALL11(___System______Assert____Z, int ___status___, int ___status___) {
43   if (!___status___) {
44     if (abortenabled&&checktrans()) {
45 #ifdef TRANSSTATS
46       numTransAbort++;
47 #endif
48       freenewobjs();
49       objstrReset();
50       t_chashreset();
51 #ifdef READSET
52       rd_t_chashreset();
53 #endif
54 #ifdef DELAYCOMP
55       dc_t_chashreset();
56       ptrstack.count=0;
57       primstack.count=0;
58       branchstack.count=0;
59 #ifdef STMARRAY
60       arraystack.count=0;
61 #endif
62 #endif
63       _longjmp(aborttrans, 1);
64     }
65     printf("Assertion violation\n");
66     *((int *)(NULL)); //force stack trace error                         
67   }
68 }
69 #endif
70
71 /* Do sandboxing */
72 void errorhandler(int sig, struct sigcontext ctx) {
73   //  printf("Error\n");
74   if (abortenabled&&checktrans()) {
75     sigset_t toclear;
76     sigemptyset(&toclear);
77     sigaddset(&toclear, sig);
78     sigprocmask(SIG_UNBLOCK, &toclear,NULL); 
79 #ifdef TRANSSTATS
80     numTransAbort++;
81 #endif
82     freenewobjs();
83     objstrReset();
84     t_chashreset();
85 #ifdef READSET
86     rd_t_chashreset();
87 #endif
88 #ifdef DELAYCOMP
89     dc_t_chashreset();
90     ptrstack.count=0;
91     primstack.count=0;
92     branchstack.count=0;
93 #ifdef STMARRAY
94     arraystack.count=0;
95 #endif
96 #endif
97     _longjmp(aborttrans, 1);
98   }
99   threadhandler(sig, ctx);
100 }
101
102 int checktrans() {
103   /* Create info to keep track of objects that can be locked */
104   chashlistnode_t *curr = c_list;
105
106   /* Inner loop to traverse the linked list of the cache lookupTable */
107   while(likely(curr != NULL)) {
108     //if the first bin in hash table is empty
109     objheader_t * headeraddr=&((objheader_t *) curr->val)[-1];
110     objheader_t *header=(objheader_t *)(((char *)curr->key)-sizeof(objheader_t));
111     unsigned int version = headeraddr->version;
112     
113     if (header->lock==0) {
114       return 1;
115     }
116     CFENCE;
117     if (version!=header->version) {
118       return 1;
119     }
120     curr = curr->lnext;
121   }
122   return 0;
123 }