-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #607 from evo-lua/uws-loop-global
Streamline the shared event loop code in preparation for future changes
- Loading branch information
Showing
4 changed files
with
62 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include "LuaVirtualMachine.hpp" | ||
#include "uws_ffi.hpp" | ||
|
||
#include <cassert> | ||
#include <format> | ||
#include <memory> | ||
#include <stdexcept> | ||
|
||
using namespace std; | ||
using uws_loop_t = uWS::Loop*; | ||
|
||
extern "C" { | ||
#include "luv.h" | ||
#include "uv.h" | ||
} | ||
|
||
class SharedEventLoop { | ||
private: | ||
shared_ptr<LuaVirtualMachine> m_mainThreadVM; | ||
|
||
uv_loop_t m_uvMainLoop; | ||
uws_loop_t m_uwsMainLoop; | ||
|
||
public: | ||
explicit SharedEventLoop(auto L) { | ||
assert(L != nullptr); | ||
m_mainThreadVM = L; | ||
|
||
int errorCode = uv_loop_init(&m_uvMainLoop); | ||
if(errorCode != 0) { | ||
auto message = format("Failed to initialize shared event loop ({}: {})", | ||
uv_err_name(errorCode), uv_strerror(errorCode)); | ||
throw runtime_error(message); | ||
} | ||
|
||
luv_set_loop(m_mainThreadVM->GetState(), &m_uvMainLoop); | ||
|
||
auto uwsEventLoop = uws_ffi::assignEventLoop(&m_uvMainLoop); | ||
assert(uwsEventLoop != nullptr); | ||
m_uwsMainLoop = uwsEventLoop; | ||
} | ||
|
||
~SharedEventLoop() { | ||
luv_set_loop(m_mainThreadVM->GetState(), nullptr); | ||
uws_ffi::unassignEventLoop(m_uwsMainLoop); | ||
} | ||
|
||
void RunMainLoopUntilDone() { | ||
// There's just a single main loop right now, but that'll probably need to change | ||
int refCount = uv_run(&m_uvMainLoop, UV_RUN_DEFAULT); | ||
if(refCount != 0) { | ||
auto message = format("Main loop finished running, but there's {} active references", refCount); | ||
throw runtime_error(message); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters