blob: 8214ffba49d7d2cb23681a5f51610df80d170f20 (
plain)
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
|
# ############################################################################ #
# #
# ::: :::::::: #
# table.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/09/27 12:44:48 by charles #+# #+# #
# Updated: 2020/09/27 16:54:52 by charles ### ########.fr #
# #
# ############################################################################ #
from philo.philo import Philo
from philo.event import Event
class Table:
def __init__(self, philo_num):
self._philos = [Philo(id_) for id_ in range(1, philo_num + 1)]
self._logs = []
self._philo_num = philo_num
def add_log(self, log):
self._logs.append(log)
philo = next(p for p in self._philos if p.id == log.id)
philo.add_log(log)
def check(self):
died_count = len([p for p in self._philos if p.last_event == Event.DIED])
if died_count > 1:
raise RuntimeError("died")
fork_used = 2 * len([p for p in self._philos if p.last_event == Event.EATING])
if fork_used > self._philo_num:
raise RuntimeError("too much fork")
for p in self._philos:
p.check()
for l1, l2 in zip(self._logs, self._logs[1:]):
if l1.timestamp > l2.timestamp:
raise RuntimeError("timestamp not ordered")
|