| 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 |
|
|---|
| 10 | void __VERIFIER_atomic_acquire(int * m)
|
|---|
| 11 | {
|
|---|
| 12 | assume(*m==0);
|
|---|
| 13 | *m = 1;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | void __VERIFIER_atomic_release(int * m)
|
|---|
| 17 | {
|
|---|
| 18 | assume(*m==1);
|
|---|
| 19 | *m = 0;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | void __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"
|
|---|
| 39 | int memory[MEMSIZE];
|
|---|
| 40 | #define INDIR(cell,idx) memory[cell+idx]
|
|---|
| 41 |
|
|---|
| 42 | int next_alloc_idx = 1;
|
|---|
| 43 | int m = 0;
|
|---|
| 44 | int top;
|
|---|
| 45 |
|
|---|
| 46 | inline 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 |
|
|---|
| 62 | inline void EBStack_init(){
|
|---|
| 63 | top = 0;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | inline int isEmpty() {
|
|---|
| 67 | if(top == 0)
|
|---|
| 68 | return 1;
|
|---|
| 69 | else
|
|---|
| 70 | return 0;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | inline 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 |
|
|---|
| 93 | void __VERIFIER_atomic_assert(int r)
|
|---|
| 94 | {
|
|---|
| 95 | assert(!r || !isEmpty());
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | inline 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 |
|
|---|
| 107 | int m2 = 0;
|
|---|
| 108 | int state = 0;
|
|---|
| 109 | void* 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 |
|
|---|
| 128 | int main()
|
|---|
| 129 | {
|
|---|
| 130 | pthread_t t;
|
|---|
| 131 |
|
|---|
| 132 | while(1) { pthread_create(&t, 0, thr1, 0); }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|