Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/goofys_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func waitFor(t *C, addr string) (err error) {

func (t *GoofysTest) deleteBlobsParallelly(cloud StorageBackend, blobs []string) error {
const concurrency = 10
sem := make(semaphore, concurrency)
sem := NewSemaphore(concurrency)
sem.P(concurrency)
var err error
for _, blobOuter := range blobs {
Expand Down Expand Up @@ -373,7 +373,7 @@ func (s *GoofysTest) removeBlob(cloud StorageBackend, t *C, blobPath string) {

func (s *GoofysTest) setupBlobs(cloud StorageBackend, t *C, env map[string]*string) {
const concurrency = 10
throttler := make(semaphore, concurrency)
throttler := NewSemaphore(concurrency)
throttler.P(concurrency)

var globalErr atomic.Value
Expand Down Expand Up @@ -411,7 +411,7 @@ func (s *GoofysTest) setupBlobs(cloud StorageBackend, t *C, env map[string]*stri
}(path, c)
}
throttler.V(concurrency)
throttler = make(semaphore, concurrency)
throttler = NewSemaphore(concurrency)
throttler.P(concurrency)
t.Assert(globalErr.Load(), IsNil)

Expand Down
27 changes: 16 additions & 11 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package core

import (
"context"
"fmt"
"strings"
"time"
"unicode"

"golang.org/x/sync/semaphore"
)

var TIME_MAX = time.Unix(1<<63-62135596801, 999999999)
Expand Down Expand Up @@ -170,20 +173,22 @@ func Dup(value []byte) []byte {
return ret
}

type empty struct{}

// TODO(dotslash/khc): Remove this semaphore in favor of
// https://godoc.org/golang.org/x/sync/semaphore
type semaphore chan empty
type Semaphore struct {
sem *semaphore.Weighted
}

func (sem semaphore) P(n int) {
for i := 0; i < n; i++ {
sem <- empty{}
func NewSemaphore(n int64) *Semaphore {
return &Semaphore{
sem: semaphore.NewWeighted(n),
}
}

func (sem semaphore) V(n int) {
for i := 0; i < n; i++ {
<-sem
func (s *Semaphore) P(n int) {
if err := s.sem.Acquire(context.Background(), int64(n)); err != nil {
panic(err)
}
}

func (s *Semaphore) V(n int) {
s.sem.Release(int64(n))
}
Loading