source: CIVL/examples/translation/pthread/queue_ok_true.c@ 50f834b

1.23 2.0 main test-branch
Last change on this file since 50f834b was bf46837, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

added comments.

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

  • Property mode set to 100755
File size: 2.3 KB
Line 
1/**
2* This program is taken from the Pthread benchmarks of SV-COMP 2014.
3* https://svn.sosy-lab.org/software/sv-benchmarks/tags/svcomp14/
4*/
5
6#include <pthread.h>
7#include <stdio.h>
8#include <assert.h>
9#include <stdlib.h>
10#define SIZE (20)
11#define EMPTY (-1)
12#define FULL (-2)
13#define FALSE (0)
14#define TRUE (1)
15
16typedef struct {
17 int element[SIZE];
18 int head;
19 int tail;
20 int amount;
21} QType;
22
23pthread_mutex_t m;
24
25int stored_elements[SIZE];
26_Bool enqueue_flag, dequeue_flag;
27QType queue;
28int nondet()
29{
30 if (((double) rand() / (RAND_MAX+1.0)) > 0.5){
31 printf("1");
32 return 1;
33 }
34 printf("0");
35 return 0;
36}
37
38int init(QType *q)
39{
40 q->head=0;
41 q->tail=0;
42 q->amount=0;
43}
44
45int empty(QType * q)
46{
47 if (q->head == q->tail)
48 {
49 printf("queue is empty\n");
50 return EMPTY;
51 }
52 else
53 return 0;
54}
55
56int full(QType * q)
57{
58 if (q->amount == SIZE)
59 {
60 printf("queue is full\n");
61 return FULL;
62 }
63 else
64 return 0;
65}
66
67int enqueue(QType *q, int x)
68{
69 q->element[q->tail] = x;
70 q->amount++;
71 if (q->tail == SIZE)
72 {
73 q->tail = 1;
74 }
75 else
76 {
77 q->tail++;
78 }
79
80 return 0;
81}
82
83int dequeue(QType *q)
84{
85 int x;
86
87 x = q->element[q->head];
88 q->amount--;
89 if (q->head == SIZE)
90 {
91 q->head = 1;
92 }
93 else
94 q->head++;
95
96 return x;
97}
98
99void *t1(void *arg)
100{
101 int value, i;
102
103 pthread_mutex_lock(&m);
104 if (enqueue_flag)
105 {
106 for(i=0; i<SIZE; i++)
107 {
108 value = nondet();
109 enqueue(&queue,value);
110 stored_elements[i]=value;
111 }
112 enqueue_flag=FALSE;
113 dequeue_flag=TRUE;
114 }
115 pthread_mutex_unlock(&m);
116
117 return NULL;
118}
119
120void *t2(void *arg)
121{
122 int i;
123
124 pthread_mutex_lock(&m);
125 if (dequeue_flag)
126 {
127 for(i=0; i<SIZE; i++)
128 {
129 if (empty(&queue)!=EMPTY)
130 if (!dequeue(&queue)==stored_elements[i]) {
131 ERROR:
132 goto ERROR;
133 }
134 }
135 dequeue_flag=FALSE;
136 enqueue_flag=TRUE;
137 }
138 pthread_mutex_unlock(&m);
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:
154 goto ERROR;
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.