source: CIVL/examples/pthread/esbmc/queue_false.c@ bddaf4c

1.23 2.0 main test-branch
Last change on this file since bddaf4c was e3151da, checked in by Ziqing Luo <ziqing@…>, 11 years ago

re-organized example directory

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

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#include <pthread.h>
2#include <stdio.h>
3#include <assert.h>
4
5#define SIZE (5)
6#define EMPTY (-1)
7#define FULL (-2)
8#define FALSE (0)
9#define TRUE (1)
10
11typedef struct {
12 int element[SIZE];
13 int head;
14 int tail;
15 int amount;
16} QType;
17
18pthread_mutex_t m;
19int __VERIFIER_nondet_int(void);
20int stored_elements[SIZE];
21_Bool enqueue_flag, dequeue_flag;
22QType queue;
23
24int init(QType *q)
25{
26 q->head=0;
27 q->tail=0;
28 q->amount=0;
29 return 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:
120 goto ERROR;
121}
122
123void *t2(void *arg)
124{
125 int i;
126
127 for(i=0; i<SIZE; i++)
128 {
129 pthread_mutex_lock(&m);
130 if (dequeue_flag)
131 {
132 if (!dequeue(&queue)==stored_elements[i]) {
133 ERROR:
134 goto ERROR;
135 }
136 dequeue_flag=FALSE;
137 enqueue_flag=TRUE;
138 }
139 pthread_mutex_unlock(&m);
140 }
141
142 return NULL;
143}
144
145int main()
146{
147 pthread_t id1, id2;
148
149 enqueue_flag=TRUE;
150 dequeue_flag=FALSE;
151
152 init(&queue);
153
154 if (!empty(&queue)==EMPTY) {
155 ERROR:
156 goto ERROR;
157 }
158
159
160 pthread_mutex_init(&m, 0);
161
162 pthread_create(&id1, NULL, t1, &queue);
163 pthread_create(&id2, NULL, t2, &queue);
164
165 pthread_join(id1, NULL);
166 pthread_join(id2, NULL);
167
168 return 0;
169}
170
171
172
Note: See TracBrowser for help on using the repository browser.