source: CIVL/examples/translation/pthread/stack_false.c@ 3f8afc7

1.23 2.0 main test-branch
Last change on this file since 3f8afc7 was e182ee04, checked in by John Edenhofner <johneden@…>, 12 years ago

Added basic scheduling header, modified pthread_mutex_lock as well as mutliple other methods

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

  • Property mode set to 100644
File size: 1.5 KB
RevLine 
[09bc145]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
[e182ee04]10unsigned int __VERIFIER_nondet_uint(void);
[09bc145]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 (get_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 flag=TRUE;
85 pthread_mutex_unlock(&m);
86 }
87}
88
89void *t2(void *arg)
90{
91 int i;
92
93 for(i=0; i<SIZE; i++)
94 {
95 pthread_mutex_lock(&m);
96 if (flag)
97 {
98 if (!(pop(arr)!=UNDERFLOW))
99 error();
100 }
101 pthread_mutex_unlock(&m);
102 }
103}
104
105
106int main(void)
107{
108 pthread_t id1, id2;
109
110 pthread_mutex_init(&m, 0);
111
112 pthread_create(&id1, NULL, t1, NULL);
113 pthread_create(&id2, NULL, t2, NULL);
114
115 pthread_join(id1, NULL);
116 pthread_join(id2, NULL);
117
118 return 0;
119}
120
Note: See TracBrowser for help on using the repository browser.