-
Notifications
You must be signed in to change notification settings - Fork 997
Implement sync
#881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement sync
#881
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ceed751
implement mutex blocking
niaow 92b41b0
add sync.Cond
niaow 1091665
internal/task: fix nil panic in (*internal/task.Stack).Pop
niaow d9cdadf
sync: add WaitGroup
niaow a64ba4c
testdata: replace fake waitgroup in channel.go with sync.WaitGroup
niaow 49ea7d6
sync: modify sync.Cond
niaow 7b7e9f7
testdata, sync: add sync.Mutex test to testdata/coroutines.go
niaow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package sync | ||
|
|
||
| import "internal/task" | ||
|
|
||
| type Cond struct { | ||
| L Locker | ||
|
|
||
| unlocking *earlySignal | ||
| blocked task.Stack | ||
| } | ||
|
|
||
| // earlySignal is a type used to implement a stack for signalling waiters while they are unlocking. | ||
| type earlySignal struct { | ||
| next *earlySignal | ||
|
|
||
| signaled bool | ||
| } | ||
|
|
||
| func (c *Cond) trySignal() bool { | ||
| // Pop a blocked task off of the stack, and schedule it if applicable. | ||
| t := c.blocked.Pop() | ||
| if t != nil { | ||
| scheduleTask(t) | ||
| return true | ||
| } | ||
|
|
||
| // If there any tasks which are currently unlocking, signal one. | ||
| if c.unlocking != nil { | ||
| c.unlocking.signaled = true | ||
| c.unlocking = c.unlocking.next | ||
| return true | ||
| } | ||
|
|
||
| // There was nothing to signal. | ||
| return false | ||
| } | ||
|
|
||
| func (c *Cond) Signal() { | ||
| c.trySignal() | ||
| } | ||
|
|
||
| func (c *Cond) Broadcast() { | ||
| // Signal everything. | ||
| for c.trySignal() { | ||
| } | ||
| } | ||
|
|
||
| func (c *Cond) Wait() { | ||
| // Add an earlySignal frame to the stack so we can be signalled while unlocking. | ||
| early := earlySignal{ | ||
| next: c.unlocking, | ||
| } | ||
| c.unlocking = &early | ||
|
|
||
| // Temporarily unlock L. | ||
| c.L.Unlock() | ||
|
|
||
| // Re-acquire the lock before returning. | ||
| defer c.L.Lock() | ||
niaow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // If we were signaled while unlocking, immediately complete. | ||
| if early.signaled { | ||
| return | ||
| } | ||
|
|
||
| // Remove the earlySignal frame. | ||
| prev := c.unlocking | ||
| for prev != nil && prev.next != &early { | ||
| prev = prev.next | ||
| } | ||
| if prev != nil { | ||
| prev.next = early.next | ||
| } else { | ||
| c.unlocking = early.next | ||
| } | ||
|
|
||
| // Wait for a signal. | ||
| c.blocked.Push(task.Current()) | ||
| task.Pause() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package sync | ||
|
|
||
| import "internal/task" | ||
|
|
||
| type WaitGroup struct { | ||
| counter uint | ||
| waiters task.Stack | ||
| } | ||
|
|
||
| func (wg *WaitGroup) Add(delta int) { | ||
| if delta > 0 { | ||
| // Check for overflow. | ||
| if uint(delta) > (^uint(0))-wg.counter { | ||
| panic("sync: WaitGroup counter overflowed") | ||
| } | ||
|
|
||
| // Add to the counter. | ||
| wg.counter += uint(delta) | ||
| } else { | ||
| // Check for underflow. | ||
| if uint(-delta) > wg.counter { | ||
| panic("sync: negative WaitGroup counter") | ||
| } | ||
|
|
||
| // Subtract from the counter. | ||
| wg.counter -= uint(-delta) | ||
|
|
||
| // If the counter is zero, everything is done and the waiters should be resumed. | ||
| // This code assumes that the waiters cannot wake up until after this function returns. | ||
| // In the current implementation, this is always correct. | ||
| if wg.counter == 0 { | ||
| for t := wg.waiters.Pop(); t != nil; t = wg.waiters.Pop() { | ||
| scheduleTask(t) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (wg *WaitGroup) Done() { | ||
| wg.Add(-1) | ||
| } | ||
|
|
||
| func (wg *WaitGroup) Wait() { | ||
| if wg.counter == 0 { | ||
| // Everything already finished. | ||
| return | ||
| } | ||
|
|
||
| // Push the current goroutine onto the waiter stack. | ||
| wg.waiters.Push(task.Current()) | ||
|
|
||
| // Pause until the waiters are awoken by Add/Done. | ||
| task.Pause() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.