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