source: CIVL/examples/translation/pthread/stack_true.c@ 3f3d4f8

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

added comments.

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

  • Property mode set to 100755
File size: 1.6 KB
Line 
1/**
2* This program is taken from the Pthread benchmarks of SV-COMP 2014.
3* https://svn.sosy-lab.org/software/sv-benchmarks/tags/svcomp14/
4*/
5
6#include <pthread.h>
7#include <stdio.h>
8
9#define TRUE (1)
10#define FALSE (0)
11#define SIZE (5)
12#define OVERFLOW (-1)
13#define UNDERFLOW (-2)
14
15static int top=0;
16static unsigned int arr[SIZE];
17pthread_mutex_t m;
18_Bool flag=FALSE;
19
20void error(void)
21{
22 ERROR: ;
23 goto ERROR;
24 return;
25}
26
27void inc_top(void)
28{
29 top++;
30}
31
32void dec_top(void)
33{
34 top--;
35}
36
37int get_top(void)
38{
39 return top;
40}
41
42int stack_empty(void)
43{
44 (top==0) ? TRUE : FALSE;
45}
46
47int push(unsigned int *stack, int x)
48{
49 if (top==SIZE)
50 {
51 printf("stack overflow\n");
52 return OVERFLOW;
53 }
54 else
55 {
56 stack[get_top()] = x;
57 inc_top();
58 }
59 return 0;
60}
61
62int pop(unsigned int *stack)
63{
64 if (top==0)
65 {
66 printf("stack underflow\n");
67 return UNDERFLOW;
68 }
69 else
70 {
71 dec_top();
72 return stack[get_top()];
73 }
74 return 0;
75}
76
77void *t1(void *arg)
78{
79 int i;
80 unsigned int tmp;
81
82 for(i=0; i<SIZE; i++)
83 {
84 pthread_mutex_lock(&m);
85 tmp = 1%SIZE;
86 if ((push(arr,tmp)==OVERFLOW))
87 error();
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 (top>0)
100 {
101 if ((pop(arr)==UNDERFLOW))
102 error();
103 }
104 pthread_mutex_unlock(&m);
105 }
106}
107
108
109int main(void)
110{
111 pthread_t id1, id2;
112
113 pthread_mutex_init(&m, 0);
114
115 pthread_create(&id1, NULL, t1, NULL);
116 pthread_create(&id2, NULL, t2, NULL);
117
118 pthread_join(id1, NULL);
119 pthread_join(id2, NULL);
120
121 return 0;
122}
123
124
Note: See TracBrowser for help on using the repository browser.