-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ex3.cxx
172 lines (148 loc) · 3.41 KB
/
ex3.cxx
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// TEST CASE
#include "sync3.hpp"
#include <list>
struct producer : con_t {
::std::list<int> *plst;
::std::list<int>::iterator it;
channel_t *chan;
union {
void *iodata;
int value;
};
io_request_t w_req;
con_t *call(
con_t *caller_a,
::std::list<int> *plst_a,
channel_t *outchan)
{
caller = caller_a;
plst = plst_a;
pc = 0;
w_req.chan = outchan;
return this;
}
con_t *resume() override {
switch (pc) {
case 0:
it = plst->begin();
pc = 1;
w_req.svc_code = write_request_code_e;
w_req.pdata = &iodata;
return this;
case 1:
if(it == plst->end()) {
auto tmp = caller;
delete this;
return caller;
}
value = *it++;
svc_req = (svc_req_t*)(void*)&w_req; // service request
return this;
default: assert(false);
}
}
};
struct consumer: con_t {
::std::list<int> *plst;
union {
void *iodata;
int value;
};
io_request_t r_req;
con_t *call(
con_t *caller_a,
::std::list<int> *plst_a,
channel_t *inchan_a)
{
caller = caller_a;
plst = plst_a;
r_req.chan = inchan_a;
pc = 0;
return this;
}
con_t *resume() override {
switch (pc) {
case 0:
pc = 1;
r_req.svc_code = read_request_code_e;
r_req.pdata = &iodata;
return this;
case 1:
svc_req = (svc_req_t*)(void*)&r_req; // service request
pc = 2;
return this;
case 2:
plst->push_back(value);
pc = 1;
return this;
default: assert(false);
}
}
};
struct transducer: con_t {
union {
void *iodata;
int value;
};
io_request_t r_req;
io_request_t w_req;
con_t *call(
con_t *caller_a,
channel_t *inchan_a,
channel_t *outchan_a)
{
caller = caller_a;
r_req.chan = inchan_a;
w_req.chan = outchan_a;
pc = 0;
return this;
}
con_t *resume() override {
switch (pc) {
case 0:
pc = 1;
r_req.svc_code = read_request_code_e;
r_req.pdata = &iodata;
w_req.svc_code = write_request_code_e;
w_req.pdata = &iodata;
return this;
case 1:
svc_req = (svc_req_t*)(void*)&r_req; // service request
pc = 2;
return this;
case 2:
value = value * value; // square value
svc_req = (svc_req_t*)(void*)&w_req; // service request
pc = 1;
return this;
default: assert(false);
}
}
};
#include <iostream>
int main() {
// create the input list
::std::list<int> inlst;
for (auto i = 0; i < 20; ++i) inlst.push_back(i);
// output list
::std::list<int> outlst;
// create scheduler
sync_sched sched;
// create channels
channel_t chan1;
channel_t chan2;
// create fibres
fibre_t *prod = new fibre_t ((new producer)->call(nullptr, &inlst, &chan1));
fibre_t *trans = new fibre_t ((new transducer)->call(nullptr, &chan1, &chan2));
fibre_t *cons = new fibre_t ((new consumer)->call(nullptr, &outlst, &chan2));
// push initial fibres onto scheduler active list
sched.active_set->push_new(prod);
sched.active_set->push_new(trans);
sched.active_set->push_new(cons);
// run it
sched.sync_run();
::std::fflush(stdout);
// the result is now in the outlist so print it
::std::cout<< "List of squares:" << ::std::endl;
for(auto v : outlst) ::std::cout << v << ::std::endl;
}