forked from stevedonovan/AndroLua
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init.lua: change method for print() from coroutines
- Loading branch information
Ildar Mulyukov
committed
Jun 30, 2020
1 parent
cf71adf
commit 7f477c4
Showing
3 changed files
with
42 additions
and
129 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-- Helper module for changing call context from a coroutine to the main thread | ||
|
||
local mainthread_stash = {} | ||
|
||
local function mainthread_call(f, ...) | ||
local thread = coroutine.running() | ||
assert(thread, "cannot call from the main thread") | ||
mainthread_stash[thread] = mainthread_stash[thread] or {} | ||
mainthread_stash[thread].status = nil | ||
mainthread_stash[thread].f = f | ||
mainthread_stash[thread].args = {...} | ||
while mainthread_stash[thread].status == nil do | ||
coroutine.yield() | ||
end | ||
return mainthread_stash[thread].status, mainthread_stash[thread].res | ||
end | ||
|
||
local function mainthread_process() | ||
local co, main = coroutine.running() | ||
assert( main or co == nil, "cannot call from a coroutine") | ||
for thread,_ in pairs(mainthread_stash) do | ||
if mainthread_stash[thread].status == nil then | ||
mainthread_stash[thread].status, mainthread_stash[thread].res = | ||
pcall( mainthread_stash[thread].f, unpack(mainthread_stash[thread].args) ) | ||
end | ||
end | ||
end | ||
|
||
return { | ||
mainthread_call = mainthread_call, | ||
mainthread_process = mainthread_process, | ||
} |
This file was deleted.
Oops, something went wrong.
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