Move to POSIX mutexes for SHM locks

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
This commit is contained in:
Matthew Heon
2018-08-10 13:46:07 -04:00
committed by Matthew Heon
parent f38fccb48c
commit e73484c176
6 changed files with 271 additions and 171 deletions

View File

@ -3,13 +3,7 @@
package lock
import (
"fmt"
"math"
"strconv"
"syscall"
"github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod/lock/shm"
"github.com/containers/libpod/libpod/lock/shm"
)
// SHMLockManager manages shared memory locks.
@ -18,8 +12,8 @@ type SHMLockManager struct {
}
// NewSHMLockManager makes a new SHMLockManager with the given number of locks.
func NewSHMLockManager(numLocks uint32) (Manager, error) {
locks, err := shm.CreateSHMLock(numLocks)
func NewSHMLockManager(path string, numLocks uint32) (Manager, error) {
locks, err := shm.CreateSHMLock(path, numLocks)
if err != nil {
return nil, err
}
@ -32,8 +26,8 @@ func NewSHMLockManager(numLocks uint32) (Manager, error) {
// OpenSHMLockManager opens an existing SHMLockManager with the given number of
// locks.
func OpenSHMLockManager(numLocks uint32) (Manager, error) {
locks, err := shm.OpenSHMLock(numLocks)
func OpenSHMLockManager(path string, numLocks uint32) (Manager, error) {
locks, err := shm.OpenSHMLock(path, numLocks)
if err != nil {
return nil, err
}
@ -59,27 +53,9 @@ func (m *SHMLockManager) AllocateLock() (Locker, error) {
}
// RetrieveLock retrieves a lock from the manager given its ID.
func (m *SHMLockManager) RetrieveLock(id string) (Locker, error) {
intID, err := strconv.ParseInt(id, 16, 64)
if err != nil {
return nil, errors.Wrapf(err, "given ID %q is not a valid SHMLockManager ID - cannot be parsed as int", id)
}
if intID < 0 {
return nil, errors.Wrapf(syscall.EINVAL, "given ID %q is not a valid SHMLockManager ID - must be positive", id)
}
if intID > math.MaxUint32 {
return nil, errors.Wrapf(syscall.EINVAL, "given ID %q is not a valid SHMLockManager ID - too large", id)
}
var u32ID uint32 = uint32(intID)
if u32ID >= m.locks.GetMaxLocks() {
return nil, errors.Wrapf(syscall.EINVAL, "given ID %q is not a valid SHMLockManager ID - too large to fit", id)
}
func (m *SHMLockManager) RetrieveLock(id uint32) (Locker, error) {
lock := new(SHMLock)
lock.lockID = u32ID
lock.lockID = id
lock.manager = m
return lock, nil
@ -92,8 +68,8 @@ type SHMLock struct {
}
// ID returns the ID of the lock.
func (l *SHMLock) ID() string {
return fmt.Sprintf("%x", l.lockID)
func (l *SHMLock) ID() uint32 {
return l.lockID
}
// Lock acquires the lock.