mirror of
https://github.com/EQEmu/Server.git
synced 2026-06-19 21:08:20 +00:00
Make event loops able to be run in another thread
This commit is contained in:
+10
-10
@@ -7,15 +7,20 @@ namespace EQ
|
||||
{
|
||||
class EventLoop
|
||||
{
|
||||
public:
|
||||
static EventLoop &Get() {
|
||||
static thread_local EventLoop inst;
|
||||
return inst;
|
||||
public:
|
||||
EventLoop() {
|
||||
memset(&m_loop, 0, sizeof(uv_loop_t));
|
||||
uv_loop_init(&m_loop);
|
||||
}
|
||||
|
||||
~EventLoop() {
|
||||
uv_loop_close(&m_loop);
|
||||
}
|
||||
|
||||
static EventLoop &GetDefault() {
|
||||
static EventLoop inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
void Process() {
|
||||
uv_run(&m_loop, UV_RUN_NOWAIT);
|
||||
@@ -23,12 +28,7 @@ namespace EQ
|
||||
|
||||
uv_loop_t* Handle() { return &m_loop; }
|
||||
|
||||
private:
|
||||
EventLoop() {
|
||||
memset(&m_loop, 0, sizeof(uv_loop_t));
|
||||
uv_loop_init(&m_loop);
|
||||
}
|
||||
|
||||
private:
|
||||
EventLoop(const EventLoop&);
|
||||
EventLoop& operator=(const EventLoop&);
|
||||
|
||||
|
||||
+8
-1
@@ -24,7 +24,13 @@ namespace EQ {
|
||||
std::exception error;
|
||||
};
|
||||
|
||||
Task(EQ::EventLoop *loop, TaskFn fn) {
|
||||
m_loop = loop;
|
||||
m_fn = fn;
|
||||
}
|
||||
|
||||
Task(TaskFn fn) {
|
||||
m_loop = &EQ::EventLoop::GetDefault();
|
||||
m_fn = fn;
|
||||
}
|
||||
|
||||
@@ -60,7 +66,7 @@ namespace EQ {
|
||||
|
||||
m_work->data = baton;
|
||||
|
||||
uv_queue_work(EventLoop::Get().Handle(), m_work, [](uv_work_t* req) {
|
||||
uv_queue_work(m_loop->Handle(), m_work, [](uv_work_t* req) {
|
||||
TaskBaton *baton = (TaskBaton*)req->data;
|
||||
|
||||
baton->fn([baton](const EQEmu::Any& result) {
|
||||
@@ -92,6 +98,7 @@ namespace EQ {
|
||||
}
|
||||
|
||||
private:
|
||||
EQ::EventLoop *m_loop;
|
||||
TaskFn m_fn;
|
||||
ResolveFn m_then;
|
||||
RejectFn m_catch;
|
||||
|
||||
+20
-2
@@ -6,14 +6,31 @@ namespace EQ {
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
Timer(std::function<void(Timer *)> cb)
|
||||
Timer(EQ::EventLoop *loop, std::function<void(Timer *)> cb)
|
||||
{
|
||||
m_loop = loop;
|
||||
m_timer = nullptr;
|
||||
m_cb = cb;
|
||||
}
|
||||
|
||||
Timer(std::function<void(Timer *)> cb)
|
||||
{
|
||||
m_loop = &EQ::EventLoop::GetDefault();
|
||||
m_timer = nullptr;
|
||||
m_cb = cb;
|
||||
}
|
||||
|
||||
Timer(EQ::EventLoop *loop, uint64_t duration_ms, bool repeats, std::function<void(Timer *)> cb)
|
||||
{
|
||||
m_loop = loop;
|
||||
m_timer = nullptr;
|
||||
m_cb = cb;
|
||||
Start(duration_ms, repeats);
|
||||
}
|
||||
|
||||
Timer(uint64_t duration_ms, bool repeats, std::function<void(Timer *)> cb)
|
||||
{
|
||||
m_loop = &EQ::EventLoop::GetDefault();
|
||||
m_timer = nullptr;
|
||||
m_cb = cb;
|
||||
Start(duration_ms, repeats);
|
||||
@@ -25,7 +42,7 @@ namespace EQ {
|
||||
}
|
||||
|
||||
void Start(uint64_t duration_ms, bool repeats) {
|
||||
auto loop = EventLoop::Get().Handle();
|
||||
auto loop = m_loop->Handle();
|
||||
if (!m_timer) {
|
||||
m_timer = new uv_timer_t;
|
||||
memset(m_timer, 0, sizeof(uv_timer_t));
|
||||
@@ -61,6 +78,7 @@ namespace EQ {
|
||||
m_cb(this);
|
||||
}
|
||||
|
||||
EQ::EventLoop *m_loop;
|
||||
uv_timer_t *m_timer;
|
||||
std::function<void(Timer*)> m_cb;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user