kbfssync

package
v2.11.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2018 License: BSD-3-Clause Imports: 5 Imported by: 4

README

kbfssync

Types used for synchronization in KBFS. The package is named kbfssync rather than sync to avoid conflicting with the sync package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RepeatedWaitGroup

type RepeatedWaitGroup struct {
	// contains filtered or unexported fields
}

RepeatedWaitGroup can be used in place of a sync.WaitGroup when code may need to repeatedly wait for a set of tasks to finish. (sync.WaitGroup requires special mutex usage to make this work properly, which can easily lead to deadlocks.) We use a mutex, int, and channel to track and synchronize on the number of outstanding tasks.

func (*RepeatedWaitGroup) Add

func (rwg *RepeatedWaitGroup) Add(delta int)

Add indicates that a number of tasks have begun.

func (*RepeatedWaitGroup) Done

func (rwg *RepeatedWaitGroup) Done()

Done indicates that one task has completed.

func (*RepeatedWaitGroup) Pause

func (rwg *RepeatedWaitGroup) Pause()

Pause causes any current or future callers of `WaitUnlessPaused` to return immediately.

func (*RepeatedWaitGroup) Resume

func (rwg *RepeatedWaitGroup) Resume()

Resume unpauses the wait group, allowing future callers of `WaitUnlessPaused` to wait until all the outstanding work is completed.

func (*RepeatedWaitGroup) Wait

func (rwg *RepeatedWaitGroup) Wait(ctx context.Context) error

Wait blocks until either the underlying task count goes to 0, or the given context is canceled.

func (*RepeatedWaitGroup) WaitUnlessPaused

func (rwg *RepeatedWaitGroup) WaitUnlessPaused(ctx context.Context) (
	bool, error)

WaitUnlessPaused works like Wait, except it can return early if the wait group is paused. It returns whether it was paused with outstanding work still left in the group.

type Semaphore

type Semaphore struct {
	// contains filtered or unexported fields
}

Semaphore implements a counting semaphore; it maintains a resource count, and exposes methods for acquiring those resources -- waiting if desired -- and releasing those resources back.

func NewSemaphore

func NewSemaphore() *Semaphore

NewSemaphore returns a new Semaphore with a resource count of 0. Use Release() to set the initial resource count.

func (*Semaphore) Acquire

func (s *Semaphore) Acquire(ctx context.Context, n int64) (int64, error)

Acquire blocks until it is possible to atomically subtract n (which must be positive) from the resource count without causing it to go negative, and then returns the updated resource count and nil. If the given context is canceled or times out first, it instead does not change the resource count, and returns the resource count at the time it blocked (which is necessarily less than n), and a wrapped ctx.Err().

func (*Semaphore) Count

func (s *Semaphore) Count() int64

Count returns the current resource count.

func (*Semaphore) ForceAcquire

func (s *Semaphore) ForceAcquire(n int64) int64

ForceAcquire atomically subtracts n (which must be positive) from the resource count without waking up any waiting acquirers. It is meant for correcting the initial resource count of the semaphore. It's okay if adding n causes the resource count goes negative, but it must not cause the resource count to underflow. The updated resource count is returned.

func (*Semaphore) Release

func (s *Semaphore) Release(n int64) int64

Release atomically adds n (which must be positive) to the resource count. It must not cause the resource count to overflow. If there are waiting acquirers, it wakes up at least one of them to make progress, assuming that no new acquirers arrive in the meantime. The updated resource count is returned.

func (*Semaphore) TryAcquire

func (s *Semaphore) TryAcquire(n int64) int64

TryAcquire atomically subtracts n (which must be positive) from the resource count without waking up any waiting acquirers, as long as it wouldn't go negative. If the count would go negative, it doesn't update the count but still returns the difference between the count and n. TryAcquire is successful if the return value is non-negative, and unsuccessful if the return value is negative. If the count would underflow, it panics. Otherwise, TryAcquire returns the updated resource count.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL