diff --git a/ostd/channel.hh b/ostd/channel.hh index bd90182..bee7323 100644 --- a/ostd/channel.hh +++ b/ostd/channel.hh @@ -70,15 +70,17 @@ private: impl() {} template - impl(F &func): p_lock(), p_cond(func(p_lock)) {} + impl(F &func): p_lock(), p_cond(func()) {} template void put(U &&val) { - std::lock_guard l{p_lock}; - if (p_closed) { - throw channel_error{"put in a closed channel"}; + { + std::lock_guard l{p_lock}; + if (p_closed) { + throw channel_error{"put in a closed channel"}; + } + p_messages.push_back(std::forward(val)); } - p_messages.push_back(std::forward(val)); p_cond.notify_one(); } @@ -106,8 +108,10 @@ private: } void close() { - std::lock_guard l{p_lock}; - p_closed = true; + { + std::lock_guard l{p_lock}; + p_closed = true; + } p_cond.notify_all(); } diff --git a/ostd/concurrency.hh b/ostd/concurrency.hh index e95c532..8d14592 100644 --- a/ostd/concurrency.hh +++ b/ostd/concurrency.hh @@ -88,9 +88,7 @@ private: coro_cond &operator=(coro_cond const &) = delete; coro_cond &operator=(coro_cond &&) = delete; - coro_cond(basic_simple_coroutine_scheduler &s, std::mutex &mtx): - p_sched(s), p_mtx(mtx) - {} + coro_cond(basic_simple_coroutine_scheduler &s): p_sched(s) {} template void wait(L &l) noexcept { @@ -104,20 +102,15 @@ private: void notify_one() noexcept { p_notified = true; - p_mtx.unlock(); p_sched.yield(); - p_mtx.lock(); } void notify_all() noexcept { p_notified = true; - p_mtx.unlock(); p_sched.yield(); - p_mtx.lock(); } private: basic_simple_coroutine_scheduler &p_sched; - std::mutex &p_mtx; bool p_notified = false; }; @@ -182,8 +175,8 @@ public: template channel make_channel() { - return channel{[this](std::mutex &mtx) { - return coro_cond{*this, mtx}; + return channel{[this]() { + return coro_cond{*this}; }}; } private: