source: CIVL/examples/pthread/queue_ok_true.cvl@ 4b4bbb3

1.23 2.0 main test-branch
Last change on this file since 4b4bbb3 was 38374b7, checked in by John Edenhofner <johneden@…>, 12 years ago

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

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/**
2* 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*
6* Command line execution:
7* civl verify queue_ok_true.cvl
8*/
9
10#include <civlc.h>
11#include <stdio.h>
12#include <assert.h>
13#include <stdlib.h>
14#include "pthread.cvh"
15
16#define SIZE (4)
17#define EMPTY (-1)
18#define FULL (-2)
19#define FALSE (0)
20#define TRUE (1)
21
22typedef struct {
23 int element[SIZE];
24 int head;
25 int tail;
26 int amount;
27} QType;
28
29int stored_elements[SIZE];
30_Bool enqueue_flag, dequeue_flag;
31pthread_mutex_t m;
32QType queue;
33
34int init(QType *q)
35{
36 q->head=0;
37 q->tail=0;
38 q->amount=0;
39}
40
41int nondet()
42{
43 return $choose_int(2);
44}
45
46int empty(QType *q)
47{
48 if (q->head == q->tail)
49 {
50 printf("queue is empty\n");
51 return EMPTY;
52 }
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 printf("1");
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 int tmp0 = empty(&queue);
130
131 if (tmp0 != EMPTY){
132 int tmp1 = dequeue(&queue);
133
134 if (tmp1 != stored_elements[i]) {
135 $assert($false);
136 }
137 }
138 }
139 dequeue_flag=FALSE;
140 enqueue_flag=TRUE;
141 }
142 pthread_mutex_unlock(&m);
143
144 return NULL;
145}
146
147void main()
148{
149 pthread_t id1, id2;
150
151 enqueue_flag=TRUE;
152 dequeue_flag=FALSE;
153
154 init(&queue);
155 pthread_mutex_init(&m, PTHREAD_MUTEX_DEFAULT);
156
157 int tmp = empty(&queue);
158 if (tmp != EMPTY) {
159 $assert($false);
160 }
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}
Note: See TracBrowser for help on using the repository browser.