-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpproutine.h
66 lines (52 loc) · 1.03 KB
/
cpproutine.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
#ifndef _CU_CPROUTINE_H_
#define _CU_CPROUTINE_H_
#include <coroutine/coroutine.h>
namespace cu {
class scheduler_basic
{
public:
virtual ~scheduler_basic() { ; }
virtual bool run() = 0;
virtual bool ready() const = 0;
virtual std::string get_name() const = 0;
virtual pid_type getpid() const = 0;
};
class cpproutine : public scheduler_basic
{
public:
template <typename Function>
explicit cpproutine(const std::string& name, pid_type pid, Function&& func)
: _name(name)
, _pid(pid)
, _coroutine(cu::make_generator<control_type>(
[f = std::move(func)](auto& yield) {
yield( cu::control_type{} );
f(yield);
}
))
{ ; }
virtual ~cpproutine() { ; }
std::string get_name() const override final
{
return _name;
}
bool ready() const override final
{
return bool(*_coroutine);
}
bool run() override final
{
(*_coroutine)();
return true;
}
int getpid() const override final
{
return _pid;
}
protected:
std::string _name;
pid_type _pid;
cu::pull_type_ptr<control_type> _coroutine;
};
}
#endif