-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytest.c
182 lines (132 loc) · 4.11 KB
/
mytest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Single Author info:
dvvira Denil Vijay Vira
Group info:
dvvira Denil Vijay Vira
prajago4 Priyadarshini Rajagopal
wli29 Wanqui Li
*/
#include "mycond.h"
#include "mybarrier.h"
#include "mymutex.h"
#include <stdio.h>
// set PASS to zero. For an error returned by any function, decrement PASS.
int PASS = 0;
// decalre mutex, condition variable and barrier
mythread_mutex_t mut;
mythread_cond_t con;
mythread_barrier_t barr;
// declare 6 threads, to be used in this test.
mythread_t t1, t2, t3, t4, t5, t6;
int count = 1;
// grab the lock. If count is less than 100, then wait for signal. Once signal is recieved, unlock mutex and wait on barrier.
void *foo1 (void *arg)
{
int test;
mythread_mutex_lock (&mut);
while (count < 100)
{
mythread_cond_wait (&con, &mut);
}
if ( mythread_mutex_unlock (&mut) != 0)
PASS--;
if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD))
PASS--;
return NULL;
}
// grab the mutex, increment the count to 100. Send signal to waiting threads. Unlock the mutex and wait on barrier.
void *foo2 (void *arg)
{
int test;
mythread_mutex_lock (&mut);
while (count < 100)
{
count++;
}
if ( mythread_cond_broadcast (&con) != 0)
PASS--;
if ( mythread_mutex_unlock (&mut) != 0)
PASS--;
if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD))
PASS--;
return NULL;
}
// count up to ceratin number and then wait on barrier.
void *foo3 (void *arg)
{
int test;
int val = 0;
while (val < 999)
val++;
if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD))
PASS--;
while (val > 0)
val--;
return NULL;
}
// count up to ceratin number and then wait on barrier.
void *foo4 (void *arg)
{
int test;
int val = 0;
while (val < 9999)
val++;
if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD))
PASS--;
while (val > 0)
val--;
return NULL;
}
// count up to ceratin number and then wait on barrier.
void *foo5 (void *arg)
{
int test;
int val = 0;
while (val < 99)
val++;
if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD))
PASS--;
while (val > 0)
val--;
return NULL;
}
int main (void)
{
int test;
// initialize mutex, condition variable and barrier.
mythread_mutex_init (&mut, NULL);
mythread_cond_init (&con, NULL);
// 4 threads will be calling wait.
mythread_barrier_init (&barr, NULL, 4);
mythread_setconcurrency (8);
// create first 3 threads and assign them functions.
mythread_create (&t1, NULL, foo1, NULL);
mythread_create (&t2, NULL, foo2, NULL);
mythread_create (&t3, NULL, foo3, NULL);
// stall main until all threads have finished.
if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD))
PASS--;
// after this stage, mutex and condition variables are not required, destroy them.
if ( mythread_cond_destroy (&con) != 0)
PASS --;
if ( mythread_mutex_destroy (&mut) != 0)
PASS --;
// create next 3 threads and assign functions. This is to test reusability of barriers.
mythread_create (&t4, NULL, foo3, NULL);
mythread_create (&t5, NULL, foo4, NULL);
mythread_create (&t6, NULL, foo5, NULL);
// let main also wait on the barrier
if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD))
PASS--;
// at this point, we are sure that all the threads have finished running. Now check value of PASS for any errors.
// If PASS is 0, means all function executed successfully. If PASS is less than 0, atleast one function returned error.
// print PASS or FAIL accordingly.
if (PASS == 0)
printf ("PASS\n");
else
printf ("FAIL\n");
//destroy the barrier and exit.
mythread_barrier_destroy (&barr);
mythread_exit(NULL);
return 0;
}