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

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

added comments to pthread examples.

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

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