mirror of
https://github.com/containers/podman.git
synced 2025-06-22 18:08:11 +08:00
Merge pull request #2083 from mheon/shm_locking_fixes
Address lingering review comments from SHM locking PR
This commit is contained in:
@ -43,6 +43,9 @@ type Locker interface {
|
|||||||
// encounters a fatal error.
|
// encounters a fatal error.
|
||||||
// All errors must be handled internally, as they are not returned. For
|
// All errors must be handled internally, as they are not returned. For
|
||||||
// the most part, panicking should be appropriate.
|
// the most part, panicking should be appropriate.
|
||||||
|
// Some lock implementations may require that Lock() and Unlock() occur
|
||||||
|
// within the same goroutine (SHM locking, for example). The usual Go
|
||||||
|
// Lock()/defer Unlock() pattern will still work fine in these cases.
|
||||||
Lock()
|
Lock()
|
||||||
// Unlock unlocks the lock.
|
// Unlock unlocks the lock.
|
||||||
// All errors must be handled internally, as they are not returned. For
|
// All errors must be handled internally, as they are not returned. For
|
||||||
|
@ -36,7 +36,7 @@ type SHMLocks struct { // nolint
|
|||||||
// size used by the underlying implementation.
|
// size used by the underlying implementation.
|
||||||
func CreateSHMLock(path string, numLocks uint32) (*SHMLocks, error) {
|
func CreateSHMLock(path string, numLocks uint32) (*SHMLocks, error) {
|
||||||
if numLocks == 0 {
|
if numLocks == 0 {
|
||||||
return nil, errors.Wrapf(syscall.EINVAL, "number of locks must greater than 0 0")
|
return nil, errors.Wrapf(syscall.EINVAL, "number of locks must be greater than 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
locks := new(SHMLocks)
|
locks := new(SHMLocks)
|
||||||
@ -65,7 +65,7 @@ func CreateSHMLock(path string, numLocks uint32) (*SHMLocks, error) {
|
|||||||
// segment was created with.
|
// segment was created with.
|
||||||
func OpenSHMLock(path string, numLocks uint32) (*SHMLocks, error) {
|
func OpenSHMLock(path string, numLocks uint32) (*SHMLocks, error) {
|
||||||
if numLocks == 0 {
|
if numLocks == 0 {
|
||||||
return nil, errors.Wrapf(syscall.EINVAL, "number of locks must greater than 0")
|
return nil, errors.Wrapf(syscall.EINVAL, "number of locks must be greater than 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
locks := new(SHMLocks)
|
locks := new(SHMLocks)
|
||||||
|
@ -256,13 +256,13 @@ func TestLockSemaphoreActuallyLocks(t *testing.T) {
|
|||||||
// Ensures that runtime.LockOSThread() is doing its job
|
// Ensures that runtime.LockOSThread() is doing its job
|
||||||
func TestLockAndUnlockTwoSemaphore(t *testing.T) {
|
func TestLockAndUnlockTwoSemaphore(t *testing.T) {
|
||||||
runLockTest(t, func(t *testing.T, locks *SHMLocks) {
|
runLockTest(t, func(t *testing.T, locks *SHMLocks) {
|
||||||
err := locks.LockSemaphore(0)
|
err := locks.LockSemaphore(5)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
err = locks.LockSemaphore(1)
|
err = locks.LockSemaphore(6)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
err = locks.UnlockSemaphore(1)
|
err = locks.UnlockSemaphore(6)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// Now yield scheduling
|
// Now yield scheduling
|
||||||
@ -272,7 +272,7 @@ func TestLockAndUnlockTwoSemaphore(t *testing.T) {
|
|||||||
// And unlock the last semaphore
|
// And unlock the last semaphore
|
||||||
// If we are in a different OS thread, this should fail.
|
// If we are in a different OS thread, this should fail.
|
||||||
// However, runtime.UnlockOSThread() should guarantee we are not
|
// However, runtime.UnlockOSThread() should guarantee we are not
|
||||||
err = locks.UnlockSemaphore(0)
|
err = locks.UnlockSemaphore(5)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,10 @@
|
|||||||
package lock
|
package lock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/containers/libpod/libpod/lock/shm"
|
"github.com/containers/libpod/libpod/lock/shm"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SHMLockManager manages shared memory locks.
|
// SHMLockManager manages shared memory locks.
|
||||||
@ -60,6 +63,11 @@ func (m *SHMLockManager) RetrieveLock(id uint32) (Locker, error) {
|
|||||||
lock.lockID = id
|
lock.lockID = id
|
||||||
lock.manager = m
|
lock.manager = m
|
||||||
|
|
||||||
|
if id >= m.locks.GetMaxLocks() {
|
||||||
|
return nil, errors.Wrapf(syscall.EINVAL, "lock ID %d is too large - max lock size is %d",
|
||||||
|
id, m.locks.GetMaxLocks()-1)
|
||||||
|
}
|
||||||
|
|
||||||
return lock, nil
|
return lock, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user