-
Notifications
You must be signed in to change notification settings - Fork 12
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 #47 from ArtisanCloud/develop
feature(coroutine): add mutex status checker
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 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 @@ | ||
package cache |
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,23 @@ | ||
package locker | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
const mutexLocked = 1 | ||
|
||
func MutexLocked(m *sync.Mutex) bool { | ||
state := reflect.ValueOf(m).Elem().FieldByName("state") | ||
return state.Int()&mutexLocked == mutexLocked | ||
} | ||
|
||
func RWMutexWriteLocked(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 RWMutexReadLocked(rw *sync.RWMutex) bool { | ||
return reflect.ValueOf(rw).Elem().FieldByName("readerCount").Int() > 0 | ||
} |
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,37 @@ | ||
package locker | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func Test_MutexLocked(t *testing.T) { | ||
|
||
m := sync.Mutex{} | ||
fmt.Println("m locked =", MutexLocked(&m)) | ||
m.Lock() | ||
fmt.Println("m locked =", MutexLocked(&m)) | ||
m.Unlock() | ||
fmt.Println("m locked =", MutexLocked(&m)) | ||
|
||
} | ||
|
||
func Test_RWMutexWriteAndReadLocked(t *testing.T) { | ||
|
||
rw := sync.RWMutex{} | ||
fmt.Println("rw write locked =", RWMutexWriteLocked(&rw), " read locked =", RWMutexReadLocked(&rw)) | ||
rw.Lock() | ||
fmt.Println("rw write locked =", RWMutexWriteLocked(&rw), " read locked =", RWMutexReadLocked(&rw)) | ||
rw.Unlock() | ||
fmt.Println("rw write locked =", RWMutexWriteLocked(&rw), " read locked =", RWMutexReadLocked(&rw)) | ||
rw.RLock() | ||
fmt.Println("rw write locked =", RWMutexWriteLocked(&rw), " read locked =", RWMutexReadLocked(&rw)) | ||
rw.RLock() | ||
fmt.Println("rw write locked =", RWMutexWriteLocked(&rw), " read locked =", RWMutexReadLocked(&rw)) | ||
rw.RUnlock() | ||
fmt.Println("rw write locked =", RWMutexWriteLocked(&rw), " read locked =", RWMutexReadLocked(&rw)) | ||
rw.RUnlock() | ||
fmt.Println("rw write locked =", RWMutexWriteLocked(&rw), " read locked =", RWMutexReadLocked(&rw)) | ||
|
||
} |