source: CIVL/examples/pthread/cprover/26_stack_cas_true.c@ bd7a43e

main test-branch
Last change on this file since bd7a43e 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.9 KB
Line 
1//original file: EBStack.java
2//amino-cbbs\trunk\amino\java\src\main\java\org\amino\ds\lockfree
3//push only
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
10void __VERIFIER_atomic_acquire(int * m)
11{
12 assume(*m==0);
13 *m = 1;
14}
15
16void __VERIFIER_atomic_release(int * m)
17{
18 assume(*m==1);
19 *m = 0;
20}
21
22void __VERIFIER_atomic_CAS(
23 int *v,
24 int e,
25 int u,
26 int *r)
27{
28 if(*v == e)
29 {
30 *v = u, *r = 1;
31 }
32 else
33 {
34 *r = 0;
35 }
36}
37
38#define MEMSIZE (2*32+1) //0 for "NULL"
39int memory[MEMSIZE];
40#define INDIR(cell,idx) memory[cell+idx]
41
42int next_alloc_idx = 1;
43int m = 0;
44int top;
45
46inline int index_malloc(){
47 int curr_alloc_idx = -1;
48
49 __VERIFIER_atomic_acquire(&m);
50 if(next_alloc_idx+2-1 > MEMSIZE){
51 __VERIFIER_atomic_release(&m);
52 curr_alloc_idx = 0;
53 }else{
54 curr_alloc_idx = next_alloc_idx;
55 next_alloc_idx += 2;
56 __VERIFIER_atomic_release(&m);
57 }
58
59 return curr_alloc_idx;
60}
61
62inline void EBStack_init(){
63 top = 0;
64}
65
66inline int isEmpty() {
67 if(top == 0)
68 return 1;
69 else
70 return 0;
71}
72
73inline int push(int d) {
74 int oldTop = -1, newTop = -1, casret = -1;
75
76 newTop = index_malloc();
77 if(newTop == 0){
78 return 0;
79 }else{
80 INDIR(newTop,0) = d;
81 while (1) {
82 oldTop = top;
83 INDIR(newTop,1) = oldTop;
84 __VERIFIER_atomic_CAS(&top,oldTop,newTop,&casret);
85 if(casret==1){
86 return 1;
87 }
88
89 }
90 }
91}
92
93void __VERIFIER_atomic_assert(int r)
94{
95 assert(!r || !isEmpty());
96}
97
98inline void push_loop(){
99 int r = -1;
100 int arg = __VERIFIER_nondet_int();
101 while(1){
102 r = push(arg);
103 __VERIFIER_atomic_assert(r);
104 }
105}
106
107int m2 = 0;
108int state = 0;
109void* thr1(void* arg)
110{
111 __VERIFIER_atomic_acquire(&m2);
112 switch(state)
113 {
114 case 0:
115 EBStack_init();
116 state = 1;
117 //fall-through
118 case 1:
119 __VERIFIER_atomic_release(&m2);
120
121 push_loop();
122 break;
123 }
124
125 return 0;
126}
127
128int main()
129{
130 pthread_t t;
131
132 while(1) { pthread_create(&t, 0, thr1, 0); }
133}
134
Note: See TracBrowser for help on using the repository browser.