source: CIVL/examples/translation/pthread/stack_true.cvl@ bfebb46

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

added comments to pthread examples.

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

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