source: CIVL/examples/pthread/cprover/04_incdec_cas_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//http://www.ibm.com/developerworks/java/library/j-jtp04186/index.html
2//Listing 2. A nonblocking counter using CAS
3
4#include <pthread.h>
5
6#define assert(e) { if(!(e)) { ERROR: goto ERROR; (void)0; } }
7
8void __VERIFIER_atomic_CAS(
9 volatile unsigned *v,
10 unsigned e,
11 unsigned u,
12 unsigned *r,
13 volatile unsigned *flag)
14{
15 if(*v == e)
16 {
17 *flag = 1, *v = u, *r = 1;
18 }
19 else
20 {
21 *r = 0;
22 }
23}
24
25volatile unsigned value = 0;
26
27/*helpers for the property*/
28volatile unsigned inc_flag = 0;
29volatile unsigned dec_flag = 0;
30
31void __VERIFIER_atomic_assert1(unsigned inc__v)
32{
33 assert(dec_flag || value > inc__v);
34}
35
36inline unsigned inc() {
37 unsigned inc__v, inc__vn, inc__casret;
38
39 do {
40 inc__v = value;
41
42 if(inc__v == 0u-1) {
43 return 0; /*increment failed, return min*/
44 }
45
46 inc__vn = inc__v + 1;
47
48 __VERIFIER_atomic_CAS(&value,inc__v,inc__vn,&inc__casret,&inc_flag);
49 }
50 while (inc__casret==0);
51
52 __VERIFIER_atomic_assert1(inc__v);
53
54 return inc__vn;
55}
56
57void __VERIFIER_atomic_assert2(unsigned dec__v)
58{
59 assert(inc_flag || value < dec__v);
60}
61
62inline unsigned dec() {
63 unsigned dec__v, dec__vn, dec__casret;
64
65 do {
66 dec__v = value;
67
68 if(dec__v == 0) {
69 return 0u-1; /*decrement failed, return max*/
70 }
71
72 dec__vn = dec__v - 1;
73
74 __VERIFIER_atomic_CAS(&value,dec__v,dec__vn,&dec__casret,&dec_flag);
75
76 }
77 while (dec__casret==0);
78
79 __VERIFIER_atomic_assert2(dec__v);
80 return dec__vn;
81}
82
83void* thr1(void* arg){
84 int r = __VERIFIER_nondet_int();
85
86 if(r){ inc(); }
87 else{ dec(); }
88
89 return 0;
90}
91
92int main(){
93 pthread_t t;
94
95 while(1) {
96 pthread_create(&t, 0, thr1, 0);
97 }
98}
99
Note: See TracBrowser for help on using the repository browser.