https://github.com/lrita/gosync
a TryLock implementation
https://github.com/lrita/gosync
go golang golang-package mutex trylock
Last synced: about 1 month ago
JSON representation
a TryLock implementation
- Host: GitHub
- URL: https://github.com/lrita/gosync
- Owner: lrita
- License: mit
- Created: 2016-12-09T12:11:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-07T07:35:58.000Z (about 7 years ago)
- Last Synced: 2025-04-05T10:51:08.627Z (2 months ago)
- Topics: go, golang, golang-package, mutex, trylock
- Language: Go
- Size: 4.88 KB
- Stars: 16
- Watchers: 3
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A mutex implementation with TryLock
## safe guaranteed
Here is a general situations:```
mutex.Lock()
A() mutex.Lock()
mutex.UnLock() B()
mutex.UnLock()
```When we synchronize using mutex, we should guarantee unlock() `happens before` lock()
to make no data race.[_A send on a channel happens before the corresponding receive from that channel completes_](https://golang.org/ref/mem#tmp_7),
so we use a buffered channel to guarantee this.## interface
```
type Mutex interface {
Lock()
UnLock()
// TryLock return true if it fetch mutex
TryLock() bool
// TryLockTimeout return true if it fetch mutex, return false if timeout
TryLockTimeout(timeout time.Duration) bool
// TryLockTimeout return true if it fetch mutex, return false if context done
TryLockContext(ctx context.Context) bool
}type MutexGroup interface {
Lock(i interface{})
UnLock(i interface{})
// TryLock return true if it fetch mutex
TryLock(i interface{}) bool
// TryLockTimeout return true if it fetch mutex, return false if timeout
TryLockTimeout(i interface{}, timeout time.Duration) bool
// TryLockTimeout return true if it fetch mutex, return false if context done
TryLockContext(i interface{}, ctx context.Context) bool
}
```