source: CIVL/examples/pthread/cprover/03_incdec_true.c@ 1aaefd4

main test-branch
Last change on this file since 1aaefd4 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

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