Skip to content

Commit c54e1cc

Browse files
niaowaykevl
authored andcommitted
sync: modify sync.Cond
1 parent 7801921 commit c54e1cc

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/sync/cond.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func (c *Cond) Wait() {
5454

5555
// Temporarily unlock L.
5656
c.L.Unlock()
57+
58+
// Re-acquire the lock before returning.
5759
defer c.L.Lock()
5860

5961
// If we were signaled while unlocking, immediately complete.
@@ -62,16 +64,14 @@ func (c *Cond) Wait() {
6264
}
6365

6466
// Remove the earlySignal frame.
65-
if c.unlocking == &early {
66-
c.unlocking = early.next
67-
} else {
68-
// Something else happened after the unlock - the earlySignal is somewhere in the middle.
69-
// This would be faster but less space-efficient if it were a doubly linked list.
70-
prev := c.unlocking
71-
for prev.next != &early {
72-
prev = prev.next
73-
}
67+
prev := c.unlocking
68+
for prev != nil && prev.next != &early {
69+
prev = prev.next
70+
}
71+
if prev != nil {
7472
prev.next = early.next
73+
} else {
74+
c.unlocking = early.next
7575
}
7676

7777
// Wait for a signal.

0 commit comments

Comments
 (0)