summaryrefslogtreecommitdiff
path: root/libvirt/pool_sync.go
blob: a50528bcbada269cc7eac151b0f9cc99d6790e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package libvirt

import (
	"sync"
)

// LVirtPoolSync makes possible to synchronize operations
// against libvirt pools.
// Doing pool.Refresh() operations while uploading or removing
// a volume into the pool causes errors inside of libvirtd
type LVirtPoolSync struct {
	PoolLocks     map[string]*sync.Mutex
	internalMutex sync.Mutex
}

// NewLVirtPoolSync allocates a new instance of LVirtPoolSync
func NewLVirtPoolSync() LVirtPoolSync {
	pool := LVirtPoolSync{}
	pool.PoolLocks = make(map[string]*sync.Mutex)

	return pool
}

// AcquireLock acquires a lock for the specified pool
func (ps LVirtPoolSync) AcquireLock(pool string) {
	ps.internalMutex.Lock()
	defer ps.internalMutex.Unlock()

	lock, exists := ps.PoolLocks[pool]
	if !exists {
		lock = new(sync.Mutex)
		ps.PoolLocks[pool] = lock
	}

	lock.Lock()
}

// ReleaseLock releases the look for the specified pool
func (ps LVirtPoolSync) ReleaseLock(pool string) {
	ps.internalMutex.Lock()
	defer ps.internalMutex.Unlock()

	lock, exists := ps.PoolLocks[pool]
	if !exists {
		return
	}

	lock.Unlock()
}