sync

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2016 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStopped = errors.New("ThreadGroup already stopped")

ErrStopped is returned by ThreadGroup methods if Stop has already been called.

Functions

This section is empty.

Types

type RWMutex

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

RWMutex provides locking functions, and an ability to detect and remove deadlocks.

func New

func New(maxLockTime time.Duration, callDepth int) *RWMutex

New takes a maxLockTime and returns a lock. The lock will never stay locked for more than maxLockTime, instead printing an error and unlocking after maxLockTime has passed.

func (*RWMutex) Lock

func (rwm *RWMutex) Lock() int

Lock will lock the RWMutex. The return value must be used as input when calling RUnlock.

func (*RWMutex) RLock

func (rwm *RWMutex) RLock() int

RLock will read lock the RWMutex. The return value must be used as input when calling RUnlock.

func (*RWMutex) RUnlock

func (rwm *RWMutex) RUnlock(id int)

RUnlock will read unlock the RWMutex. The return value of calling RLock must be used as input.

func (*RWMutex) Unlock

func (rwm *RWMutex) Unlock(id int)

Unlock will unlock the RWMutex. The return value of calling Lock must be used as input.

type ThreadGroup added in v1.0.0

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

ThreadGroup is a sync.WaitGroup with additional functionality for facilitating clean shutdown. Namely, it provides a StopChan method for notifying callers when shutdown occurrs. Another key difference is that a ThreadGroup is only intended be used once; as such, its Add and Stop methods return errors if Stop has already been called.

During shutdown, it is common to close resources such as net.Listeners. Typically, this would require spawning a goroutine to wait on the ThreadGroup's StopChan and then close the resource. To make this more convenient, ThreadGroup provides an OnStop method. Functions passed to OnStop will be called automatically when Stop is called.

func (*ThreadGroup) Add added in v1.0.0

func (tg *ThreadGroup) Add() error

Add increments the ThreadGroup counter.

func (*ThreadGroup) Done added in v1.0.0

func (tg *ThreadGroup) Done()

Done decrements the ThreadGroup counter.

func (*ThreadGroup) IsStopped added in v1.0.0

func (tg *ThreadGroup) IsStopped() bool

IsStopped returns true if Stop has been called.

func (*ThreadGroup) OnStop added in v1.0.0

func (tg *ThreadGroup) OnStop(fn func())

OnStop adds a function to the ThreadGroup's stopFns set. Members of the set will be called when Stop is called, in reverse order. If the ThreadGroup is already stopped, the function will be called immediately.

func (*ThreadGroup) Stop added in v1.0.0

func (tg *ThreadGroup) Stop() error

Stop closes the ThreadGroup's stopChan, closes members of the closer set, and blocks until the counter is zero.

func (*ThreadGroup) StopChan added in v1.0.0

func (tg *ThreadGroup) StopChan() <-chan struct{}

StopChan provides read-only access to the ThreadGroup's stopChan. Callers should select on StopChan in order to interrupt long-running reads (such as time.After).

Jump to

Keyboard shortcuts

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