-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The mutex implementation needs a different implementation once support for threading lands. This implementation just moves code to the internal/task package to centralize these algorithms.
- Loading branch information
Showing
3 changed files
with
47 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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,43 @@ | ||
package task | ||
|
||
type Mutex struct { | ||
locked bool | ||
blocked Stack | ||
} | ||
|
||
func (m *Mutex) Lock() { | ||
if m.locked { | ||
// Push self onto stack of blocked tasks, and wait to be resumed. | ||
m.blocked.Push(Current()) | ||
Pause() | ||
return | ||
} | ||
|
||
m.locked = true | ||
} | ||
|
||
func (m *Mutex) Unlock() { | ||
if !m.locked { | ||
panic("sync: unlock of unlocked Mutex") | ||
} | ||
|
||
// Wake up a blocked task, if applicable. | ||
if t := m.blocked.Pop(); t != nil { | ||
scheduleTask(t) | ||
} else { | ||
m.locked = false | ||
} | ||
} | ||
|
||
// TryLock tries to lock m and reports whether it succeeded. | ||
// | ||
// Note that while correct uses of TryLock do exist, they are rare, | ||
// and use of TryLock is often a sign of a deeper problem | ||
// in a particular use of mutexes. | ||
func (m *Mutex) TryLock() bool { | ||
if m.locked { | ||
return false | ||
} | ||
m.Lock() | ||
return true | ||
} |
This file contains 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 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