-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement async send. #27
Open
ImagicTheCat
wants to merge
1
commit into
brimworks:master
Choose a base branch
from
ImagicTheCat:async_work
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -12,6 +12,12 @@ static int luaopen_ev_async(lua_State *L) { | |
lua_pushcfunction(L, async_new); | ||
lua_setfield(L, -2, "new"); | ||
|
||
lua_pushcfunction(L, async_export); | ||
lua_setfield(L, -2, "export"); | ||
|
||
lua_pushcfunction(L, async_import); | ||
lua_setfield(L, -2, "import"); | ||
|
||
return 1; | ||
} | ||
|
||
|
@@ -23,6 +29,7 @@ static int luaopen_ev_async(lua_State *L) { | |
static int create_async_mt(lua_State *L) { | ||
|
||
static luaL_Reg fns[] = { | ||
{ "send", async_send }, | ||
{ "stop", async_stop }, | ||
{ "start", async_start }, | ||
{ NULL, NULL } | ||
|
@@ -50,6 +57,69 @@ static int async_new(lua_State* L) { | |
return 1; | ||
} | ||
|
||
struct async_serialized { | ||
ev_async* async; | ||
struct ev_loop* loop; | ||
}; | ||
|
||
static int async_sz_send(lua_State *L) { | ||
void *udata = lua_touserdata(L, lua_upvalueindex(1)); | ||
struct async_serialized* data = (struct async_serialized*)udata; | ||
|
||
ev_async_send(data->loop, data->async); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Export serialized send for multi-threading signalization (low-level API). | ||
* | ||
* Return string. | ||
* | ||
* Usage: | ||
* string = Async.export(async, loop) | ||
* | ||
* [+1, -0, ?] | ||
*/ | ||
static int async_export(lua_State *L) { | ||
ev_async* async = check_async(L, 1); | ||
struct ev_loop* loop = *check_loop_and_init(L, 2); | ||
|
||
struct async_serialized data; | ||
data.async = async; | ||
data.loop = loop; | ||
lua_pushlstring(L, (const char*)&data, sizeof(struct async_serialized)); | ||
|
||
return 1; | ||
} | ||
|
||
/** | ||
* Import serialized send for multi-threading signalization (low-level API). | ||
* | ||
* Return a function which will act as async:send(loop) when called. | ||
* | ||
* Calling the function beyond the lifespan of either the exported loop or | ||
* async object will result in undefined behavior (crash/corruption). The same | ||
* applies with an ill-formed string. | ||
* | ||
* Usage: | ||
* async_send = Async.import(string) | ||
* | ||
* [+1, -0, ?] | ||
*/ | ||
static int async_import(lua_State *L) { | ||
size_t len; | ||
const char* str = luaL_checklstring(L, 1, &len); | ||
if ( len != sizeof(struct async_serialized) ) | ||
luaL_error(L, "invalid data size"); | ||
|
||
void* data = lua_newuserdata(L, sizeof(struct async_serialized)); | ||
memcpy(data, str, sizeof(struct async_serialized)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can we ensure that the memory pointed to by the fields of the async_serialized structure are still valid? |
||
lua_pushcclosure(L, async_sz_send, 1); | ||
|
||
return 1; | ||
} | ||
|
||
/** | ||
* @see watcher_cb() | ||
* | ||
|
@@ -59,6 +129,24 @@ static void async_cb(struct ev_loop* loop, ev_async* async, int revents) { | |
watcher_cb(loop, async, revents); | ||
} | ||
|
||
/** | ||
* Sends/signals/activates the given "ev_async" watcher, that is, feeds an | ||
* "EV_ASYNC" event on the watcher into the event loop, and instantly returns. | ||
* | ||
* Usage: | ||
* async:send(loop) | ||
* | ||
* [+0, -0, e] | ||
*/ | ||
static int async_send(lua_State *L) { | ||
ev_async* async = check_async(L, 1); | ||
struct ev_loop* loop = *check_loop_and_init(L, 2); | ||
|
||
ev_async_send(loop, async); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Stops the async so it won't be called by the specified event loop. | ||
* | ||
|
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,40 @@ | ||
print '1..20' | ||
|
||
local src_dir, build_dir = ... | ||
package.path = src_dir .. "?.lua;" .. package.path | ||
package.cpath = build_dir .. "?.so;" .. package.cpath | ||
|
||
local tap = require("tap") | ||
local ev = require("ev") | ||
local help = require("help") | ||
local dump = require("dumper").dump | ||
local ok = tap.ok | ||
|
||
local noleaks = help.collect_and_assert_no_watchers | ||
local loop = ev.Loop.default | ||
|
||
function test_basic() | ||
local async1 = ev.Async.new( | ||
function(loop, async, revents) | ||
ok(true, 'async callback') | ||
ok(ev.ASYNC == revents, 'ev.ASYNC(' .. ev.ASYNC .. ') == revents (' .. revents .. ')') | ||
end) | ||
async1:start(loop) | ||
async1:send(loop) | ||
loop:loop() | ||
end | ||
|
||
function test_export() | ||
local async1 = ev.Async.new( | ||
function(loop, async, revents) | ||
ok(true, 'async callback') | ||
ok(ev.ASYNC == revents, 'ev.ASYNC(' .. ev.ASYNC .. ') == revents (' .. revents .. ')') | ||
end) | ||
local send = ev.Async.import(ev.Async.export(async1, loop)) | ||
async1:start(loop) | ||
send() | ||
loop:loop() | ||
end | ||
|
||
noleaks(test_basic, "test_basic") | ||
noleaks(test_basic, "test_export") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we ensure the lifetime of the
data->async
is still valid when this is called?