Skip to content

Commit

Permalink
channel的close语义调整: 当channel中仍有数据时, close后依然可以读取
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyangzi committed Mar 9, 2023
1 parent 7ec8487 commit 093285d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions libgo/routine_sync/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class ChannelImplWithSignal : public DebuggerId<ChannelImplWithSignal<int>>
return pop_impl_with_signal_noqueued(t, isWait, abstime, lock);
}

if (closed_)
return false;

if (!isWait) {
RS_DBG(dbg_channel, "channel=%ld | %s | pop contended && not wait | return false",
id(), __func__);
Expand Down Expand Up @@ -381,9 +384,6 @@ class ChannelImpl : public ChannelImplWithSignal<T>
RS_DBG(dbg_channel, "channel(queue)=%ld | %s | ptr(t)=0x%p | isWait=%d | abstime=%d | closed=%d | cap=%lu | size=%lu",
id(), __func__, (void*)&t, isWait, !!abstime, closed_, cap_, q_.size());

if (closed_)
return false;

if (!q_.empty()) {
t = q_.front();
q_.pop_front();
Expand All @@ -395,6 +395,9 @@ class ChannelImpl : public ChannelImplWithSignal<T>
return true;
}

if (closed_)
return false;

if (!isWait) {
RS_DBG(dbg_channel, "channel(queue)=%ld | %s | not match && not wait | return false",
id(), __func__);
Expand Down Expand Up @@ -586,9 +589,6 @@ class ChannelImpl<nullptr_t, QueueT> : public ChannelImplWithSignal<nullptr_t>
RS_DBG(dbg_channel, "channel(void)=%ld | %s | isWait=%d | abstime=%d | closed=%d | cap=%lu | size=%lu",
id(), __func__, isWait, !!abstime, closed_, cap_, count_);

if (closed_)
return false;

if (count_ > 0) {
--count_;
pushCv_.notify_one();
Expand All @@ -598,6 +598,9 @@ class ChannelImpl<nullptr_t, QueueT> : public ChannelImplWithSignal<nullptr_t>
return true;
}

if (closed_)
return false;

if (!isWait) {
RS_DBG(dbg_channel, "channel(void)=%ld | %s | not match && not wait | return false",
id(), __func__);
Expand Down

0 comments on commit 093285d

Please sign in to comment.