forked from cindywhan/cosc301_proj04
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test03.c
83 lines (63 loc) · 1.45 KB
/
test03.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <assert.h>
#include "threadsalive.h"
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
talock_t mutex;
tacond_t condv;
int value = 0;
void thread1(void *v)
{
fprintf(stderr, "thread1 started up\n");
ta_lock(&mutex);
ta_yield();
while (value == 0)
{
fprintf(stderr, "thread1 going into cond_wait()\n");
ta_wait(&mutex, &condv);
}
fprintf(stderr, "thread1 emerged from cond_wait()\n");
value = 42;
ta_yield();
ta_unlock(&mutex);
fprintf(stderr, "thread1 exiting\n");
}
void thread2(void *v)
{
fprintf(stderr, "thread2 started up\n");
ta_lock(&mutex);
ta_yield();
fprintf(stderr, "thread2 not updating value, signalling thread1\n");
ta_signal(&condv);
ta_unlock(&mutex);
ta_yield();
ta_lock(&mutex);
ta_yield();
fprintf(stderr, "thread2 IS updating value, signalling thread1\n");
value = -1;
ta_signal(&condv);
ta_unlock(&mutex);
fprintf(stderr, "thread2 exiting\n");
}
int main(int argc, char **argv)
{
printf("Tester for stage 3.\n");
ta_libinit();
ta_lock_init(&mutex);
ta_cond_init(&condv);
ta_create(thread1, NULL);
ta_create(thread2, NULL);
int rv = ta_waitall();
assert(rv == 0);
assert(value == 42);
ta_lock_destroy(&mutex);
ta_cond_destroy(&condv);
return 0;
}