| 1 | #include <civlc.cvh>
|
|---|
| 2 | #include <seq.cvh>
|
|---|
| 3 | #pragma CIVL ACSL
|
|---|
| 4 |
|
|---|
| 5 | $scope root = $here;
|
|---|
| 6 |
|
|---|
| 7 | typedef struct cqueue_t{
|
|---|
| 8 | int data[];
|
|---|
| 9 | $proc owner;
|
|---|
| 10 | } cqueue;
|
|---|
| 11 |
|
|---|
| 12 | /*@ depends_on \nothing;
|
|---|
| 13 | @ assigns \nothing;
|
|---|
| 14 | @ reads \nothing;
|
|---|
| 15 | @*/
|
|---|
| 16 | $atomic_f void create(cqueue* q)
|
|---|
| 17 | {
|
|---|
| 18 | q = (cqueue*)$malloc(root, sizeof(cqueue));
|
|---|
| 19 | q->owner=$proc_null;
|
|---|
| 20 | $seq_init(&q->data, 0, NULL);
|
|---|
| 21 | }
|
|---|
| 22 | /*@ depends_on \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 | /*@ depends_on \call(enqueue, q, ...), \call(dequeue,q, ...);
|
|---|
| 36 | @ reads q->data;
|
|---|
| 37 | @ assigns \nothing;
|
|---|
| 38 | @*/
|
|---|
| 39 | $atomic_f int size(cqueue* q)
|
|---|
| 40 | {
|
|---|
| 41 | return $seq_length(&q->data);
|
|---|
| 42 | }
|
|---|
| 43 | /*@ reads q->owner;
|
|---|
| 44 | @ behavior success:
|
|---|
| 45 | @ assumes q->owner==$self;
|
|---|
| 46 | @ depends_on \read(&(q->owner)) + \write(&(q->owner));
|
|---|
| 47 | @ assigns q->owner;
|
|---|
| 48 | @ behavior failure:
|
|---|
| 49 | @ assumes q->owner!=$self;
|
|---|
| 50 | @ depends_on \nothing;
|
|---|
| 51 | @ assigns \nothing;
|
|---|
| 52 | @*/
|
|---|
| 53 | $atomic_f _Bool unlock(cqueue* q)
|
|---|
| 54 | {
|
|---|
| 55 | if(q->owner==$self)
|
|---|
| 56 | {
|
|---|
| 57 | q->owner=$proc_null;
|
|---|
| 58 | return $true;
|
|---|
| 59 | }
|
|---|
| 60 | return $false;
|
|---|
| 61 | }
|
|---|
| 62 | /*@ depends_on \call(enqueue,q, ...);
|
|---|
| 63 | @ reads q->data;
|
|---|
| 64 | @*/
|
|---|
| 65 | $atomic_f _Bool enqueue(cqueue* q, int v)
|
|---|
| 66 | {
|
|---|
| 67 | $seq_append(&q->data, &v, 1);
|
|---|
| 68 | }
|
|---|
| 69 | /*@ depends_on \call(enqueue, q, ...);
|
|---|
| 70 | @ reads q->data;
|
|---|
| 71 | @*/
|
|---|
| 72 | $atomic_f _Bool dequeue(cqueue* q, int* res)
|
|---|
| 73 | {
|
|---|
| 74 | $seq_remove(&q->data, $seq_length(&q->data)-1, res, 1);
|
|---|
| 75 | }
|
|---|