source: CIVL/examples/pthread/esbmc/stack_true.c@ 4a5679a

1.23 2.0 main test-branch
Last change on this file since 4a5679a was e3151da, checked in by Ziqing Luo <ziqing@…>, 11 years ago

re-organized example directory

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1763 fb995dde-84ed-4084-dfe6-e5aef3e2452c

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