-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiomanager.h
71 lines (57 loc) · 1.56 KB
/
iomanager.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
#ifndef __SYLAR_IO_MANAGER_H__
#define __SYLAR_IO_MANAGER_H__
#include "scheduler.h"
#include <atomic>
#include "timer.h"
namespace sylar {
class IOManager : public Scheduler, public TimerManager {
public:
typedef std::shared_ptr<IOManager> ptr;
typedef RWMutex RWMutexType;
enum Event {
NONE = 0x0,
READ = 0x1,
WRITE = 0x4,
};
private:
struct FdContext {
typedef Mutex MutexType;
struct EventContext {
Scheduler* scheduler = nullptr; //事件执行的scheduler
Fiber::ptr fiber; //事件协程
std::function<void()> cb; //事件的回调函数
};
EventContext& getContext(Event event);
void resetContext(EventContext& ctx);
void triggerEvent(Event event);
EventContext read; //读事件
EventContext write; //写事件
int fd = 0; //事件关联的句柄
Event events = NONE; //已经注册的事件
MutexType mutex;
};
public:
IOManager(size_t threads = 1, bool use_caller = true, const std::string& name = "");
~IOManager();
//1 success, 0 retry, -1 error
int addEvent(int fd, Event event, std::function<void()> cb = nullptr);
void contextResize(size_t size);
bool delEvent(int fd, Event event);
bool cancelEvent(int fd, Event event);
bool canceAll(int fd);
static IOManager* GetThis();
protected:
void tickle() override;
bool stopping() override;
void idle() override;
void onTimerInsetAtFront() override;
bool stopping(uint64_t& next_timeout);
private:
int m_epfd = 0;
int m_tickleFds[2];
std::atomic<size_t> m_pendingEventCount = {0};
RWMutexType m_mutex;
std::vector<FdContext*> m_fdContexts;
};
}
#endif