2016-04-19 13:23:20 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// nameSpaceParam - carries name space resource.
|
2016-04-19 13:23:20 -04:00
|
|
|
type nameSpaceParam struct {
|
|
|
|
volume string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// nameSpaceLock - provides primitives for locking critical namespace regions.
|
2016-04-19 13:23:20 -04:00
|
|
|
type nameSpaceLock struct {
|
|
|
|
rwMutex *sync.RWMutex
|
2016-04-20 00:58:11 -04:00
|
|
|
count uint
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
func (nsLock *nameSpaceLock) InUse() bool {
|
2016-04-20 00:58:11 -04:00
|
|
|
return nsLock.count != 0
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// Lock acquires write lock and increments the namespace counter.
|
|
|
|
func (nsLock *nameSpaceLock) Lock() {
|
2016-04-20 00:58:11 -04:00
|
|
|
nsLock.rwMutex.Lock()
|
|
|
|
nsLock.count++
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// Unlock releases write lock and decrements the namespace counter.
|
|
|
|
func (nsLock *nameSpaceLock) Unlock() {
|
2016-04-20 00:58:11 -04:00
|
|
|
nsLock.rwMutex.Unlock()
|
|
|
|
if nsLock.count != 0 {
|
|
|
|
nsLock.count--
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// RLock acquires read lock and increments the namespace counter.
|
|
|
|
func (nsLock *nameSpaceLock) RLock() {
|
2016-04-20 00:58:11 -04:00
|
|
|
nsLock.rwMutex.RLock()
|
|
|
|
nsLock.count++
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// RUnlock release read lock and decrements the namespace counter.
|
|
|
|
func (nsLock *nameSpaceLock) RUnlock() {
|
2016-04-20 00:58:11 -04:00
|
|
|
nsLock.rwMutex.RUnlock()
|
|
|
|
if nsLock.count != 0 {
|
|
|
|
nsLock.count--
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// newNSLock - provides a new instance of namespace locking primitives.
|
|
|
|
func newNSLock() *nameSpaceLock {
|
|
|
|
return &nameSpaceLock{
|
|
|
|
rwMutex: &sync.RWMutex{},
|
2016-04-20 00:58:11 -04:00
|
|
|
count: 0,
|
2016-04-19 15:30:10 -04:00
|
|
|
}
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|