Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/perbu/mutexdebug
https://github.com/perbu/mutexdebug
Last synced: 4 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/perbu/mutexdebug
- Owner: perbu
- License: other
- Created: 2023-07-03T08:58:26.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-03T09:34:54.000Z (over 1 year ago)
- Last Synced: 2024-11-19T02:03:36.149Z (2 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Mutexdebug
This package provides a simple mutex implementation for debugging purposes.
The constructor takes a timeout parameter, which is the maximum time a mutex
can be locked before it is considered to be locked forever. This is useful
for debugging deadlocks.It has a significant performance impact, so it should not be used in production.
## Usage
First you need to create an interface that will allow you to easily switch
between the real mutex and the debug mutex. This is done by creating a
`MutexInterface`:```go
package mypackage
type MutexInterface interface {
Lock()
Unlock()
}
```Now you can initialize the mutex in your code:
```go
package mainfunc useMutex() {
var m MutexInterface
switch debug {
case true:
m = mutexdebug.NewDebugMutex(time.Millisecond*40, true)
default:
m = &sync.Mutex{}
}
// now we use the mutex as usual.
m.Lock()
// do something
m.Unlock()
// you should be able to cast the mutex back to the debug mutex
// then you can access the debug information, like the number of warnings issued.
// see the tests for more information.
}```
## Complete example
Now you use the interface instead of the real mutex in your code and you can switch between the two.```go
package mainimport (
"github.com/perbu/mutexdebug"
"time"
)type MutexInterface interface {
Lock()
Unlock()
}const debug = true
func main() {
var m MutexInterface
switch debug {
case true:
m = mutexdebug.NewDebugMutex(time.Millisecond * 40, true)
default:
m = &sync.Mutex{}
}
// now we use the mutex as usual.
m.Lock()
// do something
m.Unlock()
if debug {
dm := m.(*mutexdebug.DebugMutex)
fmt.Println("Number of mutexes held too long: ",dm.Warnings.Load())
}
}```