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