Mercurial > hg > Gears > Gears
annotate src/parallel_execution/examples/DPP2/PhilsImpl.cbc @ 877:e2a0e5a65a3d
...
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 23 Jan 2021 21:02:49 +0900 |
parents | 8c89ee7dd6b3 |
children | 5125f75dd6f2 |
rev | line source |
---|---|
838 | 1 #include "../../../context.h" |
2 #include <stdio.h> | |
848 | 3 #impl "Phils.h" for "PhilsImpl.h" |
838 | 4 #interface "Fork.h" |
5 #interface "Worker.h" | |
840 | 6 #interface "AtomicT_int.h" |
838 | 7 #interface "TaskManager.h" |
8 | |
9 | |
10 // ---- | |
11 // typedef struct PhilsImpl <Self, Isa> impl Phils { | |
12 // __code next(...); | |
13 // atomic Leftfork; | |
14 // atomic Rightfork; | |
15 // int self; | |
16 // } PhilsImpl; | |
17 // ---- | |
18 | |
844 | 19 Phils* createPhilsImpl(struct Context* context, int id, AtomicT_int* right, AtomicT_int* left) { |
838 | 20 struct Phils* phils = new Phils(); |
21 struct PhilsImpl* phils_impl = new PhilsImpl(); | |
22 phils->phils = (union Data*)phils_impl; | |
23 phils_impl->Leftfork = left; | |
24 phils_impl->Rightfork = right; | |
845
feb874340610
use local variable
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
844
diff
changeset
|
25 phils_impl->self = id; |
850 | 26 phils->putdown_lfork = C_putdown_lforkPhilsImpl; |
27 phils->putdown_rfork = C_putdown_rforkPhilsImpl; | |
28 phils->eating = C_eatingPhilsImpl; | |
29 phils->pickup_rfork = C_pickup_rforkPhilsImpl; | |
30 phils->pickup_lfork = C_pickup_lforkPhilsImpl; | |
31 phils->thinking = C_thinkingPhilsImpl; | |
838 | 32 return phils; |
33 } | |
34 | |
35 | |
850 | 36 __code putdown_rfork(struct PhilsImpl* phils, __code next(...)) { |
845
feb874340610
use local variable
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
844
diff
changeset
|
37 struct AtomicT_int* right_fork = phils->Rightfork; |
876 | 38 goto right_fork->set(0, putdown_lfork); |
39 } | |
40 | |
41 __code putdown_lfork(struct PhilsImpl* phils, __code next(...)) { | |
42 struct AtomicT_int* left_fork = phils->Leftfork; | |
43 goto left_fork->set(0, thinking); | |
44 | |
838 | 45 } |
46 | |
850 | 47 __code thinking(struct PhilsImpl* phils, struct Fork* fork, __code next(...)) { |
845
feb874340610
use local variable
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
844
diff
changeset
|
48 printf("%d: thinking\n", phils->self); |
877 | 49 goto pickup_lfork(); |
838 | 50 } |
51 | |
850 | 52 __code pickup_rfork(struct PhilsImpl* phils, __code next(...)) { |
845
feb874340610
use local variable
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
844
diff
changeset
|
53 struct AtomicT_int* right_fork = phils->Rightfork; |
875 | 54 goto right_fork->checkAndSet(-1, 0, pickup_lfork, pickup_rfork); |
838 | 55 } |
56 | |
850 | 57 __code pickup_lfork(struct PhilsImpl* phils, __code next(...)) { |
845
feb874340610
use local variable
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
844
diff
changeset
|
58 struct AtomicT_int* left_fork = phils->Leftfork; |
875 | 59 goto left_fork->checkAndSet(-1, 0, pickup_rfork, eating); |
838 | 60 |
61 } | |
62 | |
850 | 63 __code eating(struct PhilsImpl* phils, __code next(...)) { |
845
feb874340610
use local variable
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
844
diff
changeset
|
64 printf("%d: eating\n", phils->self); |
838 | 65 goto putdown_rfork(); |
66 } | |
67 |