source: CIVL/examples/pthread/esbmc/queue_ok_true.c@ e0fc189

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