source: CIVL/examples/translation/pthread/stack_false.cvl@ be4355b

1.23 2.0 main test-branch
Last change on this file since be4355b was 5fb1a47, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

added pthread examples

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

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