Refactor locks package to build on non-Linux

Move SHM specific code into a subpackage. Within the main locks
package, move the manager to be linux-only and add a non-Linux
unsupported build file.

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
This commit is contained in:
Matthew Heon 2018-08-08 15:50:16 -04:00 committed by Matthew Heon
parent 3ed81051e8
commit a21f21efa1
6 changed files with 48 additions and 11 deletions

View File

@ -1,4 +1,4 @@
package lock
package shm
// #cgo LDFLAGS: -lrt -lpthread
// #include "shm_lock.h"
@ -72,6 +72,11 @@ func OpenSHMLock(numLocks uint32) (*SHMLocks, error) {
return locks, nil
}
// GetMaxLocks returns the maximum number of locks in the SHM
func (locks *SHMLocks) GetMaxLocks() uint32 {
return locks.maxLocks
}
// Close closes an existing shared-memory segment.
// The segment will be rendered unusable after closing.
// WARNING: If you Close() while there are still locks locked, these locks may

View File

@ -1,4 +1,4 @@
package lock
package shm
import (
"fmt"

View File

@ -1,3 +1,5 @@
// +build linux
package lock
import (
@ -7,16 +9,17 @@ import (
"syscall"
"github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod/lock/shm"
)
// SHMLockManager manages shared memory locks.
type SHMLockManager struct {
locks *SHMLocks
locks *shm.SHMLocks
}
// NewSHMLockManager makes a new SHMLockManager with the given number of locks.
func NewSHMLockManager(numLocks uint32) (Manager, error) {
locks, err := CreateSHMLock(numLocks)
locks, err := shm.CreateSHMLock(numLocks)
if err != nil {
return nil, err
}
@ -29,8 +32,8 @@ func NewSHMLockManager(numLocks uint32) (Manager, error) {
// OpenSHMLockManager opens an existing SHMLockManager with the given number of
// locks.
func OpenSHMLockManager(numLocks uint32) (LockManager, error) {
locks, err := OpenSHMLock(numLocks)
func OpenSHMLockManager(numLocks uint32) (Manager, error) {
locks, err := shm.OpenSHMLock(numLocks)
if err != nil {
return nil, err
}
@ -59,20 +62,20 @@ func (m *SHMLockManager) AllocateLock() (Locker, error) {
func (m *SHMLockManager) RetrieveLock(id string) (Locker, error) {
intID, err := strconv.ParseInt(id, 16, 64)
if err != nil {
return errors.Wrapf(err, "given ID %q is not a valid SHMLockManager ID - cannot be parsed as int", id)
return nil, errors.Wrapf(err, "given ID %q is not a valid SHMLockManager ID - cannot be parsed as int", id)
}
if intID < 0 {
return errors.Wrapf(syscall.EINVAL, "given ID %q is not a valid SHMLockManager ID - must be positive", id)
return nil, errors.Wrapf(syscall.EINVAL, "given ID %q is not a valid SHMLockManager ID - must be positive", id)
}
if intID > math.MaxUint32 {
return errors.Wrapf(syscall.EINVAL, "given ID %q is not a valid SHMLockManager ID - too large", id)
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.maxLocks {
return errors.Wrapf(syscall.EINVAL, "given ID %q is not a valid SHMLockManager ID - too large to fit", id)
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)
}
lock := new(SHMLock)

View File

@ -0,0 +1,29 @@
// +build !linux
package lock
import "fmt"
// SHMLockManager is a shared memory lock manager.
// It is not supported on non-Unix platforms.
type SHMLockManager struct{}
// NewSHMLockManager is not supported on this platform
func NewSHMLockManager(numLocks uint32) (Manager, error) {
return nil, fmt.Errorf("not supported")
}
// OpenSHMLockManager is not supported on this platform
func OpenSHMLockManager(numLocks uint32) (Manager, error) {
return nil, fmt.Errorf("not supported")
}
// AllocateLock is not supported on this platform
func (m *SHMLockManager) AllocateLock() (Locker, error) {
return nil, fmt.Errorf("not supported")
}
// RetrieveLock is not supported on this platform
func (m *SHMLockManager) RetrieveLock(id string) (Locker, error) {
return nil, fmt.Errorf("not supported")
}