-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from LopanovCo/debug
Mutexes workaround
- Loading branch information
Showing
8 changed files
with
116 additions
and
21 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package videoserver | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
// https://github.com/trailofbits/go-mutexasserts/blob/master/mutex.go#L15 | ||
const mutexLocked = 1 | ||
|
||
func RWMutexLocked(rw *sync.RWMutex) bool { | ||
// RWMutex has a "w" sync.Mutex field for write lock | ||
state := reflect.ValueOf(rw).Elem().FieldByName("w").FieldByName("state") | ||
return state.Int()&mutexLocked == mutexLocked | ||
} | ||
|
||
func MutexLocked(m *sync.Mutex) bool { | ||
state := reflect.ValueOf(m).Elem().FieldByName("state") | ||
return state.Int()&mutexLocked == mutexLocked | ||
} | ||
|
||
func RWMutexRLocked(rw *sync.RWMutex) bool { | ||
return readerCount(rw) > 0 | ||
} | ||
|
||
// Starting in go1.20, readerCount is an atomic int32 value. | ||
// See: https://go-review.googlesource.com/c/go/+/429767 | ||
func readerCount(rw *sync.RWMutex) int64 { | ||
// Look up the address of the readerCount field and use it to create a pointer to an atomic.Int32, | ||
// then load the value to return. | ||
rc := (*atomic.Int32)(reflect.ValueOf(rw).Elem().FieldByName("readerCount").Addr().UnsafePointer()) | ||
return int64(rc.Load()) | ||
} | ||
|
||
// Prior to go1.20, readerCount was an int value. | ||
// func readerCount(rw *sync.RWMutex) int64 { | ||
// return reflect.ValueOf(rw).Elem().FieldByName("readerCount").Int() | ||
// } |
Oops, something went wrong.