Skip to content

Commit

Permalink
implement trylock
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 2, 2023
1 parent 938ce22 commit 172c1d2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/sync/mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,21 @@ func (m *Mutex) Unlock() {
// Wake up a blocked task, if applicable.
if t := m.blocked.Pop(); t != nil {
scheduleTask(t)
} else {
m.locked = false
}
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
}

type RWMutex struct {
Expand Down

0 comments on commit 172c1d2

Please sign in to comment.