瀏覽代碼

Make bitseq.Handle thread-safe

Signed-off-by: Alessandro Boch <aboch@docker.com>
Alessandro Boch 10 年之前
父節點
當前提交
883fc7bca4
共有 1 個文件被更改,包括 9 次插入1 次删除
  1. 9 1
      libnetwork/bitseq/sequence.go

+ 9 - 1
libnetwork/bitseq/sequence.go

@@ -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)
 }
 }