-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use single mutex for volume locks to prevent memory leak
Signed-off-by: Luca Berneking <[email protected]>
- Loading branch information
Showing
2 changed files
with
56 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package driver | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type volumeLock struct { | ||
cond sync.Cond | ||
locked map[string]struct{} | ||
} | ||
|
||
func newVolumeLock() *volumeLock { | ||
return &volumeLock{ | ||
cond: *sync.NewCond(&sync.Mutex{}), | ||
locked: map[string]struct{}{}, | ||
} | ||
} | ||
|
||
func (l *volumeLock) LockVolume(volume string) func() { | ||
l.cond.L.Lock() | ||
defer l.cond.L.Unlock() | ||
|
||
for { | ||
if _, locked := l.locked[volume]; !locked { | ||
break | ||
} | ||
|
||
l.cond.Wait() | ||
} | ||
|
||
l.locked[volume] = struct{}{} | ||
|
||
return func() { | ||
l.cond.L.Lock() | ||
defer l.cond.L.Unlock() | ||
|
||
delete(l.locked, volume) | ||
l.cond.Broadcast() | ||
} | ||
} | ||
|
||
func (l *volumeLock) LockVolumeWithSnapshot(volume string, snapshot string) func() { | ||
unlockVol := l.LockVolume(volume) | ||
unlockSnap := l.LockVolume(snapshot) | ||
return func() { | ||
unlockVol() | ||
unlockSnap() | ||
} | ||
} |