edits
[cdsspec-compiler.git] / output / linuxrwlocks / testcase1.c
1 #include <stdio.h>
2 #include <threads.h>
3 #include <stdatomic.h>
4
5 #include <spec_lib.h>
6 #include <stdlib.h>
7 #include <cdsannotate.h>
8 #include <specannotation.h>
9 #include <model_memory.h>
10 #include "common.h"
11
12 #include "librace.h"
13
14 #define RW_LOCK_BIAS            0x00100000
15 #define WRITE_LOCK_CMP          RW_LOCK_BIAS
16
17 typedef union {
18         atomic_int lock;
19 } rwlock_t;
20
21
22
23
24
25
26
27 /* All other user-defined structs */
28 static bool writer_lock_acquired;
29 static int reader_lock_cnt;
30 /* All other user-defined functions */
31 /* Definition of interface info struct: Write_Trylock */
32 typedef struct Write_Trylock_info {
33 int __RET__;
34 rwlock_t * rw;
35 } Write_Trylock_info;
36 /* End of info struct definition: Write_Trylock */
37
38 /* ID function of interface: Write_Trylock */
39 inline static call_id_t Write_Trylock_id(void *info, thread_id_t __TID__) {
40         Write_Trylock_info* theInfo = (Write_Trylock_info*)info;
41         int __RET__ = theInfo->__RET__;
42         rwlock_t * rw = theInfo->rw;
43
44         call_id_t __ID__ = 0;
45         return __ID__;
46 }
47 /* End of ID function: Write_Trylock */
48
49 /* Check action function of interface: Write_Trylock */
50 inline static bool Write_Trylock_check_action(void *info, call_id_t __ID__, thread_id_t __TID__) {
51         bool check_passed;
52         Write_Trylock_info* theInfo = (Write_Trylock_info*)info;
53         int __RET__ = theInfo->__RET__;
54         rwlock_t * rw = theInfo->rw;
55
56         if ( __RET__ == 1 ) writer_lock_acquired = true ;
57         return true;
58 }
59 /* End of check action function: Write_Trylock */
60
61 /* Definition of interface info struct: Read_Trylock */
62 typedef struct Read_Trylock_info {
63 int __RET__;
64 rwlock_t * rw;
65 } Read_Trylock_info;
66 /* End of info struct definition: Read_Trylock */
67
68 /* ID function of interface: Read_Trylock */
69 inline static call_id_t Read_Trylock_id(void *info, thread_id_t __TID__) {
70         Read_Trylock_info* theInfo = (Read_Trylock_info*)info;
71         int __RET__ = theInfo->__RET__;
72         rwlock_t * rw = theInfo->rw;
73
74         call_id_t __ID__ = 0;
75         return __ID__;
76 }
77 /* End of ID function: Read_Trylock */
78
79 /* Check action function of interface: Read_Trylock */
80 inline static bool Read_Trylock_check_action(void *info, call_id_t __ID__, thread_id_t __TID__) {
81         bool check_passed;
82         Read_Trylock_info* theInfo = (Read_Trylock_info*)info;
83         int __RET__ = theInfo->__RET__;
84         rwlock_t * rw = theInfo->rw;
85
86         if ( __RET__ ) reader_lock_cnt ++ ;
87         check_passed = __RET__ == ! writer_lock_acquired || ! __RET__;
88         if (!check_passed)
89                 return false;
90         return true;
91 }
92 /* End of check action function: Read_Trylock */
93
94 /* Definition of interface info struct: Write_Lock */
95 typedef struct Write_Lock_info {
96 rwlock_t * rw;
97 } Write_Lock_info;
98 /* End of info struct definition: Write_Lock */
99
100 /* ID function of interface: Write_Lock */
101 inline static call_id_t Write_Lock_id(void *info, thread_id_t __TID__) {
102         Write_Lock_info* theInfo = (Write_Lock_info*)info;
103         rwlock_t * rw = theInfo->rw;
104
105         call_id_t __ID__ = 0;
106         return __ID__;
107 }
108 /* End of ID function: Write_Lock */
109
110 /* Check action function of interface: Write_Lock */
111 inline static bool Write_Lock_check_action(void *info, call_id_t __ID__, thread_id_t __TID__) {
112         bool check_passed;
113         Write_Lock_info* theInfo = (Write_Lock_info*)info;
114         rwlock_t * rw = theInfo->rw;
115
116         check_passed = ! writer_lock_acquired && reader_lock_cnt == 0;
117         if (!check_passed)
118                 return false;
119         writer_lock_acquired = true ;
120         return true;
121 }
122 /* End of check action function: Write_Lock */
123
124 /* Definition of interface info struct: Write_Unlock */
125 typedef struct Write_Unlock_info {
126 rwlock_t * rw;
127 } Write_Unlock_info;
128 /* End of info struct definition: Write_Unlock */
129
130 /* ID function of interface: Write_Unlock */
131 inline static call_id_t Write_Unlock_id(void *info, thread_id_t __TID__) {
132         Write_Unlock_info* theInfo = (Write_Unlock_info*)info;
133         rwlock_t * rw = theInfo->rw;
134
135         call_id_t __ID__ = 0;
136         return __ID__;
137 }
138 /* End of ID function: Write_Unlock */
139
140 /* Check action function of interface: Write_Unlock */
141 inline static bool Write_Unlock_check_action(void *info, call_id_t __ID__, thread_id_t __TID__) {
142         bool check_passed;
143         Write_Unlock_info* theInfo = (Write_Unlock_info*)info;
144         rwlock_t * rw = theInfo->rw;
145
146         check_passed = reader_lock_cnt == 0 && writer_lock_acquired;
147         if (!check_passed)
148                 return false;
149         writer_lock_acquired = false ;
150         return true;
151 }
152 /* End of check action function: Write_Unlock */
153
154 /* Definition of interface info struct: Read_Unlock */
155 typedef struct Read_Unlock_info {
156 rwlock_t * rw;
157 } Read_Unlock_info;
158 /* End of info struct definition: Read_Unlock */
159
160 /* ID function of interface: Read_Unlock */
161 inline static call_id_t Read_Unlock_id(void *info, thread_id_t __TID__) {
162         Read_Unlock_info* theInfo = (Read_Unlock_info*)info;
163         rwlock_t * rw = theInfo->rw;
164
165         call_id_t __ID__ = 0;
166         return __ID__;
167 }
168 /* End of ID function: Read_Unlock */
169
170 /* Check action function of interface: Read_Unlock */
171 inline static bool Read_Unlock_check_action(void *info, call_id_t __ID__, thread_id_t __TID__) {
172         bool check_passed;
173         Read_Unlock_info* theInfo = (Read_Unlock_info*)info;
174         rwlock_t * rw = theInfo->rw;
175
176         check_passed = reader_lock_cnt > 0 && ! writer_lock_acquired;
177         if (!check_passed)
178                 return false;
179         reader_lock_cnt -- ;
180         return true;
181 }
182 /* End of check action function: Read_Unlock */
183
184 /* Definition of interface info struct: Read_Lock */
185 typedef struct Read_Lock_info {
186 rwlock_t * rw;
187 } Read_Lock_info;
188 /* End of info struct definition: Read_Lock */
189
190 /* ID function of interface: Read_Lock */
191 inline static call_id_t Read_Lock_id(void *info, thread_id_t __TID__) {
192         Read_Lock_info* theInfo = (Read_Lock_info*)info;
193         rwlock_t * rw = theInfo->rw;
194
195         call_id_t __ID__ = 0;
196         return __ID__;
197 }
198 /* End of ID function: Read_Lock */
199
200 /* Check action function of interface: Read_Lock */
201 inline static bool Read_Lock_check_action(void *info, call_id_t __ID__, thread_id_t __TID__) {
202         bool check_passed;
203         Read_Lock_info* theInfo = (Read_Lock_info*)info;
204         rwlock_t * rw = theInfo->rw;
205
206         check_passed = ! writer_lock_acquired;
207         if (!check_passed)
208                 return false;
209         reader_lock_cnt ++ ;
210         return true;
211 }
212 /* End of check action function: Read_Lock */
213
214 #define INTERFACE_SIZE 6
215 static void** func_ptr_table;
216 static hb_rule** hb_rule_table;
217 static commutativity_rule** commutativity_rule_table;
218 inline static bool CommutativityCondition0(void *info1, void *info2) {
219         Read_Lock_info *_info1 = (Read_Lock_info*) info1;
220         Read_Lock_info *_info2 = (Read_Lock_info*) info2;
221         return true;
222 }
223 inline static bool CommutativityCondition1(void *info1, void *info2) {
224         Read_Lock_info *_info1 = (Read_Lock_info*) info1;
225         Read_Unlock_info *_info2 = (Read_Unlock_info*) info2;
226         return true;
227 }
228 inline static bool CommutativityCondition2(void *info1, void *info2) {
229         Read_Lock_info *_info1 = (Read_Lock_info*) info1;
230         Read_Trylock_info *_info2 = (Read_Trylock_info*) info2;
231         return true;
232 }
233 inline static bool CommutativityCondition3(void *info1, void *info2) {
234         Read_Lock_info *_info1 = (Read_Lock_info*) info1;
235         Write_Trylock_info *_info2 = (Write_Trylock_info*) info2;
236         return ! _info2-> __RET__;
237 }
238 inline static bool CommutativityCondition4(void *info1, void *info2) {
239         Read_Unlock_info *_info1 = (Read_Unlock_info*) info1;
240         Read_Unlock_info *_info2 = (Read_Unlock_info*) info2;
241         return true;
242 }
243 inline static bool CommutativityCondition5(void *info1, void *info2) {
244         Read_Unlock_info *_info1 = (Read_Unlock_info*) info1;
245         Read_Trylock_info *_info2 = (Read_Trylock_info*) info2;
246         return true;
247 }
248 inline static bool CommutativityCondition6(void *info1, void *info2) {
249         Read_Unlock_info *_info1 = (Read_Unlock_info*) info1;
250         Write_Trylock_info *_info2 = (Write_Trylock_info*) info2;
251         return ! _info2-> __RET__;
252 }
253 inline static bool CommutativityCondition7(void *info1, void *info2) {
254         Read_Trylock_info *_info1 = (Read_Trylock_info*) info1;
255         Read_Trylock_info *_info2 = (Read_Trylock_info*) info2;
256         return true;
257 }
258 inline static bool CommutativityCondition8(void *info1, void *info2) {
259         Read_Trylock_info *_info1 = (Read_Trylock_info*) info1;
260         Write_Trylock_info *_info2 = (Write_Trylock_info*) info2;
261         return ! _info1-> __RET__ || ! _info2-> __RET__;
262 }
263 inline static bool CommutativityCondition9(void *info1, void *info2) {
264         Read_Trylock_info *_info1 = (Read_Trylock_info*) info1;
265         Write_Lock_info *_info2 = (Write_Lock_info*) info2;
266         return ! _info1-> __RET__;
267 }
268 inline static bool CommutativityCondition10(void *info1, void *info2) {
269         Read_Trylock_info *_info1 = (Read_Trylock_info*) info1;
270         Write_Unlock_info *_info2 = (Write_Unlock_info*) info2;
271         return ! _info1-> __RET__;
272 }
273 inline static bool CommutativityCondition11(void *info1, void *info2) {
274         Write_Trylock_info *_info1 = (Write_Trylock_info*) info1;
275         Write_Trylock_info *_info2 = (Write_Trylock_info*) info2;
276         return ! _info1-> __RET__ || ! _info2-> __RET__;
277 }
278 inline static bool CommutativityCondition12(void *info1, void *info2) {
279         Write_Trylock_info *_info1 = (Write_Trylock_info*) info1;
280         Write_Unlock_info *_info2 = (Write_Unlock_info*) info2;
281         return ! _info1-> __RET__;
282 }
283 inline static bool CommutativityCondition13(void *info1, void *info2) {
284         Write_Trylock_info *_info1 = (Write_Trylock_info*) info1;
285         Write_Lock_info *_info2 = (Write_Lock_info*) info2;
286         return ! _info1-> __RET__;
287 }
288
289 /* Initialization of sequential varialbes */
290 static void __SPEC_INIT__() {
291         writer_lock_acquired = false ;
292         reader_lock_cnt = 0 ;
293 }
294
295 /* Cleanup routine of sequential variables */
296 static bool __SPEC_CLEANUP__() {
297         return true;
298 }
299
300 /* Define function for sequential code initialization */
301 inline static void __sequential_init() {
302         /* Init func_ptr_table */
303         func_ptr_table = (void**) malloc(sizeof(void*) * 6 * 2);
304         func_ptr_table[2 * 3] = (void*) &Write_Trylock_id;
305         func_ptr_table[2 * 3 + 1] = (void*) &Write_Trylock_check_action;
306         func_ptr_table[2 * 2] = (void*) &Read_Trylock_id;
307         func_ptr_table[2 * 2 + 1] = (void*) &Read_Trylock_check_action;
308         func_ptr_table[2 * 1] = (void*) &Write_Lock_id;
309         func_ptr_table[2 * 1 + 1] = (void*) &Write_Lock_check_action;
310         func_ptr_table[2 * 5] = (void*) &Write_Unlock_id;
311         func_ptr_table[2 * 5 + 1] = (void*) &Write_Unlock_check_action;
312         func_ptr_table[2 * 4] = (void*) &Read_Unlock_id;
313         func_ptr_table[2 * 4 + 1] = (void*) &Read_Unlock_check_action;
314         func_ptr_table[2 * 0] = (void*) &Read_Lock_id;
315         func_ptr_table[2 * 0 + 1] = (void*) &Read_Lock_check_action;
316         /* Read_Unlock(true) -> Write_Lock(true) */
317         struct hb_rule *hbConditionInit0 = (struct hb_rule*) malloc(sizeof(struct hb_rule));
318         hbConditionInit0->interface_num_before = 4; // Read_Unlock
319         hbConditionInit0->hb_condition_num_before = 0; // 
320         hbConditionInit0->interface_num_after = 1; // Write_Lock
321         hbConditionInit0->hb_condition_num_after = 0; // 
322         /* Read_Unlock(true) -> Write_Trylock(HB_Write_Trylock_Succ) */
323         struct hb_rule *hbConditionInit1 = (struct hb_rule*) malloc(sizeof(struct hb_rule));
324         hbConditionInit1->interface_num_before = 4; // Read_Unlock
325         hbConditionInit1->hb_condition_num_before = 0; // 
326         hbConditionInit1->interface_num_after = 3; // Write_Trylock
327         hbConditionInit1->hb_condition_num_after = 1; // HB_Write_Trylock_Succ
328         /* Write_Unlock(true) -> Write_Lock(true) */
329         struct hb_rule *hbConditionInit2 = (struct hb_rule*) malloc(sizeof(struct hb_rule));
330         hbConditionInit2->interface_num_before = 5; // Write_Unlock
331         hbConditionInit2->hb_condition_num_before = 0; // 
332         hbConditionInit2->interface_num_after = 1; // Write_Lock
333         hbConditionInit2->hb_condition_num_after = 0; // 
334         /* Write_Unlock(true) -> Write_Trylock(HB_Write_Trylock_Succ) */
335         struct hb_rule *hbConditionInit3 = (struct hb_rule*) malloc(sizeof(struct hb_rule));
336         hbConditionInit3->interface_num_before = 5; // Write_Unlock
337         hbConditionInit3->hb_condition_num_before = 0; // 
338         hbConditionInit3->interface_num_after = 3; // Write_Trylock
339         hbConditionInit3->hb_condition_num_after = 1; // HB_Write_Trylock_Succ
340         /* Write_Unlock(true) -> Read_Lock(true) */
341         struct hb_rule *hbConditionInit4 = (struct hb_rule*) malloc(sizeof(struct hb_rule));
342         hbConditionInit4->interface_num_before = 5; // Write_Unlock
343         hbConditionInit4->hb_condition_num_before = 0; // 
344         hbConditionInit4->interface_num_after = 0; // Read_Lock
345         hbConditionInit4->hb_condition_num_after = 0; // 
346         /* Write_Unlock(true) -> Read_Trylock(HB_Read_Trylock_Succ) */
347         struct hb_rule *hbConditionInit5 = (struct hb_rule*) malloc(sizeof(struct hb_rule));
348         hbConditionInit5->interface_num_before = 5; // Write_Unlock
349         hbConditionInit5->hb_condition_num_before = 0; // 
350         hbConditionInit5->interface_num_after = 2; // Read_Trylock
351         hbConditionInit5->hb_condition_num_after = 2; // HB_Read_Trylock_Succ
352         /* Init hb_rule_table */
353         hb_rule_table = (hb_rule**) malloc(sizeof(hb_rule*) * 6);
354         #define HB_RULE_TABLE_SIZE 6
355         hb_rule_table[0] = hbConditionInit0;
356         hb_rule_table[1] = hbConditionInit1;
357         hb_rule_table[2] = hbConditionInit2;
358         hb_rule_table[3] = hbConditionInit3;
359         hb_rule_table[4] = hbConditionInit4;
360         hb_rule_table[5] = hbConditionInit5;
361         /* Init commutativity_rule_table */
362         commutativity_rule_table = (commutativity_rule**) malloc(sizeof(commutativity_rule*) * 14);
363         commutativity_rule* rule;
364         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
365         rule->interface_num_before = 0;
366         rule->interface_num_after = 0;
367         rule->rule = "true";
368         rule->condition = CommutativityCondition0;
369         commutativity_rule_table[0] = rule;
370         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
371         rule->interface_num_before = 0;
372         rule->interface_num_after = 4;
373         rule->rule = "true";
374         rule->condition = CommutativityCondition1;
375         commutativity_rule_table[1] = rule;
376         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
377         rule->interface_num_before = 0;
378         rule->interface_num_after = 2;
379         rule->rule = "true";
380         rule->condition = CommutativityCondition2;
381         commutativity_rule_table[2] = rule;
382         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
383         rule->interface_num_before = 0;
384         rule->interface_num_after = 3;
385         rule->rule = "! _Method2 . __RET__";
386         rule->condition = CommutativityCondition3;
387         commutativity_rule_table[3] = rule;
388         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
389         rule->interface_num_before = 4;
390         rule->interface_num_after = 4;
391         rule->rule = "true";
392         rule->condition = CommutativityCondition4;
393         commutativity_rule_table[4] = rule;
394         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
395         rule->interface_num_before = 4;
396         rule->interface_num_after = 2;
397         rule->rule = "true";
398         rule->condition = CommutativityCondition5;
399         commutativity_rule_table[5] = rule;
400         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
401         rule->interface_num_before = 4;
402         rule->interface_num_after = 3;
403         rule->rule = "! _Method2 . __RET__";
404         rule->condition = CommutativityCondition6;
405         commutativity_rule_table[6] = rule;
406         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
407         rule->interface_num_before = 2;
408         rule->interface_num_after = 2;
409         rule->rule = "true";
410         rule->condition = CommutativityCondition7;
411         commutativity_rule_table[7] = rule;
412         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
413         rule->interface_num_before = 2;
414         rule->interface_num_after = 3;
415         rule->rule = "! _Method1 . __RET__ || ! _Method2 . __RET__";
416         rule->condition = CommutativityCondition8;
417         commutativity_rule_table[8] = rule;
418         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
419         rule->interface_num_before = 2;
420         rule->interface_num_after = 1;
421         rule->rule = "! _Method1 . __RET__";
422         rule->condition = CommutativityCondition9;
423         commutativity_rule_table[9] = rule;
424         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
425         rule->interface_num_before = 2;
426         rule->interface_num_after = 5;
427         rule->rule = "! _Method1 . __RET__";
428         rule->condition = CommutativityCondition10;
429         commutativity_rule_table[10] = rule;
430         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
431         rule->interface_num_before = 3;
432         rule->interface_num_after = 3;
433         rule->rule = "! _Method1 . __RET__ || ! _Method2 . __RET__";
434         rule->condition = CommutativityCondition11;
435         commutativity_rule_table[11] = rule;
436         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
437         rule->interface_num_before = 3;
438         rule->interface_num_after = 5;
439         rule->rule = "! _Method1 . __RET__";
440         rule->condition = CommutativityCondition12;
441         commutativity_rule_table[12] = rule;
442         rule = (commutativity_rule*) malloc (sizeof(commutativity_rule));
443         rule->interface_num_before = 3;
444         rule->interface_num_after = 1;
445         rule->rule = "! _Method1 . __RET__";
446         rule->condition = CommutativityCondition13;
447         commutativity_rule_table[13] = rule;
448         /* Pass init info, including function table info & HB rules & Commutativity Rules */
449         struct anno_init *anno_init = (struct anno_init*) malloc(sizeof(struct anno_init));
450         anno_init->init_func = (void_func_t) __SPEC_INIT__;
451         anno_init->cleanup_func = (cleanup_func_t) __SPEC_CLEANUP__;
452         anno_init->func_table = func_ptr_table;
453         anno_init->func_table_size = INTERFACE_SIZE;
454         anno_init->hb_rule_table = hb_rule_table;
455         anno_init->hb_rule_table_size = HB_RULE_TABLE_SIZE;
456         anno_init->commutativity_rule_table = commutativity_rule_table;
457         anno_init->commutativity_rule_table_size = 14;
458         struct spec_annotation *init = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
459         init->type = INIT;
460         init->annotation = anno_init;
461         cdsannotate(SPEC_ANALYSIS, init);
462
463 }
464
465 /* End of Global construct generation in class */
466
467
468 static inline int read_can_lock(rwlock_t *lock)
469 {
470         return atomic_load_explicit(&lock->lock, memory_order_relaxed) > 0;
471 }
472
473 static inline int write_can_lock(rwlock_t *lock)
474 {
475         return atomic_load_explicit(&lock->lock, memory_order_relaxed) == RW_LOCK_BIAS;
476 }
477
478
479 void __wrapper__read_lock(rwlock_t * rw);
480
481 void read_lock(rwlock_t * rw) {
482         /* Interface begins */
483         struct anno_interface_begin *interface_begin = (struct anno_interface_begin*) malloc(sizeof(struct anno_interface_begin));
484         interface_begin->interface_num = 0; // Read_Lock
485                 interface_begin->interface_name = "Read_Lock";
486         struct spec_annotation *annotation_interface_begin = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
487         annotation_interface_begin->type = INTERFACE_BEGIN;
488         annotation_interface_begin->annotation = interface_begin;
489         cdsannotate(SPEC_ANALYSIS, annotation_interface_begin);
490         __wrapper__read_lock(rw);
491         struct anno_hb_condition *hb_condition = (struct anno_hb_condition*) malloc(sizeof(struct anno_hb_condition));
492         hb_condition->interface_num = 0; // Read_Lock
493         hb_condition->hb_condition_num = 0;
494         struct spec_annotation *annotation_hb_condition = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
495         annotation_hb_condition->type = HB_CONDITION;
496         annotation_hb_condition->annotation = hb_condition;
497         cdsannotate(SPEC_ANALYSIS, annotation_hb_condition);
498
499         Read_Lock_info* info = (Read_Lock_info*) malloc(sizeof(Read_Lock_info));
500         info->rw = rw;
501         struct anno_interface_end *interface_end = (struct anno_interface_end*) malloc(sizeof(struct anno_interface_end));
502         interface_end->interface_num = 0; // Read_Lock
503         interface_end->info = info;
504         struct spec_annotation *annoation_interface_end = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
505         annoation_interface_end->type = INTERFACE_END;
506         annoation_interface_end->annotation = interface_end;
507         cdsannotate(SPEC_ANALYSIS, annoation_interface_end);
508 }
509
510 void __wrapper__read_lock(rwlock_t * rw)
511 {
512         int priorvalue = atomic_fetch_sub_explicit(&rw->lock, 1, memory_order_acquire);
513         /* Automatically generated code for commit point define check: Read_Lock_Success_1 */
514
515         if (priorvalue > 0) {
516                 struct anno_cp_define_check *cp_define_check = (struct anno_cp_define_check*) malloc(sizeof(struct anno_cp_define_check));
517                 cp_define_check->label_num = 0;
518                 cp_define_check->label_name = "Read_Lock_Success_1";
519                 cp_define_check->interface_num = 0;
520                 struct spec_annotation *annotation_cp_define_check = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
521                 annotation_cp_define_check->type = CP_DEFINE_CHECK;
522                 annotation_cp_define_check->annotation = cp_define_check;
523                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define_check);
524         }
525         
526         while (priorvalue <= 0) {
527                 atomic_fetch_add_explicit(&rw->lock, 1, memory_order_relaxed);
528                 while (atomic_load_explicit(&rw->lock, memory_order_relaxed) <= 0) {
529                         thrd_yield();
530                 }
531                 priorvalue = atomic_fetch_sub_explicit(&rw->lock, 1, memory_order_acquire);
532         /* Automatically generated code for commit point define check: Read_Lock_Success_2 */
533
534         if (priorvalue > 0) {
535                 struct anno_cp_define_check *cp_define_check = (struct anno_cp_define_check*) malloc(sizeof(struct anno_cp_define_check));
536                 cp_define_check->label_num = 1;
537                 cp_define_check->label_name = "Read_Lock_Success_2";
538                 cp_define_check->interface_num = 0;
539                 struct spec_annotation *annotation_cp_define_check = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
540                 annotation_cp_define_check->type = CP_DEFINE_CHECK;
541                 annotation_cp_define_check->annotation = cp_define_check;
542                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define_check);
543         }
544                 
545         }
546 }
547
548
549 void __wrapper__write_lock(rwlock_t * rw);
550
551 void write_lock(rwlock_t * rw) {
552         /* Interface begins */
553         struct anno_interface_begin *interface_begin = (struct anno_interface_begin*) malloc(sizeof(struct anno_interface_begin));
554         interface_begin->interface_num = 1; // Write_Lock
555                 interface_begin->interface_name = "Write_Lock";
556         struct spec_annotation *annotation_interface_begin = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
557         annotation_interface_begin->type = INTERFACE_BEGIN;
558         annotation_interface_begin->annotation = interface_begin;
559         cdsannotate(SPEC_ANALYSIS, annotation_interface_begin);
560         __wrapper__write_lock(rw);
561         struct anno_hb_condition *hb_condition = (struct anno_hb_condition*) malloc(sizeof(struct anno_hb_condition));
562         hb_condition->interface_num = 1; // Write_Lock
563         hb_condition->hb_condition_num = 0;
564         struct spec_annotation *annotation_hb_condition = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
565         annotation_hb_condition->type = HB_CONDITION;
566         annotation_hb_condition->annotation = hb_condition;
567         cdsannotate(SPEC_ANALYSIS, annotation_hb_condition);
568
569         Write_Lock_info* info = (Write_Lock_info*) malloc(sizeof(Write_Lock_info));
570         info->rw = rw;
571         struct anno_interface_end *interface_end = (struct anno_interface_end*) malloc(sizeof(struct anno_interface_end));
572         interface_end->interface_num = 1; // Write_Lock
573         interface_end->info = info;
574         struct spec_annotation *annoation_interface_end = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
575         annoation_interface_end->type = INTERFACE_END;
576         annoation_interface_end->annotation = interface_end;
577         cdsannotate(SPEC_ANALYSIS, annoation_interface_end);
578 }
579
580 void __wrapper__write_lock(rwlock_t * rw)
581 {
582         int priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_acquire);
583         /* Automatically generated code for commit point define check: Write_Lock_Success_1 */
584
585         if (priorvalue == RW_LOCK_BIAS) {
586                 struct anno_cp_define_check *cp_define_check = (struct anno_cp_define_check*) malloc(sizeof(struct anno_cp_define_check));
587                 cp_define_check->label_num = 2;
588                 cp_define_check->label_name = "Write_Lock_Success_1";
589                 cp_define_check->interface_num = 1;
590                 struct spec_annotation *annotation_cp_define_check = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
591                 annotation_cp_define_check->type = CP_DEFINE_CHECK;
592                 annotation_cp_define_check->annotation = cp_define_check;
593                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define_check);
594         }
595         
596         while (priorvalue != RW_LOCK_BIAS) {
597                 atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_relaxed);
598                 while (atomic_load_explicit(&rw->lock, memory_order_relaxed) != RW_LOCK_BIAS) {
599                         thrd_yield();
600                 }
601                 priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_acquire);
602         /* Automatically generated code for commit point define check: Write_Lock_Success_2 */
603
604         if (priorvalue == RW_LOCK_BIAS) {
605                 struct anno_cp_define_check *cp_define_check = (struct anno_cp_define_check*) malloc(sizeof(struct anno_cp_define_check));
606                 cp_define_check->label_num = 3;
607                 cp_define_check->label_name = "Write_Lock_Success_2";
608                 cp_define_check->interface_num = 1;
609                 struct spec_annotation *annotation_cp_define_check = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
610                 annotation_cp_define_check->type = CP_DEFINE_CHECK;
611                 annotation_cp_define_check->annotation = cp_define_check;
612                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define_check);
613         }
614                 
615         }
616 }
617
618 int __wrapper__read_trylock(rwlock_t * rw);
619
620 int read_trylock(rwlock_t * rw) {
621         /* Interface begins */
622         struct anno_interface_begin *interface_begin = (struct anno_interface_begin*) malloc(sizeof(struct anno_interface_begin));
623         interface_begin->interface_num = 2; // Read_Trylock
624                 interface_begin->interface_name = "Read_Trylock";
625         struct spec_annotation *annotation_interface_begin = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
626         annotation_interface_begin->type = INTERFACE_BEGIN;
627         annotation_interface_begin->annotation = interface_begin;
628         cdsannotate(SPEC_ANALYSIS, annotation_interface_begin);
629         int __RET__ = __wrapper__read_trylock(rw);
630         if (__RET__ == 1) {
631                 struct anno_hb_condition *hb_condition = (struct anno_hb_condition*) malloc(sizeof(struct anno_hb_condition));
632                 hb_condition->interface_num = 2; // Read_Trylock
633                 hb_condition->hb_condition_num = 2;
634                 struct spec_annotation *annotation_hb_condition = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
635                 annotation_hb_condition->type = HB_CONDITION;
636                 annotation_hb_condition->annotation = hb_condition;
637                 cdsannotate(SPEC_ANALYSIS, annotation_hb_condition);
638         }
639
640         Read_Trylock_info* info = (Read_Trylock_info*) malloc(sizeof(Read_Trylock_info));
641         info->__RET__ = __RET__;
642         info->rw = rw;
643         struct anno_interface_end *interface_end = (struct anno_interface_end*) malloc(sizeof(struct anno_interface_end));
644         interface_end->interface_num = 2; // Read_Trylock
645         interface_end->info = info;
646         struct spec_annotation *annoation_interface_end = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
647         annoation_interface_end->type = INTERFACE_END;
648         annoation_interface_end->annotation = interface_end;
649         cdsannotate(SPEC_ANALYSIS, annoation_interface_end);
650         return __RET__;
651 }
652
653 int __wrapper__read_trylock(rwlock_t * rw)
654 {
655         int priorvalue = atomic_fetch_sub_explicit(&rw->lock, 1, memory_order_acquire);
656         /* Automatically generated code for potential commit point: Potential_Read_Trylock_Point */
657
658         if (true) {
659                 struct anno_potential_cp_define *potential_cp_define = (struct anno_potential_cp_define*) malloc(sizeof(struct anno_potential_cp_define));
660                 potential_cp_define->label_num = 4;
661                 potential_cp_define->label_name = "Potential_Read_Trylock_Point";
662                 struct spec_annotation *annotation_potential_cp_define = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
663                 annotation_potential_cp_define->type = POTENTIAL_CP_DEFINE;
664                 annotation_potential_cp_define->annotation = potential_cp_define;
665                 cdsannotate(SPEC_ANALYSIS, annotation_potential_cp_define);
666         }
667         
668         if (priorvalue > 0) {
669         /* Automatically generated code for commit point define: Read_Trylock_Succ_Point */
670
671         if (true) {
672                 struct anno_cp_define *cp_define = (struct anno_cp_define*) malloc(sizeof(struct anno_cp_define));
673                 cp_define->label_num = 5;
674                 cp_define->label_name = "Read_Trylock_Succ_Point";
675                 cp_define->potential_cp_label_num = 4;
676                 cp_define->potential_label_name = "Potential_Read_Trylock_Point";
677                 cp_define->interface_num = 2;
678                 struct spec_annotation *annotation_cp_define = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
679                 annotation_cp_define->type = CP_DEFINE;
680                 annotation_cp_define->annotation = cp_define;
681                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define);
682         }
683                 
684                 return 1;
685         }
686         /* Automatically generated code for commit point define: Read_Trylock_Fail_Point */
687
688         if (true) {
689                 struct anno_cp_define *cp_define = (struct anno_cp_define*) malloc(sizeof(struct anno_cp_define));
690                 cp_define->label_num = 6;
691                 cp_define->label_name = "Read_Trylock_Fail_Point";
692                 cp_define->potential_cp_label_num = 4;
693                 cp_define->potential_label_name = "Potential_Read_Trylock_Point";
694                 cp_define->interface_num = 2;
695                 struct spec_annotation *annotation_cp_define = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
696                 annotation_cp_define->type = CP_DEFINE;
697                 annotation_cp_define->annotation = cp_define;
698                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define);
699         }
700         
701         atomic_fetch_add_explicit(&rw->lock, 1, memory_order_relaxed);
702         return 0;
703 }
704
705 int __wrapper__write_trylock(rwlock_t * rw);
706
707 int write_trylock(rwlock_t * rw) {
708         /* Interface begins */
709         struct anno_interface_begin *interface_begin = (struct anno_interface_begin*) malloc(sizeof(struct anno_interface_begin));
710         interface_begin->interface_num = 3; // Write_Trylock
711                 interface_begin->interface_name = "Write_Trylock";
712         struct spec_annotation *annotation_interface_begin = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
713         annotation_interface_begin->type = INTERFACE_BEGIN;
714         annotation_interface_begin->annotation = interface_begin;
715         cdsannotate(SPEC_ANALYSIS, annotation_interface_begin);
716         int __RET__ = __wrapper__write_trylock(rw);
717         if (__RET__ == 1) {
718                 struct anno_hb_condition *hb_condition = (struct anno_hb_condition*) malloc(sizeof(struct anno_hb_condition));
719                 hb_condition->interface_num = 3; // Write_Trylock
720                 hb_condition->hb_condition_num = 1;
721                 struct spec_annotation *annotation_hb_condition = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
722                 annotation_hb_condition->type = HB_CONDITION;
723                 annotation_hb_condition->annotation = hb_condition;
724                 cdsannotate(SPEC_ANALYSIS, annotation_hb_condition);
725         }
726
727         Write_Trylock_info* info = (Write_Trylock_info*) malloc(sizeof(Write_Trylock_info));
728         info->__RET__ = __RET__;
729         info->rw = rw;
730         struct anno_interface_end *interface_end = (struct anno_interface_end*) malloc(sizeof(struct anno_interface_end));
731         interface_end->interface_num = 3; // Write_Trylock
732         interface_end->info = info;
733         struct spec_annotation *annoation_interface_end = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
734         annoation_interface_end->type = INTERFACE_END;
735         annoation_interface_end->annotation = interface_end;
736         cdsannotate(SPEC_ANALYSIS, annoation_interface_end);
737         return __RET__;
738 }
739
740 int __wrapper__write_trylock(rwlock_t * rw)
741 {
742         int priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_acquire);
743         /* Automatically generated code for potential commit point: Potential_Write_Trylock_Point */
744
745         if (true) {
746                 struct anno_potential_cp_define *potential_cp_define = (struct anno_potential_cp_define*) malloc(sizeof(struct anno_potential_cp_define));
747                 potential_cp_define->label_num = 7;
748                 potential_cp_define->label_name = "Potential_Write_Trylock_Point";
749                 struct spec_annotation *annotation_potential_cp_define = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
750                 annotation_potential_cp_define->type = POTENTIAL_CP_DEFINE;
751                 annotation_potential_cp_define->annotation = potential_cp_define;
752                 cdsannotate(SPEC_ANALYSIS, annotation_potential_cp_define);
753         }
754                 
755         if (priorvalue == RW_LOCK_BIAS) {
756         /* Automatically generated code for commit point define: Write_Trylock_Succ_Point */
757
758         if (true) {
759                 struct anno_cp_define *cp_define = (struct anno_cp_define*) malloc(sizeof(struct anno_cp_define));
760                 cp_define->label_num = 8;
761                 cp_define->label_name = "Write_Trylock_Succ_Point";
762                 cp_define->potential_cp_label_num = 7;
763                 cp_define->potential_label_name = "Potential_Write_Trylock_Point";
764                 cp_define->interface_num = 3;
765                 struct spec_annotation *annotation_cp_define = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
766                 annotation_cp_define->type = CP_DEFINE;
767                 annotation_cp_define->annotation = cp_define;
768                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define);
769         }
770                 
771                 return 1;
772         }
773         /* Automatically generated code for commit point define: Write_Trylock_Fail_Point */
774
775         if (true) {
776                 struct anno_cp_define *cp_define = (struct anno_cp_define*) malloc(sizeof(struct anno_cp_define));
777                 cp_define->label_num = 9;
778                 cp_define->label_name = "Write_Trylock_Fail_Point";
779                 cp_define->potential_cp_label_num = 7;
780                 cp_define->potential_label_name = "Potential_Write_Trylock_Point";
781                 cp_define->interface_num = 3;
782                 struct spec_annotation *annotation_cp_define = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
783                 annotation_cp_define->type = CP_DEFINE;
784                 annotation_cp_define->annotation = cp_define;
785                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define);
786         }
787         
788         atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_relaxed);
789         return 0;
790 }
791
792 void __wrapper__read_unlock(rwlock_t * rw);
793
794 void read_unlock(rwlock_t * rw) {
795         /* Interface begins */
796         struct anno_interface_begin *interface_begin = (struct anno_interface_begin*) malloc(sizeof(struct anno_interface_begin));
797         interface_begin->interface_num = 4; // Read_Unlock
798                 interface_begin->interface_name = "Read_Unlock";
799         struct spec_annotation *annotation_interface_begin = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
800         annotation_interface_begin->type = INTERFACE_BEGIN;
801         annotation_interface_begin->annotation = interface_begin;
802         cdsannotate(SPEC_ANALYSIS, annotation_interface_begin);
803         __wrapper__read_unlock(rw);
804         struct anno_hb_condition *hb_condition = (struct anno_hb_condition*) malloc(sizeof(struct anno_hb_condition));
805         hb_condition->interface_num = 4; // Read_Unlock
806         hb_condition->hb_condition_num = 0;
807         struct spec_annotation *annotation_hb_condition = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
808         annotation_hb_condition->type = HB_CONDITION;
809         annotation_hb_condition->annotation = hb_condition;
810         cdsannotate(SPEC_ANALYSIS, annotation_hb_condition);
811
812         Read_Unlock_info* info = (Read_Unlock_info*) malloc(sizeof(Read_Unlock_info));
813         info->rw = rw;
814         struct anno_interface_end *interface_end = (struct anno_interface_end*) malloc(sizeof(struct anno_interface_end));
815         interface_end->interface_num = 4; // Read_Unlock
816         interface_end->info = info;
817         struct spec_annotation *annoation_interface_end = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
818         annoation_interface_end->type = INTERFACE_END;
819         annoation_interface_end->annotation = interface_end;
820         cdsannotate(SPEC_ANALYSIS, annoation_interface_end);
821 }
822
823 void __wrapper__read_unlock(rwlock_t * rw)
824 {
825         
826         atomic_fetch_add_explicit(&rw->lock, 1, memory_order_release);
827         /* Automatically generated code for commit point define check: Read_Unlock_Point */
828
829         if (true) {
830                 struct anno_cp_define_check *cp_define_check = (struct anno_cp_define_check*) malloc(sizeof(struct anno_cp_define_check));
831                 cp_define_check->label_num = 10;
832                 cp_define_check->label_name = "Read_Unlock_Point";
833                 cp_define_check->interface_num = 4;
834                 struct spec_annotation *annotation_cp_define_check = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
835                 annotation_cp_define_check->type = CP_DEFINE_CHECK;
836                 annotation_cp_define_check->annotation = cp_define_check;
837                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define_check);
838         }
839         
840 }
841
842 void __wrapper__write_unlock(rwlock_t * rw);
843
844 void write_unlock(rwlock_t * rw) {
845         /* Interface begins */
846         struct anno_interface_begin *interface_begin = (struct anno_interface_begin*) malloc(sizeof(struct anno_interface_begin));
847         interface_begin->interface_num = 5; // Write_Unlock
848                 interface_begin->interface_name = "Write_Unlock";
849         struct spec_annotation *annotation_interface_begin = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
850         annotation_interface_begin->type = INTERFACE_BEGIN;
851         annotation_interface_begin->annotation = interface_begin;
852         cdsannotate(SPEC_ANALYSIS, annotation_interface_begin);
853         __wrapper__write_unlock(rw);
854         struct anno_hb_condition *hb_condition = (struct anno_hb_condition*) malloc(sizeof(struct anno_hb_condition));
855         hb_condition->interface_num = 5; // Write_Unlock
856         hb_condition->hb_condition_num = 0;
857         struct spec_annotation *annotation_hb_condition = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
858         annotation_hb_condition->type = HB_CONDITION;
859         annotation_hb_condition->annotation = hb_condition;
860         cdsannotate(SPEC_ANALYSIS, annotation_hb_condition);
861
862         Write_Unlock_info* info = (Write_Unlock_info*) malloc(sizeof(Write_Unlock_info));
863         info->rw = rw;
864         struct anno_interface_end *interface_end = (struct anno_interface_end*) malloc(sizeof(struct anno_interface_end));
865         interface_end->interface_num = 5; // Write_Unlock
866         interface_end->info = info;
867         struct spec_annotation *annoation_interface_end = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
868         annoation_interface_end->type = INTERFACE_END;
869         annoation_interface_end->annotation = interface_end;
870         cdsannotate(SPEC_ANALYSIS, annoation_interface_end);
871 }
872
873 void __wrapper__write_unlock(rwlock_t * rw)
874 {
875         
876         atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_release);
877         /* Automatically generated code for commit point define check: Write_Unlock_Point */
878
879         if (true) {
880                 struct anno_cp_define_check *cp_define_check = (struct anno_cp_define_check*) malloc(sizeof(struct anno_cp_define_check));
881                 cp_define_check->label_num = 11;
882                 cp_define_check->label_name = "Write_Unlock_Point";
883                 cp_define_check->interface_num = 5;
884                 struct spec_annotation *annotation_cp_define_check = (struct spec_annotation*) malloc(sizeof(struct spec_annotation));
885                 annotation_cp_define_check->type = CP_DEFINE_CHECK;
886                 annotation_cp_define_check->annotation = cp_define_check;
887                 cdsannotate(SPEC_ANALYSIS, annotation_cp_define_check);
888         }
889         
890
891         
892 }
893
894 rwlock_t mylock;
895 int shareddata;
896
897 static void a(void *obj)
898 {
899         write_lock(&mylock);
900                 shareddata = 47;
901         write_unlock(&mylock);
902 }
903
904 static void b(void *obj)
905 {
906         if (read_trylock(&mylock)) {
907                                                 read_unlock(&mylock);
908         }
909 }
910
911 int user_main(int argc, char **argv)
912 {
913         __sequential_init();
914         
915         thrd_t t1, t2;
916         atomic_init(&mylock.lock, RW_LOCK_BIAS);
917
918         thrd_create(&t1, (thrd_start_t)&a, NULL);
919         thrd_create(&t2, (thrd_start_t)&b, NULL);
920
921         thrd_join(t1);
922         thrd_join(t2);
923
924         return 0;
925 }
926