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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include "../../../context.h"
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #include <stdio.h>
848
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 847
diff changeset
3 #impl "Phils.h" for "PhilsImpl.h"
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 #interface "Fork.h"
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #interface "Worker.h"
840
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 838
diff changeset
6 #interface "AtomicT_int.h"
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 #interface "TaskManager.h"
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // ----
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 // typedef struct PhilsImpl <Self, Isa> impl Phils {
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 // __code next(...);
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 // atomic Leftfork;
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 // atomic Rightfork;
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 // int self;
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 // } PhilsImpl;
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 // ----
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
844
49408bce514c use pointer
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 843
diff changeset
19 Phils* createPhilsImpl(struct Context* context, int id, AtomicT_int* right, AtomicT_int* left) {
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 struct Phils* phils = new Phils();
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 struct PhilsImpl* phils_impl = new PhilsImpl();
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 phils->phils = (union Data*)phils_impl;
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 phils_impl->Leftfork = left;
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
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
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
26 phils->putdown_lfork = C_putdown_lforkPhilsImpl;
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
27 phils->putdown_rfork = C_putdown_rforkPhilsImpl;
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
28 phils->eating = C_eatingPhilsImpl;
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
29 phils->pickup_rfork = C_pickup_rforkPhilsImpl;
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
30 phils->pickup_lfork = C_pickup_lforkPhilsImpl;
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
31 phils->thinking = C_thinkingPhilsImpl;
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 return phils;
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 }
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
850
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
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
8c89ee7dd6b3 fix putdown
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 875
diff changeset
38 goto right_fork->set(0, putdown_lfork);
8c89ee7dd6b3 fix putdown
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 875
diff changeset
39 }
8c89ee7dd6b3 fix putdown
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 875
diff changeset
40
8c89ee7dd6b3 fix putdown
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 875
diff changeset
41 __code putdown_lfork(struct PhilsImpl* phils, __code next(...)) {
8c89ee7dd6b3 fix putdown
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 875
diff changeset
42 struct AtomicT_int* left_fork = phils->Leftfork;
8c89ee7dd6b3 fix putdown
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 875
diff changeset
43 goto left_fork->set(0, thinking);
8c89ee7dd6b3 fix putdown
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 875
diff changeset
44
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 }
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
850
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
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
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 876
diff changeset
49 goto pickup_lfork();
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 }
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
850
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
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
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 850
diff changeset
54 goto right_fork->checkAndSet(-1, 0, pickup_lfork, pickup_rfork);
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 }
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
850
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
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
ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
parents: 850
diff changeset
59 goto left_fork->checkAndSet(-1, 0, pickup_rfork, eating);
838
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 }
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
850
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 849
diff changeset
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
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 goto putdown_rfork();
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 }
8bd2d32ff8e7 add DPP2
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67