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