From 883fc7bca43d54b21fe1b4d1f730e640001aca93 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Sun, 14 Jun 2015 10:21:59 -0700 Subject: [PATCH] Make bitseq.Handle thread-safe Signed-off-by: Alessandro Boch --- libnetwork/bitseq/sequence.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libnetwork/bitseq/sequence.go b/libnetwork/bitseq/sequence.go index 094a74a8c3..f3539c5de2 100644 --- a/libnetwork/bitseq/sequence.go +++ b/libnetwork/bitseq/sequence.go @@ -5,6 +5,7 @@ package bitseq import ( "fmt" + "sync" "github.com/docker/libnetwork/netutils" ) @@ -22,9 +23,10 @@ const ( type Handle struct { ID string 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 { return &Handle{ 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 func (h *Handle) GetFirstAvailable() (int, int, error) { + h.Lock() + defer h.Unlock() return GetFirstAvailable(h.Head) } // 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 func (h *Handle) CheckIfAvailable(ordinal int) (int, int, error) { + h.Lock() + defer h.Unlock() return CheckIfAvailable(h.Head, ordinal) } // PushReservation pushes the bit reservation inside the bitmask. func (h *Handle) PushReservation(bytePos, bitPos int, release bool) { + h.Lock() + defer h.Unlock() h.Head = PushReservation(bytePos, bitPos, h.Head, release) }