source: CIVL/examples/translation/pthread/queue_ok_true.cvl@ d87ec9c

1.23 2.0 main test-branch
Last change on this file since d87ec9c was 37fe96b, checked in by John Edenhofner <johneden@…>, 12 years ago

Modified pthread_mutex_lock to work without the need for a state function, as well as modifying pthread_cond_wait to be in line with the specification.

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

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