source: CIVL/examples/pthread/esbmc/queue_longest_false-unreach-call.c@ beab7f2

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