summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-02-04 00:58:36 +0800
committerruki <[email protected]>2020-02-07 22:45:56 +0800
commit0c3f6069c16dbc7b6612fb792cbe6b990a7acef2 (patch)
treedde9571fe643e4b70e4a52ba68762e815757655c
parentff45e17b26152544961e52671d61aa5f8e838d38 (diff)
remove scheduler.runloop in outside
-rw-r--r--tests/modules/process/test.lua5
-rw-r--r--tests/modules/scheduler/test.lua10
-rw-r--r--tests/modules/socket/sched_tcp/echo_client.lua5
-rw-r--r--xmake/core/base/scheduler.lua32
-rw-r--r--xmake/core/sandbox/modules/import/core/base/scheduler.lua30
5 files changed, 55 insertions, 27 deletions
diff --git a/tests/modules/process/test.lua b/tests/modules/process/test.lua
index 15a1ef946..30a42fd99 100644
--- a/tests/modules/process/test.lua
+++ b/tests/modules/process/test.lua
@@ -25,9 +25,10 @@ function test_sched_process(t)
stderr:close()
count = count + 1
end
+ local cotasks = {}
for i = 1, 3 do
- scheduler.co_start(_session)
+ table.insert(cotasks, scheduler.co_start(_session))
end
- scheduler.runloop()
+ scheduler.co_waitexit(cotasks)
t:are_equal(count, 3)
end
diff --git a/tests/modules/scheduler/test.lua b/tests/modules/scheduler/test.lua
index 07af3dd80..d6e090c6a 100644
--- a/tests/modules/scheduler/test.lua
+++ b/tests/modules/scheduler/test.lua
@@ -7,10 +7,11 @@ function test_runjobs(t)
t:are_equal(a, "xmake!")
count = count + 1
end
+ local cotasks = {}
for i = 1, 100 do
- scheduler.co_start(task, "xmake!")
+ table.insert(cotasks, scheduler.co_start(task, "xmake!"))
end
- scheduler.runloop()
+ scheduler.co_waitexit(cotasks)
t:are_equal(count, 100)
end
@@ -36,9 +37,10 @@ function test_yield(t)
scheduler.co_yield()
count = count + 1
end
+ local cotasks = {}
for i = 1, 10 do
- scheduler.co_start(task)
+ table.insert(cotasks, scheduler.co_start(task))
end
- scheduler.runloop()
+ scheduler.co_waitexit(cotasks)
t:are_equal(count, 10)
end
diff --git a/tests/modules/socket/sched_tcp/echo_client.lua b/tests/modules/socket/sched_tcp/echo_client.lua
index 6dfd92439..acc681584 100644
--- a/tests/modules/socket/sched_tcp/echo_client.lua
+++ b/tests/modules/socket/sched_tcp/echo_client.lua
@@ -50,11 +50,12 @@ function _session(addr, port)
end
function main(count)
+ local cotasks = {}
count = count and tonumber(count) or 1
for i = 1, count do
- scheduler.co_start(_session, "127.0.0.1", 9001)
+ table.insert(cotasks, scheduler.co_start(_session, "127.0.0.1", 9001))
end
- scheduler.runloop()
+ scheduler.co_waitexit(cotasks)
for _, sock in ipairs(socks) do
sock:close()
end
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua
index 1c6db570d..14dffce4a 100644
--- a/xmake/core/base/scheduler.lua
+++ b/xmake/core/base/scheduler.lua
@@ -355,6 +355,38 @@ function scheduler:co_sleep(ms)
return true
end
+-- wait for exiting the given coroutine tasks
+function scheduler:co_waitexit(cotasks)
+
+ -- get the running coroutine
+ local running = self:co_running()
+ if not running then
+ return false, "we must call waitexit() in coroutine with scheduler!"
+ end
+
+ -- is stopped?
+ if not self._STARTED then
+ return false, "the scheduler is stopped!"
+ end
+
+ -- wait it
+ cotasks = table.copy(table.wrap(cotasks))
+ while #cotasks > 0 do
+ for i = #cotasks, 1, -1 do
+ local co = cotasks[i]
+ if co:is_dead() then
+ table.remove(cotasks, i)
+ else
+ local ok, errors = self:co_yield()
+ if not ok then
+ return false, errors
+ end
+ end
+ end
+ end
+ return true
+end
+
-- get the current running coroutine
function scheduler:co_running()
local running = coroutine.running()
diff --git a/xmake/core/sandbox/modules/import/core/base/scheduler.lua b/xmake/core/sandbox/modules/import/core/base/scheduler.lua
index 3476c9333..62bfcbb5c 100644
--- a/xmake/core/sandbox/modules/import/core/base/scheduler.lua
+++ b/xmake/core/sandbox/modules/import/core/base/scheduler.lua
@@ -61,16 +61,6 @@ function sandbox_core_base_scheduler.co_yield()
end
end
--- get the current running coroutine
-function sandbox_core_base_scheduler.co_running()
- return scheduler:co_running()
-end
-
--- get the all coroutine task count
-function sandbox_core_base_scheduler.co_count()
- return scheduler:co_count()
-end
-
-- sleep some times (ms)
function sandbox_core_base_scheduler.co_sleep(ms)
local ok, errors = scheduler:co_sleep(ms)
@@ -79,20 +69,22 @@ function sandbox_core_base_scheduler.co_sleep(ms)
end
end
--- stop loop
-function sandbox_core_base_scheduler.stop()
- local ok, errors = scheduler:stop()
+-- wait for exiting the given coroutine tasks
+function sandbox_core_base_scheduler.co_waitexit(cotasks)
+ local ok, errors = scheduler:co_waitexit(cotasks)
if not ok then
raise(errors)
end
end
--- run loop
-function sandbox_core_base_scheduler.runloop()
- local ok, errors = scheduler:runloop()
- if not ok then
- raise(errors)
- end
+-- get the current running coroutine
+function sandbox_core_base_scheduler.co_running()
+ return scheduler:co_running()
+end
+
+-- get the all coroutine task count
+function sandbox_core_base_scheduler.co_count()
+ return scheduler:co_count()
end
-- return module