-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.h
185 lines (163 loc) · 3.61 KB
/
scheduler.h
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
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifndef _CU_SCHEDULER_H_
#define _CU_SCHEDULER_H_
#include <map>
#include <teelogging/teelogging.h>
#include "cpproutine.h"
#include <asyncply/run.h>
#include <asyncply/algorithm.h>
#include <fast-event-system/sync.h>
// #include <mqtt/async_client.h>
namespace asyncply
{
template <typename TOKEN>
struct is_complete
{
bool operator()(TOKEN token) const
{
return token->is_ready();
}
};
// template <>
// struct is_complete<mqtt::token_ptr>
// {
// bool operator()(mqtt::token_ptr token) const
// {
// return token->is_complete();
// }
// };
//
// template <>
// struct is_complete<mqtt::delivery_token_ptr>
// {
// bool operator()(mqtt::delivery_token_ptr token) const
// {
// return token->is_complete();
// }
// };
}
namespace cu {
template <typename T>
static void sleep(cu::yield_type& yield, T time)
{
auto timeout = fes::high_resolution_clock() + fes::deltatime(time);
while(fes::high_resolution_clock() <= timeout)
{
yield( cu::control_type{} );
}
}
template <typename TOKEN>
static void await(cu::yield_type& yield, TOKEN token)
{
while(!asyncply::is_complete<TOKEN>{}(token))
{
yield( cu::control_type{} );
}
}
// implementar ejecutar corutina "atexit" al salir
class scheduler : public scheduler_basic
{
public:
explicit scheduler()
: _pid_counter(0)
, _active(nullptr)
{
;
}
virtual ~scheduler()
{
;
}
template <typename Function>
void spawn(Function&& func)
{
_running.emplace_back(std::make_unique<cpproutine>("anonymous", _pid_counter, std::forward<Function>(func)));
++_pid_counter;
}
template <typename Function>
void spawn(std::string name, Function&& func)
{
_running.emplace_back(std::make_unique<cpproutine>(std::move(name), _pid_counter, std::forward<Function>(func)));
++_pid_counter;
}
void run_until_complete()
{
while(ready())
{
run();
}
if(_blocked.size() > 0)
{
std::stringstream ss;
ss << "fatal error: all cpproutines are asleep" << std::endl;
throw std::runtime_error(ss.str());
}
}
void run_forever()
{
while(true)
{
run();
}
}
std::string get_name() const override final
{
return _active->get_name();
}
pid_type getpid() const override final
{
return _active->getpid();
}
void wait(int id)
{
_move_to_blocked = true;
_last_id = id;
}
bool notify_one(int id)
{
auto& blocked = _blocked[id];
if(blocked.size() > 0)
{
LOGV("<%s> begin resuming", get_name().c_str());
LOGV("%s se desbloquea porque ha sido despertado por la señal %d", (*blocked.begin())->get_name().c_str(), id);
_running.emplace(_running.end(), std::move(*blocked.begin()));
blocked.erase(blocked.begin());
if(blocked.size() == 0)
{
_blocked.erase(id);
}
LOGV("<%s> end resuming", get_name().c_str());
return true;
}
return false;
}
bool notify_all(int id)
{
auto& blocked = _blocked[id];
bool notified_any = false;
while(blocked.size() > 0)
{
LOGV("<%s> begin resuming", get_name().c_str());
LOGV("%s se desbloquea porque ha sido despertado por la señal %d", (*blocked.begin())->get_name().c_str(), id);
_running.emplace(_running.end(), std::move(*blocked.begin()));
blocked.erase(blocked.begin());
if(blocked.size() == 0)
{
_blocked.erase(id);
}
notified_any = true;
LOGV("<%s> end resuming", get_name().c_str());
}
return notified_any;
}
protected:
scheduler_basic* _active;
// normal running
std::vector<std::unique_ptr<scheduler_basic> > _running;
// cpproutines waiting for pid
std::map<int, std::vector<std::unique_ptr<scheduler_basic> > > _blocked;
pid_type _pid_counter;
bool _move_to_blocked;
int _last_id;
};
}
#endif