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