Skip to content

Commit

Permalink
New sync package.
Browse files Browse the repository at this point in the history
* Rename syncutil to sync.
* Add aliases to sync types.
* Replace existing usage of standard library sync package.

This will make it easier to swap out synchronization primitives. For example,
this will allow us to use primitives from github.com/sasha-s/go-deadlock to
check for lock ordering violations.

Updates google#1472

PiperOrigin-RevId: 289033387
  • Loading branch information
iangudger authored and gvisor-bot committed Jan 10, 2020
1 parent b08da42 commit 27500d5
Show file tree
Hide file tree
Showing 286 changed files with 406 additions and 267 deletions.
1 change: 1 addition & 0 deletions pkg/amutex/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ go_test(
size = "small",
srcs = ["amutex_test.go"],
embed = [":amutex"],
deps = ["//pkg/sync"],
)
3 changes: 2 additions & 1 deletion pkg/amutex/amutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package amutex

import (
"sync"
"testing"
"time"

"gvisor.dev/gvisor/pkg/sync"
)

type sleeper struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/atomicbitops/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ go_test(
size = "small",
srcs = ["atomic_bitops_test.go"],
embed = [":atomicbitops"],
deps = ["//pkg/sync"],
)
3 changes: 2 additions & 1 deletion pkg/atomicbitops/atomic_bitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ package atomicbitops

import (
"runtime"
"sync"
"testing"

"gvisor.dev/gvisor/pkg/sync"
)

const iterations = 100
Expand Down
5 changes: 4 additions & 1 deletion pkg/compressio/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ go_library(
srcs = ["compressio.go"],
importpath = "gvisor.dev/gvisor/pkg/compressio",
visibility = ["//:sandbox"],
deps = ["//pkg/binary"],
deps = [
"//pkg/binary",
"//pkg/sync",
],
)

