1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 12:45:38 +01:00
Files
glusterd2/pkg/utils/mutex.go
Prashanth Pai af0da645c8 Allow only one cluster join/leave request at a time
If a glusterd2 instance is already processing a cluster membership
request (leave or join), return 409 (Conflict) to the other such
client requests immediately.

Signed-off-by: Prashanth Pai <ppai@redhat.com>
2018-04-06 23:35:56 +05:30

33 lines
884 B
Go

package utils
import (
"sync"
"sync/atomic"
"unsafe"
)
const mutexLocked = 1 << iota
// MutexWithTry is a regular mutex with TryLock() functionality
type MutexWithTry struct {
sync.Mutex
}
// TryLock is the non-blocking version of Lock(). It returns true if the lock
// is successfully acquired and false otherwise. This is functionally similar
// to Lock.acquire(false) from Python's threading module and tryLock() from
// Java.
func (mwt *MutexWithTry) TryLock() bool {
// Refer golang source: src/sync/mutex.go
//
// type Mutex struct {
// state int32
// sema uint32
// }
//
// The first (unexported) member of sync.Mutex structure maintains the
// internal state of the mutex. It's of type int32. The type, size and
// offset of this state variable is very unlikely to change.
return atomic.CompareAndSwapInt32((*int32)(unsafe.Pointer(mwt)), 0, mutexLocked)
}