source: CIVL/examples/translation/pthread/stack_true.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
5$input int SIZE;
6
7_Bool TRUE = 1;
8_Bool FALSE = 0;
9int SIZE = 5;
10int OVERFLOW = -1;
11int UNDERFLOW = -2;
12
13static int top=0;
14static unsigned int arr[SIZE];
15pthread_mutex_t m;
16_Bool flag=FALSE;
17
18void error(void)
19{
20 $assert($false);
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 = 1%SIZE;
82 int tmp1 = push(arr,tmp);
83 if (tmp1==OVERFLOW)
84 error();
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 (top>0)
97 {
98 int tmp = pop(arr);
99 if (tmp==UNDERFLOW)
100 error();
101 }
102 pthread_mutex_unlock(&m);
103 }
104}
105
106
107void main()
108{
109 pthread_t id1, id2;
110
111 pthread_mutex_init(&m, 0);
112
113 pthread_create(&id1, NULL, t1, NULL);
114 pthread_create(&id2, NULL, t2, NULL);
115
116 pthread_join(id1, NULL);
117 pthread_join(id2, NULL);
118}
Note: See TracBrowser for help on using the repository browser.