-
Notifications
You must be signed in to change notification settings - Fork 0
/
philosophers_stm.h
112 lines (95 loc) · 2.85 KB
/
philosophers_stm.h
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
#ifndef PHILOSOPHERS_STM_H
#define PHILOSOPHERS_STM_H
#include <functional>
#include <any>
#include <stm.h>
#include "philosophers_types.h"
namespace philosophers
{
using namespace stm;
const std::function<STML<ForkPair>(TForkPair)>
readForks =
[](const TForkPair& forks)
{
STML<Fork> lm = readTVar(forks.left);
STML<Fork> rm = readTVar(forks.right);
return both(lm, rm, mkForkPair);
};
const std::function<bool(Fork)>
isForkTaken = [](const Fork& fork)
{
return fork.state == ForkState::Taken;
};
const std::function<Fork(Fork)>
setForkTaken = [](const Fork& fork)
{
return Fork {fork.name, ForkState::Taken};
};
const std::function<int(int)>
increment = [](int i)
{
return i + 1;
};
const std::function<Fork(Fork)>
setFree = [](const Fork& fork)
{
return Fork {fork.name, ForkState::Free};
};
STML<bool> takeFork(const TFork& tFork)
{
STML<bool> taken = withTVar(tFork, isForkTaken);
STML<Unit> newFork = modifyTVar(tFork, setForkTaken);
STML<bool> success = sequence(newFork, pure(true));
STML<bool> fail = pure(false);
STML<bool> result = ifThenElse(taken, fail, success);
return result;
}
STML<bool> takeForks(const TForkPair& forks)
{
STML<bool> lm = takeFork(forks.left);
STML<bool> rm = takeFork(forks.right);
return both<bool, bool, bool>(lm, rm, [](bool l, bool r) { return l && r; });
}
STML<Unit> putFork(const TFork& tFork)
{
std::function<Fork(Fork)> f = [](const Fork& fork)
{
return Fork {fork.name, ForkState::Free};
};
return modifyTVar(tFork, f);
}
STML<Unit> putForks(const TForkPair& forks)
{
STML<Unit> lm = putFork(forks.left);
STML<Unit> rm = putFork(forks.right);
return bothVoided(lm, rm);
}
STML<Activity> changeActivity(const Philosopher& philosopher)
{
STML<Activity> mAct = readTVar(philosopher.activity);
STML<Activity> newAct = bind<Activity, Activity>(mAct, [=](Activity act)
{
if (act == Activity::Thinking)
{
STML<bool> taken = takeForks(philosopher.forks);
STML<Unit> changed = writeTVar(philosopher.activity, Activity::Eating);
STML<Unit> result = ifThenElse(taken, changed, mRetry);
return sequence(result, pure(Activity::Eating));
}
else
{
STML<Unit> freed = putForks(philosopher.forks);
STML<Unit> changed = writeTVar(philosopher.activity, Activity::Thinking);
STML<Unit> result = stm::sequence(freed, changed);
return sequence(result, pure(Activity::Thinking));
}
});
return newAct;
}
const std::function<STML<int>(Philosopher)>
incrementCycle = [](const Philosopher& philosopher)
{
return modifyTVarRet(philosopher.cycle, increment);
};
} // namespace philosophers
#endif // PHILOSOPHERS_STM_H