Skip to content

Commit 093285d

Browse files
author
yuyangzi
committed
channel的close语义调整: 当channel中仍有数据时, close后依然可以读取
1 parent 7ec8487 commit 093285d

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

libgo/routine_sync/channel.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class ChannelImplWithSignal : public DebuggerId<ChannelImplWithSignal<int>>
9797
return pop_impl_with_signal_noqueued(t, isWait, abstime, lock);
9898
}
9999

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

384-
if (closed_)
385-
return false;
386-
387387
if (!q_.empty()) {
388388
t = q_.front();
389389
q_.pop_front();
@@ -395,6 +395,9 @@ class ChannelImpl : public ChannelImplWithSignal<T>
395395
return true;
396396
}
397397

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

589-
if (closed_)
590-
return false;
591-
592592
if (count_ > 0) {
593593
--count_;
594594
pushCv_.notify_one();
@@ -598,6 +598,9 @@ class ChannelImpl<nullptr_t, QueueT> : public ChannelImplWithSignal<nullptr_t>
598598
return true;
599599
}
600600

601+
if (closed_)
602+
return false;
603+
601604
if (!isWait) {
602605
RS_DBG(dbg_channel, "channel(void)=%ld | %s | not match && not wait | return false",
603606
id(), __func__);

0 commit comments

Comments
 (0)