Fix race condition in channel (#112)

* Fix race condition in channel

* add test
This commit is contained in:
mihacooper 2018-11-11 21:05:26 +03:00 committed by Ilia
parent e1d2544995
commit 51552e5f01
2 changed files with 31 additions and 2 deletions

View File

@ -43,9 +43,8 @@ bool Channel::push(const sol::variadic_args& args) {
}
RETHROW_WITH_PREFIX("effil.channel:push");
}
if (ctx_->channel_.empty())
ctx_->cv_.notify_one();
ctx_->channel_.emplace(array);
ctx_->cv_.notify_one();
return true;
}

View File

@ -1,5 +1,7 @@
require "bootstrap-tests"
local effil = require "effil"
test.channel_stress.tear_down = default_tear_down
test.channel_stress.with_multiple_threads = function ()
@ -90,3 +92,31 @@ test.channel_stress.retun_tables = function ()
thr:wait()
end
end
-- regress for multiple wait on channel
test.channel_stress.regress_for_multiple_waiters = function ()
for i = 1, 20 do
local chan = effil.channel()
local function receiver()
return chan:pop(5) ~= nil
end
local threads = {}
for i = 1, 10 do
table.insert(threads, effil.thread(receiver)())
end
effil.sleep(0.1)
for i = 1, 100 do
chan:push(1)
end
for _, thr in ipairs(threads) do
local ret = thr:get()
test.is_true(ret)
if not ret then
return
end
end
end
end