Created
December 3, 2018 00:47
-
-
Save mrnkr/b06f675f995a2a91d496eb730d65473c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Chopsticks: Array 0..4 of Chopstick; | |
Philosophers: Array 0..4 of Philosopher; | |
task type Philosopher is | |
end | |
task body Philosopher is | |
Left: Int; | |
Enumerator.Gimme_My_Number(Left); | |
Right: Int := (Left + 1) mod 5; | |
begin | |
loop | |
Table.Enter | |
Chopsticks[Left].Request | |
Chopsticks[Right].Request | |
Eat | |
Chopsticks[Left].Release | |
Chopsticks[Right].Release | |
Table.Leave | |
end loop | |
end Philosopher; | |
task Enumerator is | |
entry Gimme_My_Number (Num: out Int); | |
end | |
task body Enumerator is | |
Next: Int := 0; | |
begin | |
loop | |
accept Gimme_My_Number (Num: out Int) do | |
Num := Next; | |
end; | |
Next := Next + 1; | |
end loop | |
end Enumerator; | |
task type Chopstick is | |
entry Request; | |
entry Release; | |
end | |
task body Chopstick is | |
begin | |
loop | |
accept Request; | |
accept Release; | |
end loop | |
end Chopstick; | |
task Table is | |
entry Enter; | |
entry Leave; | |
end | |
task body Table is | |
Remaining_Space: Int := 4; | |
begin | |
loop | |
select | |
when Remaining_Space > 0 accept Enter; | |
Remaining_Space := Remaining_Space - 1; | |
or | |
accept Leave; | |
Remaining_Space := Remaining_Space + 1; | |
end select | |
end loop | |
end Table; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment