-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox.c
73 lines (56 loc) · 1007 Bytes
/
mailbox.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
#include <assert.h>
#include "lock.h"
#include "mailbox.h"
#include "ULT.h"
static void sanity();
void
mb_init(Mailbox *mb, int workers, int max)
{
Lock_Init(&(mb->mutex));
mb->maxCount = max;
mb->counter = 0;
mb->partial0 = 0;
mb->partial1 = 0;
}
void
mb_increment(Mailbox *mb, int whichPartial)
{
Lock_Acquire(&(mb->mutex));
sanity(mb);
ULT_Yield(ULT_ANY);
sanity(mb);
mb->counter++;
ULT_Yield(ULT_ANY);
ULT_Yield(ULT_ANY);
ULT_Yield(ULT_ANY);
ULT_Yield(ULT_ANY);
if(whichPartial % 2 == 0){
mb->partial0++;
}
else{
mb->partial1++;
}
sanity(mb);
Lock_Release(&(mb->mutex));
return;
}
int
mb_checkDone(Mailbox *mb)
{
int ret = 0;
Lock_Acquire(&(mb->mutex));
sanity(mb);
ULT_Yield(ULT_ANY);
sanity(mb);
if(mb->counter == mb->maxCount){
ret = 1;
}
Lock_Release(&(mb->mutex));
return ret;
}
static void
sanity(Mailbox *mb)
{
assert(mb->counter <= mb->maxCount);
assert(mb->partial0 + mb->partial1 == mb->counter);
}