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

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

Modified header files and fixed Ticket 218

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

  • Property mode set to 100755
File size: 3.0 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: fib_bench_false.cvl
6* DESCRIPTION:
7* Simple program of threads interacting which causes an error when
8* their total reaches a certain value. The fix simply involves changing the output
9* for which an error will be caused; specifically, here error is caused when i or j
10* >=144, the fix has i or j > 144.
[a83ea55]11* Command line execution:
[506de9d]12* civl verify -inputSIZE=5 queue_false.cvl
13******************************************************************************/
[a83ea55]14
[8d512ae]15#include "pthread.h"
[5fb1a47]16#include <stdio.h>
17#include <assert.h>
18#include <civlc.h>
19#include <stdlib.h>
20
[506de9d]21$input int SIZE;
[5fb1a47]22#define EMPTY (-1)
23#define FULL (-2)
24#define FALSE (0)
25#define TRUE (1)
26
27typedef struct {
28 int element[SIZE];
29 int head;
30 int tail;
31 int amount;
32} QType;
33
34int stored_elements[SIZE];
35_Bool enqueue_flag, dequeue_flag;
36pthread_mutex_t mutex;
37QType queue;
38
39int init(QType *q)
40{
41 q->head=0;
42 q->tail=0;
43 q->amount=0;
[37fe96b]44 return 0;
[5fb1a47]45}
46
47int empty(QType *q)
48{
49 if (q->head == q->tail)
50 {
51 printf("queue is empty\n");
52 return EMPTY;
53 }
54 else
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
105 pthread_mutex_lock(&mutex);
106 value = 0;
107 int tmp = enqueue(&queue,value);
108 printf("0");
109 if (tmp) {
110 $assert($false);
111 }
112
113 stored_elements[0]=value;
114 int tmp1 = empty(&queue);
115 if (tmp1) {
116 $assert($false);
117 }
118
119 pthread_mutex_unlock(&mutex);
120
121 for(i=0; i<(SIZE-1); i++)
122 {
123 pthread_mutex_lock(&mutex);
124 printf("1");
125 if (enqueue_flag)
126 {
127 value = 1;
128 enqueue(&queue,value);
129 stored_elements[i+1]=value;
130 enqueue_flag=FALSE;
131 dequeue_flag=TRUE;
132 }
133 pthread_mutex_unlock(&mutex);
134 }
[37fe96b]135 return NULL;
[5fb1a47]136}
137
138void *t2(void *arg)
139{
140 int i;
141
142 for(i=0; i<SIZE; i++)
143 {
144 pthread_mutex_lock(&mutex);
145 if (dequeue_flag)
146 printf("2");
147 {
148 int tmp = dequeue(&queue);
149 if (!tmp==stored_elements[i]) {
150 $assert($false);
151 }
152 dequeue_flag=FALSE;
153 enqueue_flag=TRUE;
154 }
155 pthread_mutex_unlock(&mutex);
156 }
[37fe96b]157 return NULL;
[5fb1a47]158}
159
160void main()
161{
[1746c5c]162 pthread_t id1, id2;
[5fb1a47]163
164 enqueue_flag=TRUE;
165 dequeue_flag=FALSE;
166
[37fe96b]167 pthread_mutex_init(&mutex, 0);
[5fb1a47]168 init(&queue);
169 int tmp = empty(&queue);
170 if (!tmp==EMPTY) {
171 $assert($false);
172 }
173
174 pthread_create(&id1, NULL, t1, &queue);
175 pthread_create(&id2, NULL, t2, &queue);
176
177 pthread_join(id1, NULL);
178 pthread_join(id2, NULL);
179}
Note: See TracBrowser for help on using the repository browser.