source: CIVL/examples/pthread/cprover/03_incdec_true.c@ 41340c1

1.23 2.0 main test-branch
Last change on this file since 41340c1 was e3151da, checked in by Ziqing Luo <ziqing@…>, 11 years ago

re-organized example directory

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1763 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.4 KB
Line 
1//http://www.ibm.com/developerworks/java/library/j-jtp04186/index.html
2//Listing 2. A counter using locks
3
4#include <pthread.h>
5
6#define assume(e) __VERIFIER_assume(e)
7#define assert(e) { if(!(e)) { ERROR: goto ERROR; (void)0; } }
8
9#define atomic_assert(e) {__VERIFIER_atomic_acquire();assert(e);__VERIFIER_atomic_release();}
10
11volatile unsigned value = 0, m = 0;
12
13void __VERIFIER_atomic_acquire()
14{
15 assume(m==0);
16 m = 1;
17}
18
19void __VERIFIER_atomic_release()
20{
21 assume(m==1);
22 m = 0;
23}
24
25/*helpers for the property*/
26volatile unsigned inc_flag = 0;
27volatile unsigned dec_flag = 0;
28
29inline unsigned inc() {
30 unsigned inc_v = 0;
31
32 __VERIFIER_atomic_acquire();
33 if(value == 0u-1) {
34 __VERIFIER_atomic_release();
35 return 0;
36 }else{
37 inc_v = value;
38 inc_flag = 1, value = inc_v + 1; /*set flag, then update*/
39 __VERIFIER_atomic_release();
40
41 atomic_assert(dec_flag || value > inc_v);
42
43 return inc_v + 1;
44 }
45}
46
47inline unsigned dec() {
48 unsigned dec_v;
49
50 __VERIFIER_atomic_acquire();
51 if(value == 0) {
52 __VERIFIER_atomic_release();
53
54 return 0u-1; /*decrement failed, return max*/
55 }else{
56 dec_v = value;
57 dec_flag = 1, value = dec_v - 1; /*set flag, then update*/
58 __VERIFIER_atomic_release();
59
60 atomic_assert(inc_flag || value < dec_v);
61
62 return dec_v - 1;
63 }
64}
65
66void* thr1(void* arg){
67 int r = __VERIFIER_nondet_int();
68
69 if(r){ inc(); }
70 else{ dec(); }
71
72 return 0;
73}
74
75int main(){
76 pthread_t t;
77
78 while(1) { pthread_create(&t, 0, thr1, 0); }
79}
80
Note: See TracBrowser for help on using the repository browser.