1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Command line execution:
|
|---|
| 3 | * civl verify -inputSIZE=8 stack_false.cvl -min
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include <civlc.h>
|
|---|
| 7 | #include "pthread.cvh"
|
|---|
| 8 | #include <stdio.h>
|
|---|
| 9 | #include <assert.h>
|
|---|
| 10 | #include <stdlib.h>
|
|---|
| 11 |
|
|---|
| 12 | $input int SIZE;
|
|---|
| 13 |
|
|---|
| 14 | _Bool TRUE = 1;
|
|---|
| 15 | _Bool FALSE = 0;
|
|---|
| 16 | int OVERFLOW = -1;
|
|---|
| 17 | int UNDERFLOW = -2;
|
|---|
| 18 |
|
|---|
| 19 | int top = 0;
|
|---|
| 20 | unsigned int arr[SIZE];
|
|---|
| 21 | pthread_mutex_t m;
|
|---|
| 22 | _Bool flag=$false;
|
|---|
| 23 |
|
|---|
| 24 | void error(void)
|
|---|
| 25 | {
|
|---|
| 26 | $assert($false);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | void inc_top(void)
|
|---|
| 30 | {
|
|---|
| 31 | top++;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | void dec_top(void)
|
|---|
| 35 | {
|
|---|
| 36 | top--;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | int get_top(void)
|
|---|
| 40 | {
|
|---|
| 41 | return top;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | int stack_empty(void)
|
|---|
| 45 | {
|
|---|
| 46 | (top==0) ? TRUE : FALSE;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | int push(unsigned int *stack, int x)
|
|---|
| 50 | {
|
|---|
| 51 | if (top==SIZE)
|
|---|
| 52 | {
|
|---|
| 53 | printf("stack overflow\n");
|
|---|
| 54 | return OVERFLOW;
|
|---|
| 55 | }
|
|---|
| 56 | else
|
|---|
| 57 | {
|
|---|
| 58 | stack[get_top()] = x;
|
|---|
| 59 | inc_top();
|
|---|
| 60 | }
|
|---|
| 61 | return 0;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | int pop(unsigned int *stack)
|
|---|
| 65 | {
|
|---|
| 66 | if (get_top()==0)
|
|---|
| 67 | {
|
|---|
| 68 | printf("stack underflow\n");
|
|---|
| 69 | return UNDERFLOW;
|
|---|
| 70 | }
|
|---|
| 71 | else
|
|---|
| 72 | {
|
|---|
| 73 | dec_top();
|
|---|
| 74 | return stack[get_top()];
|
|---|
| 75 | }
|
|---|
| 76 | return 0;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | void *t1(void*arg)
|
|---|
| 80 | {
|
|---|
| 81 | int i;
|
|---|
| 82 | unsigned int tmp;
|
|---|
| 83 |
|
|---|
| 84 | for(i=0; i<SIZE; i++)
|
|---|
| 85 | {
|
|---|
| 86 | pthread_mutex_lock(&m);
|
|---|
| 87 | tmp = 1;
|
|---|
| 88 | tmp = tmp % 5;
|
|---|
| 89 | int tmp1 = push(arr,tmp);
|
|---|
| 90 | if (tmp1==OVERFLOW)
|
|---|
| 91 | error();
|
|---|
| 92 | flag=TRUE;
|
|---|
| 93 | pthread_mutex_unlock(&m);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void *t2(void*arg)
|
|---|
| 98 | {
|
|---|
| 99 | int i;
|
|---|
| 100 |
|
|---|
| 101 | for(i=0; i<SIZE; i++)
|
|---|
| 102 | {
|
|---|
| 103 | pthread_mutex_lock(&m);
|
|---|
| 104 | if (flag)
|
|---|
| 105 | {
|
|---|
| 106 | int tmp = pop(arr);
|
|---|
| 107 | if (!(tmp)!=UNDERFLOW)
|
|---|
| 108 | error();
|
|---|
| 109 | }
|
|---|
| 110 | pthread_mutex_unlock(&m);
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | void main()
|
|---|
| 116 | {
|
|---|
| 117 | pthread_t id1, id2;
|
|---|
| 118 |
|
|---|
| 119 | pthread_mutex_init(&m, 0);
|
|---|
| 120 |
|
|---|
| 121 | pthread_create(&id1, NULL, t1, NULL);
|
|---|
| 122 | pthread_create(&id2, NULL, t2, NULL);
|
|---|
| 123 |
|
|---|
| 124 | pthread_join(id1, NULL);
|
|---|
| 125 | pthread_join(id2, NULL);
|
|---|
| 126 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.