| 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 | #define MEMSIZE (2*32+1) //0 for "NULL"
|
|---|
| 23 | int memory[MEMSIZE];
|
|---|
| 24 | #define INDIR(cell,idx) memory[cell+idx]
|
|---|
| 25 |
|
|---|
| 26 | int next_alloc_idx = 1;
|
|---|
| 27 | int m = 0;
|
|---|
| 28 | int top;
|
|---|
| 29 |
|
|---|
| 30 | inline int index_malloc(){
|
|---|
| 31 | int curr_alloc_idx = -1;
|
|---|
| 32 |
|
|---|
| 33 | __VERIFIER_atomic_acquire(&m);
|
|---|
| 34 | if(next_alloc_idx+2-1 > MEMSIZE){
|
|---|
| 35 | __VERIFIER_atomic_release(&m);
|
|---|
| 36 | curr_alloc_idx = 0;
|
|---|
| 37 | }else{
|
|---|
| 38 | curr_alloc_idx = next_alloc_idx;
|
|---|
| 39 | next_alloc_idx += 2;
|
|---|
| 40 | __VERIFIER_atomic_release(&m);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | return curr_alloc_idx;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | inline void EBStack_init(){
|
|---|
| 47 | top = 0;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | inline int isEmpty() {
|
|---|
| 51 | if(top == 0)
|
|---|
| 52 | return 1;
|
|---|
| 53 | else
|
|---|
| 54 | return 0;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | inline int push(int d) {
|
|---|
| 58 | int oldTop = -1, newTop = -1;
|
|---|
| 59 |
|
|---|
| 60 | newTop = index_malloc();
|
|---|
| 61 | if(newTop == 0){
|
|---|
| 62 | return 0;
|
|---|
| 63 | }else{
|
|---|
| 64 | INDIR(newTop,0) = d;
|
|---|
| 65 |
|
|---|
| 66 | __VERIFIER_atomic_acquire(&m);
|
|---|
| 67 | oldTop = top;
|
|---|
| 68 | INDIR(newTop,1) = oldTop;
|
|---|
| 69 | top = newTop;
|
|---|
| 70 | __VERIFIER_atomic_release(&m);
|
|---|
| 71 | return 1;
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | inline void init(){
|
|---|
| 76 | EBStack_init();
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | void __VERIFIER_atomic_assert(int r)
|
|---|
| 80 | {
|
|---|
| 81 | assert(!r || !isEmpty());
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | inline void push_loop(){
|
|---|
| 85 | int r = -1;
|
|---|
| 86 | int arg = __VERIFIER_nondet_int();
|
|---|
| 87 | while(1){
|
|---|
| 88 | r = push(arg);
|
|---|
| 89 | __VERIFIER_atomic_assert(r);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | int m2 = 0;
|
|---|
| 94 | int state = 0;
|
|---|
| 95 | void* thr1(void* arg)
|
|---|
| 96 | {
|
|---|
| 97 | __VERIFIER_atomic_acquire(&m2);
|
|---|
| 98 | switch(state)
|
|---|
| 99 | {
|
|---|
| 100 | case 0:
|
|---|
| 101 | EBStack_init();
|
|---|
| 102 | state = 1;
|
|---|
| 103 | //fall-through
|
|---|
| 104 | case 1:
|
|---|
| 105 | __VERIFIER_atomic_release(&m2);
|
|---|
| 106 |
|
|---|
| 107 | push_loop();
|
|---|
| 108 | break;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return 0;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | int main()
|
|---|
| 115 | {
|
|---|
| 116 | pthread_t t;
|
|---|
| 117 |
|
|---|
| 118 | while(1) { pthread_create(&t, 0, thr1, 0); }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|