| [2e2855e] | 1 | #include <civlc.cvh>
|
|---|
| 2 | #include <seq.cvh>
|
|---|
| 3 |
|
|---|
| 4 | typedef struct cqueue_t{
|
|---|
| 5 | int data[];
|
|---|
| 6 | $proc owner;
|
|---|
| 7 | } cqueue;
|
|---|
| 8 |
|
|---|
| [d66b03b] | 9 | $scope root = $here;
|
|---|
| 10 |
|
|---|
| [2e2855e] | 11 | /*@ guards \true;
|
|---|
| 12 | @ depends \noact;
|
|---|
| 13 | @ assigns \nothing;
|
|---|
| 14 | @ reads \nothing;
|
|---|
| 15 | @*/
|
|---|
| 16 | $atomic_f void create(cqueue* q)
|
|---|
| 17 | {
|
|---|
| [d66b03b] | 18 | q = (cqueue*)$malloc(root, sizeof(cqueue));
|
|---|
| [2e2855e] | 19 | q->owner=$proc_null;
|
|---|
| 20 | $seq_init(&q->data, 0, NULL);
|
|---|
| 21 | }
|
|---|
| 22 | /*@ depends \read(q->owner);
|
|---|
| 23 | @ assigns q->owner;
|
|---|
| 24 | */
|
|---|
| 25 | $atomic_f _Bool lock(cqueue* q)
|
|---|
| 26 | {
|
|---|
| 27 | if(q->owner==$self)
|
|---|
| 28 | return $true;
|
|---|
| 29 | if(q->owner == $proc_null){
|
|---|
| 30 | q->owner=$self;
|
|---|
| 31 | return $true;
|
|---|
| 32 | }else
|
|---|
| 33 | return $false;
|
|---|
| 34 | }
|
|---|
| 35 | /*@ pure;
|
|---|
| 36 | @ depends \call(enqueue, q, ...), \call(dequeue,q, ...);
|
|---|
| 37 | @ reads q->data;
|
|---|
| 38 | @ assigns \nothing;
|
|---|
| 39 | @*/
|
|---|
| 40 | $atomic_f int size(cqueue* q)
|
|---|
| 41 | {
|
|---|
| 42 | return $seq_length(&q->data);
|
|---|
| 43 | }
|
|---|
| 44 | /*@ reads q->owner;
|
|---|
| 45 | @ behavior success:
|
|---|
| 46 | @ assumes q->owner==$self;
|
|---|
| 47 | @ depends \read(q->owner) + \write(q->owner);
|
|---|
| 48 | @ assigns q->owner;
|
|---|
| 49 | @ behavior failure:
|
|---|
| 50 | @ assumes q->owner!=$self;
|
|---|
| 51 | @ depends \noact;
|
|---|
| 52 | @ assigns \nothing;
|
|---|
| 53 | @*/
|
|---|
| 54 | $atomic_f _Bool unlock(cqueue* q)
|
|---|
| 55 | {
|
|---|
| 56 | if(q->owner==$self)
|
|---|
| 57 | {
|
|---|
| 58 | q->owner=$proc_null;
|
|---|
| 59 | return $true;
|
|---|
| 60 | }
|
|---|
| 61 | return $false;
|
|---|
| 62 | }
|
|---|
| 63 | /*@ depends \call(enqueue,q, ...), \call(size, q);
|
|---|
| 64 | @ assigns q->data;
|
|---|
| 65 | @*/
|
|---|
| 66 | $atomic_f _Bool enqueue(cqueue* q, int v)
|
|---|
| 67 | {
|
|---|
| 68 | $seq_append(&q->data, &v, 1);
|
|---|
| 69 | }
|
|---|
| 70 | /*@ depends \call(dequeue, q, ...), \call(size, q);
|
|---|
| 71 | @ assigns q->data;
|
|---|
| 72 | @*/
|
|---|
| 73 | $atomic_f _Bool dequeue(cqueue* q, int* res)
|
|---|
| 74 | {
|
|---|
| 75 | $seq_remove(&q->data, $seq_length(&q->data)-1, res, 1);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | cqueue q;
|
|---|
| 79 | int a;
|
|---|
| 80 |
|
|---|
| 81 | void foo(){
|
|---|
| 82 | enqueue(&q, 1);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void goo(){
|
|---|
| 86 | dequeue(&q, &a);
|
|---|
| 87 | $assert(a==0);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | int main(){
|
|---|
| 91 |
|
|---|
| 92 | create(&q);
|
|---|
| 93 | enqueue(&q, 0);
|
|---|
| 94 |
|
|---|
| 95 | $proc p0,p1;
|
|---|
| 96 |
|
|---|
| 97 | p0=$spawn foo();
|
|---|
| 98 | p1=$spawn goo();
|
|---|
| 99 | $wait(p0);
|
|---|
| 100 | $wait(p1);
|
|---|
| 101 | }
|
|---|