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 // #cgo LDFLAGS: -lrt -lpthread
// #include "shm_lock.h" // #include "shm_lock.h"
@ -72,6 +72,11 @@ func OpenSHMLock(numLocks uint32) (*SHMLocks, error) {
return locks, nil 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. // Close closes an existing shared-memory segment.
// The segment will be rendered unusable after closing. // The segment will be rendered unusable after closing.
// WARNING: If you Close() while there are still locks locked, these locks may // WARNING: If you Close() while there are still locks locked, these locks may

View File

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

View File

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