Debian Codegraph @

performance/performance-server.go

raw symbols SCIP
1 /*
2 * Minio Cloud Storage, (C) 2016 Minio, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package main
18
19 import (
20 "fmt"
21 "sync"
22
23 "github.com/minio/dsync"
24 )
25
26 const WriteLock = -1
27
28 type lockServer struct {
29 mutex sync.Mutex
30 // Map of locks, with negative value indicating (exclusive) write lock
31 // and positive values indicating number of read locks
32 lockMap map[string]int64
33 }
34
35 func (l *lockServer) Lock(args *dsync.LockArgs, reply *bool) error {
36 l.mutex.Lock()
37 defer l.mutex.Unlock()
38 if _, *reply = l.lockMap[args.Resource]; !*reply {
39 l.lockMap[args.Resource] = WriteLock // No locks held on the given name, so claim write lock
40 }
41 *reply = !*reply // Negate *reply to return true when lock is granted or false otherwise
42 return nil
43 }
44
45 func (l *lockServer) Unlock(args *dsync.LockArgs, reply *bool) error {
46 l.mutex.Lock()
47 defer l.mutex.Unlock()
48 var locksHeld int64
49 if locksHeld, *reply = l.lockMap[args.Resource]; !*reply { // No lock is held on the given name
50 return fmt.Errorf("Unlock attempted on an unlocked entity: %s", args.Resource)
51 }
52 if *reply = locksHeld == WriteLock; !*reply { // Unless it is a write lock
53 return fmt.Errorf("Unlock attempted on a read locked entity: %s (%d read locks active)", args.Resource, locksHeld)
54 }
55 delete(l.lockMap, args.Resource) // Remove the write lock
56 return nil
57 }
58
59 const ReadLock = 1
60
61 func (l *lockServer) RLock(args *dsync.LockArgs, reply *bool) error {
62 l.mutex.Lock()
63 defer l.mutex.Unlock()
64 var locksHeld int64
65 if locksHeld, *reply = l.lockMap[args.Resource]; !*reply {
66 l.lockMap[args.Resource] = ReadLock // No locks held on the given name, so claim (first) read lock
67 *reply = true
68 } else {
69 if *reply = locksHeld != WriteLock; *reply { // Unless there is a write lock
70 l.lockMap[args.Resource] = locksHeld + ReadLock // Grant another read lock
71 }
72 }
73 return nil
74 }
75
76 func (l *lockServer) RUnlock(args *dsync.LockArgs, reply *bool) error {
77 l.mutex.Lock()
78 defer l.mutex.Unlock()
79 var locksHeld int64
80 if locksHeld, *reply = l.lockMap[args.Resource]; !*reply { // No lock is held on the given name
81 return fmt.Errorf("RUnlock attempted on an unlocked entity: %s", args.Resource)
82 }
83 if *reply = locksHeld != WriteLock; !*reply { // A write-lock is held, cannot release a read lock
84 return fmt.Errorf("RUnlock attempted on a write locked entity: %s", args.Resource)
85 }
86 if locksHeld > ReadLock {
87 l.lockMap[args.Resource] = locksHeld - ReadLock // Remove one of the read locks held
88 } else {
89 delete(l.lockMap, args.Resource) // Remove the (last) read lock
90 }
91 return nil
92 }