1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Make bitseq.Handle thread-safe

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch 2015-06-14 10:21:59 -07:00
parent d1a16bbb84
commit 883fc7bca4

View file

@ -5,6 +5,7 @@ package bitseq
import ( import (
"fmt" "fmt"
"sync"
"github.com/docker/libnetwork/netutils" "github.com/docker/libnetwork/netutils"
) )
@ -22,9 +23,10 @@ const (
type Handle struct { type Handle struct {
ID string ID string
Head *Sequence Head *Sequence
sync.Mutex
} }
// NewHandle returns an instance of the bitmask handler // NewHandle returns a thread-safe instance of the bitmask handler
func NewHandle(id string, numElements uint32) *Handle { func NewHandle(id string, numElements uint32) *Handle {
return &Handle{ return &Handle{
ID: id, ID: id,
@ -135,17 +137,23 @@ func (s *Sequence) FromByteArray(data []byte) error {
// GetFirstAvailable returns the byte and bit position of the first unset bit // GetFirstAvailable returns the byte and bit position of the first unset bit
func (h *Handle) GetFirstAvailable() (int, int, error) { func (h *Handle) GetFirstAvailable() (int, int, error) {
h.Lock()
defer h.Unlock()
return GetFirstAvailable(h.Head) return GetFirstAvailable(h.Head)
} }
// CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset // CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset
// If the ordinal is beyond the Sequence limits, a negative response is returned // If the ordinal is beyond the Sequence limits, a negative response is returned
func (h *Handle) CheckIfAvailable(ordinal int) (int, int, error) { func (h *Handle) CheckIfAvailable(ordinal int) (int, int, error) {
h.Lock()
defer h.Unlock()
return CheckIfAvailable(h.Head, ordinal) return CheckIfAvailable(h.Head, ordinal)
} }
// PushReservation pushes the bit reservation inside the bitmask. // PushReservation pushes the bit reservation inside the bitmask.
func (h *Handle) PushReservation(bytePos, bitPos int, release bool) { func (h *Handle) PushReservation(bytePos, bitPos int, release bool) {
h.Lock()
defer h.Unlock()
h.Head = PushReservation(bytePos, bitPos, h.Head, release) h.Head = PushReservation(bytePos, bitPos, h.Head, release)
} }