parent
8deddb82f4
commit
984903cce1
@ -0,0 +1,128 @@ |
||||
/* |
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package main |
||||
|
||||
import "sync" |
||||
|
||||
// nsParam - carries name space resource.
|
||||
type nsParam struct { |
||||
volume string |
||||
path string |
||||
} |
||||
|
||||
// nsLock - provides primitives for locking critical namespace regions.
|
||||
type nsLock struct { |
||||
*sync.RWMutex |
||||
ref uint |
||||
} |
||||
|
||||
// nsLockMap - namespace lock map, provides primitives to Lock,
|
||||
// Unlock, RLock and RUnlock.
|
||||
type nsLockMap struct { |
||||
lockMap map[nsParam]*nsLock |
||||
mutex *sync.Mutex |
||||
} |
||||
|
||||
// Global name space lock.
|
||||
var nsMutex *nsLockMap |
||||
|
||||
// initNSLock - initialize name space lock map.
|
||||
func initNSLock() { |
||||
nsMutex = &nsLockMap{ |
||||
lockMap: make(map[nsParam]*nsLock), |
||||
mutex: &sync.Mutex{}, |
||||
} |
||||
} |
||||
|
||||
// Lock - locks the given resource for writes, using a previously
|
||||
// allocated name space lock or initializing a new one.
|
||||
func (n *nsLockMap) Lock(volume, path string) { |
||||
n.mutex.Lock() |
||||
defer n.mutex.Unlock() |
||||
|
||||
param := nsParam{volume, path} |
||||
nsLk, found := n.lockMap[param] |
||||
if !found { |
||||
nsLk = &nsLock{ |
||||
RWMutex: &sync.RWMutex{}, |
||||
ref: 0, |
||||
} |
||||
} |
||||
|
||||
// Acquire a write lock and update reference counter.
|
||||
nsLk.Lock() |
||||
nsLk.ref++ |
||||
|
||||
n.lockMap[param] = nsLk |
||||
} |
||||
|
||||
// Unlock - unlocks any previously acquired write locks.
|
||||
func (n *nsLockMap) Unlock(volume, path string) { |
||||
n.mutex.Lock() |
||||
defer n.mutex.Unlock() |
||||
|
||||
param := nsParam{volume, path} |
||||
if nsLk, found := n.lockMap[param]; found { |
||||
// Unlock a write lock and update reference counter.
|
||||
nsLk.Unlock() |
||||
if nsLk.ref != 0 { |
||||
nsLk.ref-- |
||||
} |
||||
if nsLk.ref != 0 { |
||||
n.lockMap[param] = nsLk |
||||
} |
||||
} |
||||
} |
||||
|
||||
// RLock - locks any previously acquired read locks.
|
||||
func (n *nsLockMap) RLock(volume, path string) { |
||||
n.mutex.Lock() |
||||
defer n.mutex.Unlock() |
||||
|
||||
param := nsParam{volume, path} |
||||
nsLk, found := n.lockMap[param] |
||||
if !found { |
||||
nsLk = &nsLock{ |
||||
RWMutex: &sync.RWMutex{}, |
||||
ref: 0, |
||||
} |
||||
} |
||||
|
||||
// Acquire a read lock and update reference counter.
|
||||
nsLk.RLock() |
||||
nsLk.ref++ |
||||
|
||||
n.lockMap[param] = nsLk |
||||
} |
||||
|
||||
// RUnlock - unlocks any previously acquired read locks.
|
||||
func (n *nsLockMap) RUnlock(volume, path string) { |
||||
n.mutex.Lock() |
||||
defer n.mutex.Unlock() |
||||
|
||||
param := nsParam{volume, path} |
||||
if nsLk, found := n.lockMap[param]; found { |
||||
// Unlock a read lock and update reference counter.
|
||||
nsLk.RUnlock() |
||||
if nsLk.ref != 0 { |
||||
nsLk.ref-- |
||||
} |
||||
if nsLk.ref != 0 { |
||||
n.lockMap[param] = nsLk |
||||
} |
||||
} |
||||
} |
@ -1,71 +0,0 @@ |
||||
/* |
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package main |
||||
|
||||
import "sync" |
||||
|
||||
// nameSpaceParam - carries name space resource.
|
||||
type nameSpaceParam struct { |
||||
volume string |
||||
path string |
||||
} |
||||
|
||||
// nameSpaceLock - provides primitives for locking critical namespace regions.
|
||||
type nameSpaceLock struct { |
||||
rwMutex *sync.RWMutex |
||||
count uint |
||||
} |
||||
|
||||
func (nsLock *nameSpaceLock) InUse() bool { |
||||
return nsLock.count != 0 |
||||
} |
||||
|
||||
// Lock acquires write lock and increments the namespace counter.
|
||||
func (nsLock *nameSpaceLock) Lock() { |
||||
nsLock.rwMutex.Lock() |
||||
nsLock.count++ |
||||
} |
||||
|
||||
// Unlock releases write lock and decrements the namespace counter.
|
||||
func (nsLock *nameSpaceLock) Unlock() { |
||||
nsLock.rwMutex.Unlock() |
||||
if nsLock.count != 0 { |
||||
nsLock.count-- |
||||
} |
||||
} |
||||
|
||||
// RLock acquires read lock and increments the namespace counter.
|
||||
func (nsLock *nameSpaceLock) RLock() { |
||||
nsLock.rwMutex.RLock() |
||||
nsLock.count++ |
||||
} |
||||
|
||||
// RUnlock release read lock and decrements the namespace counter.
|
||||
func (nsLock *nameSpaceLock) RUnlock() { |
||||
nsLock.rwMutex.RUnlock() |
||||
if nsLock.count != 0 { |
||||
nsLock.count-- |
||||
} |
||||
} |
||||
|
||||
// newNSLock - provides a new instance of namespace locking primitives.
|
||||
func newNSLock() *nameSpaceLock { |
||||
return &nameSpaceLock{ |
||||
rwMutex: &sync.RWMutex{}, |
||||
count: 0, |
||||
} |
||||
} |
Loading…
Reference in new issue