-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex.y
61 lines (50 loc) · 980 Bytes
/
mutex.y
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
/**********************************************************************
AUTHOR: Kenneth Pollick <me@kennethpollick.com>
COPYRIGHT: 2022-2023 Kenneth Pollick
DATE: 2023-04-21
**********************************************************************/
//also now going to be part of the language to coincide with the addition of the execution model
mutex: dt sdt
{
dt#0 data;
natural pid; //TODO: implementation change
boole using;
/*ctor()
{
data = dt();
pid = 0;
}*/
ctor(dt initial)
{
this = mutex{initial, 0, F};
}
parallel wait_lock(natural my_pid)
{
while (this.pid ~= my_pid)
{
while (this.pid ~= 0) {}
this.pid = my_pid;
}
}
parallel boole try_lock(natural my_pid)
{
if (this.pid == 0)
{
this.pid = my_pid;
return (this.pid == my_pid);
}
return F;
}
become operator dt unary_*()
{
return this.data;
}
unlock(natural my_pid)
{
if (this.pid == my_pid)
{
this.pid = 0;
this.using = F;
}
}
}