Replace containerStore.{Lock,Unlock} with {startWriting,stopWriting}
This integrates ReloadIfChanges, and makes it clearer that the responsibility for maintaining locking details is with the containerStore; we can change it in a single place. Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
parent
218dc2f35f
commit
76b412ddf1
|
@ -72,17 +72,12 @@ type rwContainerStore interface {
|
||||||
containerBigDataStore
|
containerBigDataStore
|
||||||
flaggableStore
|
flaggableStore
|
||||||
|
|
||||||
// Acquire a writer lock.
|
// startWriting makes sure the store is fresh, and locks it for writing.
|
||||||
// The default unix implementation panics if:
|
// If this succeeds, the caller MUST call stopWriting().
|
||||||
// - opening the lockfile failed
|
startWriting() error
|
||||||
// - tried to lock a read-only lock-file
|
|
||||||
Lock()
|
|
||||||
|
|
||||||
// Unlock the lock.
|
// stopWriting releases locks obtained by startWriting.
|
||||||
// The default unix implementation panics if:
|
stopWriting()
|
||||||
// - unlocking an unlocked lock
|
|
||||||
// - if the lock counter is corrupted
|
|
||||||
Unlock()
|
|
||||||
|
|
||||||
// startReading makes sure the store is fresh, and locks it for reading.
|
// startReading makes sure the store is fresh, and locks it for reading.
|
||||||
// If this succeeds, the caller MUST call stopReading().
|
// If this succeeds, the caller MUST call stopReading().
|
||||||
|
@ -209,6 +204,30 @@ func (c *Container) MountOpts() []string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// startWriting makes sure the store is fresh, and locks it for writing.
|
||||||
|
// If this succeeds, the caller MUST call stopWriting().
|
||||||
|
func (r *containerStore) startWriting() error {
|
||||||
|
r.lockfile.Lock()
|
||||||
|
succeeded := false
|
||||||
|
defer func() {
|
||||||
|
if !succeeded {
|
||||||
|
r.lockfile.Unlock()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := r.ReloadIfChanged(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
succeeded = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stopWriting releases locks obtained by startWriting.
|
||||||
|
func (r *containerStore) stopWriting() {
|
||||||
|
r.lockfile.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// startReading makes sure the store is fresh, and locks it for reading.
|
// startReading makes sure the store is fresh, and locks it for reading.
|
||||||
// If this succeeds, the caller MUST call stopReading().
|
// If this succeeds, the caller MUST call stopReading().
|
||||||
func (r *containerStore) startReading() error {
|
func (r *containerStore) startReading() error {
|
||||||
|
|
|
@ -1119,11 +1119,10 @@ func (s *store) writeToContainerStore(fn func(store rwContainerStore) error) err
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
store.Lock()
|
if err := store.startWriting(); err != nil {
|
||||||
defer store.Unlock()
|
|
||||||
if err := store.ReloadIfChanged(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer store.stopWriting()
|
||||||
return fn(store)
|
return fn(store)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1154,11 +1153,10 @@ func (s *store) writeToAllStores(fn func(rlstore rwLayerStore, ristore rwImageSt
|
||||||
if err := ristore.ReloadIfChanged(); err != nil {
|
if err := ristore.ReloadIfChanged(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
rcstore.Lock()
|
if err := rcstore.startWriting(); err != nil {
|
||||||
defer rcstore.Unlock()
|
|
||||||
if err := rcstore.ReloadIfChanged(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer rcstore.stopWriting()
|
||||||
|
|
||||||
return fn(rlstore, ristore, rcstore)
|
return fn(rlstore, ristore, rcstore)
|
||||||
}
|
}
|
||||||
|
@ -1195,11 +1193,10 @@ func (s *store) PutLayer(id, parent string, names []string, mountLabel string, w
|
||||||
if err := rlstore.ReloadIfChanged(); err != nil {
|
if err := rlstore.ReloadIfChanged(); err != nil {
|
||||||
return nil, -1, err
|
return nil, -1, err
|
||||||
}
|
}
|
||||||
rcstore.Lock()
|
if err := rcstore.startWriting(); err != nil {
|
||||||
defer rcstore.Unlock()
|
|
||||||
if err := rcstore.ReloadIfChanged(); err != nil {
|
|
||||||
return nil, -1, err
|
return nil, -1, err
|
||||||
}
|
}
|
||||||
|
defer rcstore.stopWriting()
|
||||||
if options == nil {
|
if options == nil {
|
||||||
options = &LayerOptions{}
|
options = &LayerOptions{}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue