source: CIVL/examples/contracts/cqueue.c@ bb03188

main test-branch
Last change on this file since bb03188 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.7 KB
Line 
1#include <civlc.cvh>
2#include <seq.cvh>
3
4typedef struct cqueue_t{
5 int data[];
6 $proc owner;
7} cqueue;
8
9$scope root = $here;
10
11/*@ guards \true;
12 @ depends \noact;
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 \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
78cqueue q;
79int a;
80
81void foo(){
82 enqueue(&q, 1);
83}
84
85void goo(){
86 dequeue(&q, &a);
87 $assert(a==0);
88}
89
90int 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}
Note: See TracBrowser for help on using the repository browser.