source: CIVL/examples/pthread/esbmc/queue_ok_true-unreach-call.c@ 1aaefd4

main test-branch
Last change on this file since 1aaefd4 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: 2.1 KB
Line 
1extern void __VERIFIER_error();
2
3#include <pthread.h>
4#include <stdio.h>
5#include <assert.h>
6
7#define SIZE (20)
8#define EMPTY (-1)
9#define FULL (-2)
10#define FALSE (0)
11#define TRUE (1)
12
13typedef struct {
14 int element[SIZE];
15 int head;
16 int tail;
17 int amount;
18} QType;
19
20pthread_mutex_t m;
21int __VERIFIER_nondet_int();
22int stored_elements[SIZE];
23_Bool enqueue_flag, dequeue_flag;
24QType queue;
25
26int init(QType *q)
27{
28 q->head=0;
29 q->tail=0;
30 q->amount=0;
31}
32
33int empty(QType * q)
34{
35 if (q->head == q->tail)
36 {
37 printf("queue is empty\n");
38 return EMPTY;
39 }
40 else
41 return 0;
42}
43
44int full(QType * q)
45{
46 if (q->amount == SIZE)
47 {
48 printf("queue is full\n");
49 return FULL;
50 }
51 else
52 return 0;
53}
54
55int enqueue(QType *q, int x)
56{
57 q->element[q->tail] = x;
58 q->amount++;
59 if (q->tail == SIZE)
60 {
61 q->tail = 1;
62 }
63 else
64 {
65 q->tail++;
66 }
67
68 return 0;
69}
70
71int dequeue(QType *q)
72{
73 int x;
74
75 x = q->element[q->head];
76 q->amount--;
77 if (q->head == SIZE)
78 {
79 q->head = 1;
80 }
81 else
82 q->head++;
83
84 return x;
85}
86
87void *t1(void *arg)
88{
89 int value, i;
90
91 pthread_mutex_lock(&m);
92 if (enqueue_flag)
93 {
94 for(i=0; i<SIZE; i++)
95 {
96 value = __VERIFIER_nondet_int();
97 enqueue(&queue,value);
98 stored_elements[i]=value;
99 }
100 enqueue_flag=FALSE;
101 dequeue_flag=TRUE;
102 }
103 pthread_mutex_unlock(&m);
104
105 return NULL;
106}
107
108void *t2(void *arg)
109{
110 int i;
111
112 pthread_mutex_lock(&m);
113 if (dequeue_flag)
114 {
115 for(i=0; i<SIZE; i++)
116 {
117 if (empty(&queue)!=EMPTY)
118 if (!(dequeue(&queue)==stored_elements[i])) {
119 ERROR: __VERIFIER_error();
120 }
121 }
122 dequeue_flag=FALSE;
123 enqueue_flag=TRUE;
124 }
125 pthread_mutex_unlock(&m);
126
127 return NULL;
128}
129
130int main(void)
131{
132 pthread_t id1, id2;
133
134 enqueue_flag=TRUE;
135 dequeue_flag=FALSE;
136
137 init(&queue);
138
139 if (!empty(&queue)==EMPTY) {
140 ERROR: __VERIFIER_error();
141 }
142
143 pthread_mutex_init(&m, 0);
144
145 pthread_create(&id1, NULL, t1, &queue);
146 pthread_create(&id2, NULL, t2, &queue);
147
148 pthread_join(id1, NULL);
149 pthread_join(id2, NULL);
150
151 return 0;
152}
153
Note: See TracBrowser for help on using the repository browser.