source: CIVL/examples/pthread/queue_ok_true.cvl@ 325d439

1.23 2.0 main test-branch
Last change on this file since 325d439 was 9f23e8b, checked in by John Edenhofner <johneden@…>, 12 years ago

Added bug fix programs and some documentation

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

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*****************************************************************************
2* SOURCE: This is a translation of a Pthread program from
3* the Pthread benchmarks of SV-COMP 2014.
4* https://svn.sosy-lab.org/software/sv-benchmarks/tags/svcomp14/
5* FILE: queue_ok_true.cvl
6* DESCRIPTION:
7*
8* Command line execution:
9* civl verify queue_ok_true.cvl
10******************************************************************************/
11
12#include <civlc.h>
13#include <stdio.h>
14#include <assert.h>
15#include <stdlib.h>
16#include "pthread.cvh"
17
18#define SIZE (4)
19#define EMPTY (-1)
20#define FULL (-2)
21#define FALSE (0)
22#define TRUE (1)
23
24typedef struct {
25 int element[SIZE];
26 int head;
27 int tail;
28 int amount;
29} QType;
30
31int stored_elements[SIZE];
32_Bool enqueue_flag, dequeue_flag;
33pthread_mutex_t m;
34QType queue;
35
36int init(QType *q)
37{
38 q->head=0;
39 q->tail=0;
40 q->amount=0;
41}
42
43int nondet()
44{
45 return $choose_int(2);
46}
47
48int empty(QType *q)
49{
50 if (q->head == q->tail)
51 {
52 printf("queue is empty\n");
53 return EMPTY;
54 }
55 return 0;
56}
57
58int full(QType *q)
59{
60 if (q->amount == SIZE)
61 {
62 printf("queue is full\n");
63 return FULL;
64 }
65 else
66 return 0;
67}
68
69int enqueue(QType *q, int x)
70{
71 q->element[q->tail] = x;
72 q->amount++;
73 if (q->tail == SIZE)
74 {
75 q->tail = 1;
76 }
77 else
78 {
79 q->tail++;
80 }
81
82 return 0;
83}
84
85int dequeue(QType *q)
86{
87 int x;
88
89 x = q->element[q->head];
90 q->amount--;
91 if (q->head == SIZE)
92 {
93 q->head = 1;
94 }
95 else
96 q->head++;
97
98 return x;
99}
100
101void *t1(void *arg)
102{
103 int value, i;
104 printf("1");
105 pthread_mutex_lock(&m);
106 if (enqueue_flag)
107 {
108 for(i=0; i<SIZE; i++)
109 {
110 value = nondet();
111 enqueue(&queue,value);
112 stored_elements[i]=value;
113 }
114 enqueue_flag=FALSE;
115 dequeue_flag=TRUE;
116 }
117 pthread_mutex_unlock(&m);
118
119 return NULL;
120}
121
122void *t2(void *arg)
123{
124 int i;
125
126 pthread_mutex_lock(&m);
127 if (dequeue_flag)
128 {
129 for(i=0; i<SIZE; i++)
130 {
131 int tmp0 = empty(&queue);
132
133 if (tmp0 != EMPTY){
134 int tmp1 = dequeue(&queue);
135
136 if (tmp1 != stored_elements[i]) {
137 $assert($false);
138 }
139 }
140 }
141 dequeue_flag=FALSE;
142 enqueue_flag=TRUE;
143 }
144 pthread_mutex_unlock(&m);
145
146 return NULL;
147}
148
149void main()
150{
151 pthread_t id1, id2;
152
153 enqueue_flag=TRUE;
154 dequeue_flag=FALSE;
155
156 init(&queue);
157 pthread_mutex_init(&m, PTHREAD_MUTEX_DEFAULT);
158
159 int tmp = empty(&queue);
160 if (tmp != EMPTY) {
161 $assert($false);
162 }
163
164 pthread_create(&id1, NULL, t1, &queue);
165 pthread_create(&id2, NULL, t2, &queue);
166
167 pthread_join(id1, NULL);
168 pthread_join(id2, NULL);
169
170}
Note: See TracBrowser for help on using the repository browser.