summaryrefslogtreecommitdiff
path: root/libvirt/pool_sync.go
blob: 8c53ac05a17dfdce01dbfa689f2dc2403d7512e2 (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"
)

// LibVirtPoolSync 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 LibVirtPoolSync struct {
	PoolLocks     map[string]*sync.Mutex
	internalMutex sync.Mutex
}

// Allocate a new instance of LibVirtPoolSync
func NewLibVirtPoolSync() LibVirtPoolSync {
	pool := LibVirtPoolSync{}
	pool.PoolLocks = make(map[string]*sync.Mutex)

	return pool
}

// Acquire a lock for the specified pool
func (ps LibVirtPoolSync) 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()
}

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

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

	lock.Unlock()
}