source: CIVL/examples/translation/pthread/stack_false.c@ 50f834b

1.23 2.0 main test-branch
Last change on this file since 50f834b 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.7 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#include <stdlib.h>
9
10#define TRUE (1)
11#define FALSE (0)
12#define SIZE (5)
13#define OVERFLOW (-1)
14#define UNDERFLOW (-2)
15
16static int top=0;
17static unsigned int arr[SIZE];
18pthread_mutex_t m;
19_Bool flag=FALSE;
20
21unsigned int unsignednondet(void)
22{
23 if (((double) rand() / (RAND_MAX+1.0)) > 0.5)
24 return 1;
25 return 0;
26}
27
28void error(void)
29{
30 ERROR: ;
31 goto ERROR;
32 return;
33}
34
35void inc_top(void)
36{
37 top++;
38}
39
40void dec_top(void)
41{
42 top--;
43}
44
45int get_top(void)
46{
47 return top;
48}
49
50int stack_empty(void)
51{
52 (top==0) ? TRUE : FALSE;
53}
54
55int push(unsigned int *stack, int x)
56{
57 if (top==SIZE)
58 {
59 printf("stack overflow\n");
60 return OVERFLOW;
61 }
62 else
63 {
64 stack[get_top()] = x;
65 inc_top();
66 }
67 return 0;
68}
69
70int pop(unsigned int *stack)
71{
72 if (get_top()==0)
73 {
74 printf("stack underflow\n");
75 return UNDERFLOW;
76 }
77 else
78 {
79 dec_top();
80 return stack[get_top()];
81 }
82 return 0;
83}
84
85void *t1(void *arg)
86{
87 int i;
88 unsigned int tmp;
89
90 for(i=0; i<SIZE; i++)
91 {
92 pthread_mutex_lock(&m);
93 tmp = unsignednondet()%SIZE;
94 if (push(arr,tmp)==OVERFLOW)
95 error();
96 flag=TRUE;
97 pthread_mutex_unlock(&m);
98 }
99}
100
101void *t2(void *arg)
102{
103 int i;
104
105 for(i=0; i<SIZE; i++)
106 {
107 pthread_mutex_lock(&m);
108 if (flag)
109 {
110 if (!(pop(arr)!=UNDERFLOW))
111 error();
112 }
113 pthread_mutex_unlock(&m);
114 }
115}
116
117
118int main(void)
119{
120 pthread_t id1, id2;
121
122 pthread_mutex_init(&m, 0);
123
124 pthread_create(&id1, NULL, t1, NULL);
125 pthread_create(&id2, NULL, t2, NULL);
126
127 pthread_join(id1, NULL);
128 pthread_join(id2, NULL);
129
130 return 0;
131}
132
133
Note: See TracBrowser for help on using the repository browser.