go_test(
Expand Down
2 changes: 1 addition & 1 deletion pkg/compressio/compressio.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import (
"hash"
"io"
"runtime"
"sync"

"gvisor.dev/gvisor/pkg/binary"
"gvisor.dev/gvisor/pkg/sync"
)

var bufPool = sync.Pool{
Expand Down
1 change: 1 addition & 0 deletions pkg/control/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
visibility = ["//:sandbox"],
deps = [
"//pkg/log",
"//pkg/sync",
"//pkg/unet",
"//pkg/urpc",
],
Expand Down
2 changes: 1 addition & 1 deletion pkg/control/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ package server

import (
"os"
"sync"

"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/unet"
"gvisor.dev/gvisor/pkg/urpc"
)
Expand Down
2 changes: 2 additions & 0 deletions pkg/eventchannel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
deps = [
":eventchannel_go_proto",
"//pkg/log",
"//pkg/sync",
"//pkg/unet",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//ptypes:go_default_library_gen",
Expand All @@ -40,6 +41,7 @@ go_test(
srcs = ["event_test.go"],
embed = [":eventchannel"],
deps = [
"//pkg/sync",
"@com_github_golang_protobuf//proto:go_default_library",
],
)
2 changes: 1 addition & 1 deletion pkg/eventchannel/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ package eventchannel
import (
"encoding/binary"
"fmt"
"sync"
"syscall"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
pb "gvisor.dev/gvisor/pkg/eventchannel/eventchannel_go_proto"
"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/unet"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/eventchannel/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package eventchannel

import (
"fmt"
"sync"
"testing"
"time"

"github.com/golang/protobuf/proto"
"gvisor.dev/gvisor/pkg/sync"
)

// testEmitter is an emitter that can be used in tests. It records all events
Expand Down
1 change: 1 addition & 0 deletions pkg/fdchannel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ go_test(
size = "small",
srcs = ["fdchannel_test.go"],
embed = [":fdchannel"],
deps = ["//pkg/sync"],
)
3 changes: 2 additions & 1 deletion pkg/fdchannel/fdchannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package fdchannel
import (
"io/ioutil"
"os"
"sync"
"syscall"
"testing"
"time"

"gvisor.dev/gvisor/pkg/sync"
)

func TestSendRecvFD(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/fdnotifier/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
importpath = "gvisor.dev/gvisor/pkg/fdnotifier",
visibility = ["//:sandbox"],
deps = [
"//pkg/sync",
"//pkg/waiter",
"@org_golang_x_sys//unix:go_default_library",
],
Expand Down
2 changes: 1 addition & 1 deletion pkg/fdnotifier/fdnotifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ package fdnotifier

import (
"fmt"
"sync"
"syscall"

"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/waiter"
)

Expand Down
3 changes: 2 additions & 1 deletion pkg/flipcall/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ go_library(
"//pkg/abi/linux",
"//pkg/log",
"//pkg/memutil",
"//pkg/syncutil",
"//pkg/sync",
],
)

Expand All @@ -31,4 +31,5 @@ go_test(
"flipcall_test.go",
],
embed = [":flipcall"],
deps = ["//pkg/sync"],
)
3 changes: 2 additions & 1 deletion pkg/flipcall/flipcall_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package flipcall
import (
"bytes"
"fmt"
"sync"

"gvisor.dev/gvisor/pkg/sync"
)

func Example() {
Expand Down
3 changes: 2 additions & 1 deletion pkg/flipcall/flipcall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package flipcall

import (
"runtime"
"sync"
"testing"
"time"

"gvisor.dev/gvisor/pkg/sync"
)

var testPacketWindowSize = pageSize
Expand Down
10 changes: 5 additions & 5 deletions pkg/flipcall/flipcall_unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"reflect"
"unsafe"

"gvisor.dev/gvisor/pkg/syncutil"
"gvisor.dev/gvisor/pkg/sync"
)

// Packets consist of a 16-byte header followed by an arbitrarily-sized
Expand Down Expand Up @@ -75,13 +75,13 @@ func (ep *Endpoint) Data() []byte {
var ioSync int64

func raceBecomeActive() {
if syncutil.RaceEnabled {
syncutil.RaceAcquire((unsafe.Pointer)(&ioSync))
if sync.RaceEnabled {
sync.RaceAcquire((unsafe.Pointer)(&ioSync))
}
}

func raceBecomeInactive() {
if syncutil.RaceEnabled {
syncutil.RaceReleaseMerge((unsafe.Pointer)(&ioSync))
if sync.RaceEnabled {
sync.RaceReleaseMerge((unsafe.Pointer)(&ioSync))
}
}
1 change: 1 addition & 0 deletions pkg/gate/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ go_test(
],
deps = [
":gate",
"//pkg/sync",
],
)
2 changes: 1 addition & 1 deletion pkg/gate/gate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package gate_test

import (
"sync"
"testing"
"time"

"gvisor.dev/gvisor/pkg/gate"
"gvisor.dev/gvisor/pkg/sync"
)

func TestBasicEnter(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/linewriter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
srcs = ["linewriter.go"],
importpath = "gvisor.dev/gvisor/pkg/linewriter",
visibility = ["//visibility:public"],
deps = ["//pkg/sync"],
)

go_test(
Expand Down
3 changes: 2 additions & 1 deletion pkg/linewriter/linewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package linewriter

import (
"bytes"
"sync"

"gvisor.dev/gvisor/pkg/sync"
)

// Writer is an io.Writer which buffers input, flushing
Expand Down
5 changes: 4 additions & 1 deletion pkg/log/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ go_library(
visibility = [
"//visibility:public",
],
deps = ["//pkg/linewriter"],
deps = [
"//pkg/linewriter",
"//pkg/sync",
],
)

go_test(
Expand Down
2 changes: 1 addition & 1 deletion pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
stdlog "log"
"os"
"runtime"
"sync"
"sync/atomic"
"syscall"
"time"

"gvisor.dev/gvisor/pkg/linewriter"
"gvisor.dev/gvisor/pkg/sync"
)

// Level is the log level.
Expand Down
1 change: 1 addition & 0 deletions pkg/metric/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
":metric_go_proto",
"//pkg/eventchannel",
"//pkg/log",
"//pkg/sync",
],
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package metric
import (
"errors"
"fmt"
"sync"
"sync/atomic"

"gvisor.dev/gvisor/pkg/eventchannel"
"gvisor.dev/gvisor/pkg/log"
pb "gvisor.dev/gvisor/pkg/metric/metric_go_proto"
"gvisor.dev/gvisor/pkg/sync"
)

var (
Expand Down
1 change: 1 addition & 0 deletions pkg/p9/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_library(
"//pkg/fdchannel",
"//pkg/flipcall",
"//pkg/log",
"//pkg/sync",
"//pkg/unet",
"@org_golang_x_sys//unix:go_default_library",
],
Expand Down
2 changes: 1 addition & 1 deletion pkg/p9/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package p9
import (
"errors"
"fmt"
"sync"
"syscall"

"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/flipcall"
"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/unet"
)

Expand Down
2 changes: 2 additions & 0 deletions pkg/p9/p9test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ go_library(
"//pkg/fd",
"//pkg/log",
"//pkg/p9",
"//pkg/sync",
"//pkg/unet",
"@com_github_golang_mock//gomock:go_default_library",
],
Expand All @@ -83,6 +84,7 @@ go_test(
deps = [
"//pkg/fd",
"//pkg/p9",
"//pkg/sync",
"@com_github_golang_mock//gomock:go_default_library",
],
)
2 changes: 1 addition & 1 deletion pkg/p9/p9test/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
"os"
"reflect"
"strings"
"sync"
"syscall"
"testing"
"time"

"github.com/golang/mock/gomock"
"gvisor.dev/gvisor/pkg/fd"
"gvisor.dev/gvisor/pkg/p9"
"gvisor.dev/gvisor/pkg/sync"
)

func TestPanic(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/p9/p9test/p9test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package p9test

import (
"fmt"
"sync"
"sync/atomic"
"syscall"
"testing"

"github.com/golang/mock/gomock"
"gvisor.dev/gvisor/pkg/p9"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/unet"
)

Expand Down
3 changes: 2 additions & 1 deletion pkg/p9/path_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ package p9

import (
"fmt"
"sync"

"gvisor.dev/gvisor/pkg/sync"
)

// pathNode is a single node in a path traversal.
Expand Down
2 changes: 1 addition & 1 deletion pkg/p9/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package p9

import (
"sync"
"gvisor.dev/gvisor/pkg/sync"
)

// pool is a simple allocator.
Expand Down
Loading

0 comments on commit 27500d5

Please sign in to comment.