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-19 15:30:10 -04:00
|
|
|
rcount uint
|
|
|
|
wcount uint
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
func (nsLock *nameSpaceLock) InUse() bool {
|
|
|
|
return nsLock.rcount != 0 || nsLock.wcount != 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-19 13:23:20 -04:00
|
|
|
nsLock.Lock()
|
2016-04-19 15:30:10 -04:00
|
|
|
nsLock.wcount++
|
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-19 13:23:20 -04:00
|
|
|
nsLock.Unlock()
|
2016-04-19 15:30:10 -04:00
|
|
|
if nsLock.wcount != 0 {
|
|
|
|
nsLock.wcount--
|
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-19 13:23:20 -04:00
|
|
|
nsLock.RLock()
|
2016-04-19 15:30:10 -04:00
|
|
|
nsLock.rcount++
|
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-19 13:23:20 -04:00
|
|
|
nsLock.RUnlock()
|
2016-04-19 15:30:10 -04:00
|
|
|
if nsLock.rcount != 0 {
|
|
|
|
nsLock.rcount--
|
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{},
|
|
|
|
rcount: 0,
|
|
|
|
wcount: 0,
|
|
|
|
}
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|