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