Repository navigation
Expand file tree
/
Copy pathHxcppLuvAsync.cpp
More file actions
60 lines (48 loc) · 1.41 KB
/
Copy pathHxcppLuvAsync.cpp
File metadata and controls
60 lines (48 loc) · 1.41 KB
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
#include <hxcpp.h>
#include "HxcppLuvAsync.hpp"
#include "AutoGCZone.hpp"
uv_async_t* cpp::luv::async::init(uv_loop_t* loop, Dynamic callback)
{
auto wrapper = [](uv_async_t* response) {
auto gcZone = cpp::utils::AutoGCZone();
auto object = static_cast<hx::Object**>(response->data);
auto cb = Dynamic(*object);
cb();
};
auto async = new uv_async_t();
auto root = new hx::Object*(callback.mPtr);
async->data = root;
auto result = 0;
if ((result = uv_async_init(loop, async, wrapper)) < 0)
{
hx::Throw(String::create(uv_strerror(result)));
}
hx::GCAddRoot(root);
return async;
}
int cpp::luv::async::send(uv_async_t* async)
{
return uv_async_send(async);
}
void cpp::luv::async::close(uv_async_t* async)
{
uv_close(reinterpret_cast<uv_handle_t*>(async), [](uv_handle_t* handle) {
auto data = static_cast<hx::Object**>(handle->data);
// Removing the root touches GC memory, which must not happen from a GC free zone.
auto left = hx::TryExitGCFreeZone();
hx::GCRemoveRoot(data);
if (left) {
hx::EnterGCFreeZone();
}
delete data;
delete handle;
});
}
void cpp::luv::async::ref(uv_async_t* async)
{
uv_ref(reinterpret_cast<uv_handle_t*>(async));
}
void cpp::luv::async::unref(uv_async_t* async)
{
uv_unref(reinterpret_cast<uv_handle_t*>(async));
}