-
Notifications
You must be signed in to change notification settings - Fork 0
/
philosophers_stm.h
95 lines (80 loc) · 2.24 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
#ifndef PHILOSOPHERS_STM_H
#define PHILOSOPHERS_STM_H
#include <functional>
#include <any>
#include <stm/stm.h>
#include "philosophers_types.h"
namespace philosophers
{
using namespace stm;
const std::function<Fork(Fork)>
setForkTaken = [](const Fork& fork)
{
return Fork {fork.name, ForkState::Taken};
};
const std::function<Fork(Fork)>
setForkFree = [](const Fork& fork)
{
return Fork {fork.name, ForkState::Free};
};
const std::function<int(int)>
increment = [](int i)
{
return i + 1;
};
STML<Unit> takeFork(const TFork& tFork)
{
// Warning! Passing closure by ref leads to crash.
return withTVar<Fork, Unit>(tFork, [=](const Fork& fork)
{
if (fork.state == ForkState::Free)
{
return modifyTVar<Fork>(tFork, setForkTaken);
}
else
{
return retry<Unit>();
}
});
}
STML<Unit> takeForks(const TForkPair& forks)
{
STML<Unit> lm = takeFork(forks.left);
STML<Unit> rm = takeFork(forks.right);
return sequence(lm, rm);
}
STML<Unit> putFork(const TFork& tFork)
{
return modifyTVar<Fork>(tFork, setForkFree);
}
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<Unit> changed = bind<Activity, Unit>(mAct, [=](Activity oldAct)
{
if (oldAct == Activity::Thinking)
{
STML<Unit> taken = takeForks(philosopher.forks);
return sequence<Unit, Unit>(taken, writeTVar<Activity>(philosopher.activity, Activity::Eating));
}
else
{
STML<Unit> freed = putForks(philosopher.forks);
return sequence<Unit, Unit>(freed, writeTVar<Activity>(philosopher.activity, Activity::Thinking));
}
});
return sequence<Unit, Activity>(changed, readTVar<Activity>(philosopher.activity));
}
const std::function<STML<int>(Philosopher)>
incrementCycle = [](const Philosopher& philosopher)
{
return modifyTVarRet<int>(philosopher.cycle, increment);
};
} // namespace philosophers
#endif // PHILOSOPHERS_STM_H