source: CIVL/examples/translation/pthread/queue_false.c@ 1c47cb2

1.23 2.0 main test-branch
Last change on this file since 1c47cb2 was 362cf63, checked in by John Edenhofner <johneden@…>, 12 years ago

Reverted queue_false and queue_ok_true to their original states.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1195 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();
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 value = __VERIFIER_nondet_int();
91 if (enqueue(&queue,value)) {
92 goto ERROR;
93 }
94
95 stored_elements[0]=value;
96 if (empty(&queue)) {
97 goto ERROR;
98 }
99
100 pthread_mutex_unlock(&m);
101
102 for(i=0; i<(SIZE-1); i++)
103 {
104 pthread_mutex_lock(&m);
105 if (enqueue_flag)
106 {
107 value = __VERIFIER_nondet_int();
108 enqueue(&queue,value);
109 stored_elements[i+1]=value;
110 enqueue_flag=FALSE;
111 dequeue_flag=TRUE;
112 }
113 pthread_mutex_unlock(&m);
114 }
115
116 return NULL;
117
118 ERROR:
119 goto 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:
133 goto ERROR;
134 }
135 dequeue_flag=FALSE;
136 enqueue_flag=TRUE;
137 }
138 pthread_mutex_unlock(&m);
139 }
140
141 return NULL;
142}
143
144int main(void)
145{
146 pthread_t id1, id2;
147
148 enqueue_flag=TRUE;
149 dequeue_flag=FALSE;
150
151 init(&queue);
152
153 if (!empty(&queue)==EMPTY) {
154 ERROR:
155 goto ERROR;
156 }
157
158
159 pthread_mutex_init(&m, 0);
160
161 pthread_create(&id1, NULL, t1, &queue);
162 pthread_create(&id2, NULL, t2, &queue);
163
164 pthread_join(id1, NULL);
165 pthread_join(id2, NULL);
166
167 return 0;
168}
169
170
171
Note: See TracBrowser for help on using the repository browser.