locker

package
v0.0.0-...-c3dbfd1 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2016 License: Apache-2.0 Imports: 3 Imported by: 0

README

Locker

locker provides a mechanism for creating finer-grained locking to help free up more global locks to handle other tasks.

The implementation looks close to a sync.Mutex, however the user must provide a reference to use to refer to the underlying lock when locking and unlocking, and unlock may generate an error.

If a lock with a given name does not exist when Lock is called, one is created. Lock references are automatically cleaned up on Unlock if nothing else is waiting for the lock.

Usage

package important

import (
	"sync"
	"time"

	"github.com/docker/docker/pkg/locker"
)

type important struct {
	locks *locker.Locker
	data  map[string]interface{}
	mu    sync.Mutex
}

func (i *important) Get(name string) interface{} {
	i.locks.Lock(name)
	defer i.locks.Unlock(name)
	return data[name]
}

func (i *important) Create(name string, data interface{}) {
	i.locks.Lock(name)
	defer i.locks.Unlock(name)

	i.createImportant(data)

	s.mu.Lock()
	i.data[name] = data
	s.mu.Unlock()
}

func (i *important) createImportant(data interface{}) {
	time.Sleep(10 * time.Second)
}

For functions dealing with a given name, always lock at the beginning of the function (or before doing anything with the underlying state), this ensures any other function that is dealing with the same name will block.

When needing to modify the underlying data, use the global lock to ensure nothing else is modfying it at the same time. Since name lock is already in place, no reads will occur while the modification is being performed.

Documentation

Overview

Package locker provides a mechanism for creating finer-grained locking to help free up more global locks to handle other tasks.

The implementation looks close to a sync.Mutex, however the user must provide a reference to use to refer to the underlying lock when locking and unlocking, and unlock may generate an error.

If a lock with a given name does not exist when `Lock` is called, one is created. Lock references are automatically cleaned up on `Unlock` if nothing else is waiting for the lock.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSuchLock = errors.New("no such lock")

ErrNoSuchLock is returned when the requested lock does not exist

Functions

This section is empty.

Types

type Locker

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

Locker provides a locking mechanism based on the passed in reference name

func New

func New() *Locker

New creates a new Locker

func (*Locker) Lock

func (l *Locker) Lock(name string)

Lock locks a mutex with the given name. If it doesn't exist, one is created

func (*Locker) Unlock

func (l *Locker) Unlock(name string) error

Unlock unlocks the mutex with the given name If the given lock is not being waited on by any other callers, it is deleted

Jump to

Keyboard shortcuts

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