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