syncex

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	QSize = 64
)

Variables

View Source
var SpinFor = 50

SpinFor controls the number of iterations Lock/Clock is allowed to spin for before resorting to a slower (channel based) way of locking.

Functions

func Collect added in v0.0.3

func Collect(errchans ...<-chan error) <-chan error
Example
doWork := func(url string) chan error {
	errs := make(chan error)
	go func(url string) {
		// Some expensive work..
		_, err := http.Get(url)
		if err != nil {
			errs <- err
		}
	}(url)
	return errs
}

for err := range Collect(
	doWork("1"),
	doWork("2"),
) {
	if err != nil {
		fmt.Println("Error:", err)
	}
}
Output:

func CollectContext added in v0.0.3

func CollectContext(ctx context.Context, errchans ...<-chan error) <-chan error

Types

type Counter added in v0.0.4

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

func (*Counter) Add added in v0.0.4

func (c *Counter) Add(delta int64)

func (*Counter) Dec added in v0.0.4

func (c *Counter) Dec()

func (*Counter) Inc added in v0.0.4

func (c *Counter) Inc()

func (*Counter) Reset added in v0.0.4

func (c *Counter) Reset()

func (*Counter) Value added in v0.0.4

func (c *Counter) Value() int64

type MPMCQueue added in v0.0.7

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

func NewMPMCQueue added in v0.0.7

func NewMPMCQueue(capacity int) *MPMCQueue

NewMPMCQueue creates a new MPMCQueue instance with the given capacity.

func (*MPMCQueue) Dequeue added in v0.0.7

func (q *MPMCQueue) Dequeue() interface{}

Dequeue retrieves and removes the item from the head of the queue. Blocks, if the queue is empty.

func (*MPMCQueue) Enqueue added in v0.0.7

func (q *MPMCQueue) Enqueue(item interface{})

Enqueue inserts the given item into the queue. Blocks, if the queue is full.

func (*MPMCQueue) TryDequeue added in v0.0.7

func (q *MPMCQueue) TryDequeue() (item interface{}, ok bool)

TryDequeue retrieves and removes the item from the head of the queue. Does not block and returns immediately. The ok result indicates that the queue isn't empty and an item was retrieved.

func (*MPMCQueue) TryEnqueue added in v0.0.7

func (q *MPMCQueue) TryEnqueue(item interface{}) bool

TryEnqueue inserts the given item into the queue. Does not block and returns immediately. The result indicates that the queue isn't full and the item was inserted.

type Map added in v0.0.7

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

Map is like a Go map[string]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. It follows the interface of sync.Map.

A Map must not be copied after first use.

Map uses a modified version of Cache-Line Hash Table (CLHT) data structure: https://github.com/LPD-EPFL/CLHT

CLHT is built around idea to organize the hash table in cache-line-sized buckets, so that on all modern CPUs update operations complete with at most one cache-line transfer. Also, Get operations involve no write to memory, as well as no mutexes or any other sort of locks. Due to this design, in all considered scenarios Map outperforms sync.Map.

One important difference with sync.Map is that only string keys are supported. That's because Golang standard library does not expose the built-in hash functions for interface{} values.

Also note that, unlike in sync.Map, the underlying hash table used by Map never shrinks and only grows on demand. However, this should not be an issue in many cases since updates happen in-place leaving no tombstone entries.

func NewMap added in v0.0.7

func NewMap() *Map

NewMap creates a new Map instance.

func (*Map) Delete added in v0.0.7

func (m *Map) Delete(key string)

Delete deletes the value for a key.

func (*Map) Load added in v0.0.7

func (m *Map) Load(key string) (value interface{}, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*Map) LoadAndDelete added in v0.0.7

func (m *Map) LoadAndDelete(key string) (value interface{}, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*Map) LoadOrStore added in v0.0.7

func (m *Map) LoadOrStore(key string, value interface{}) (actual interface{}, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map) Range added in v0.0.7

func (m *Map) Range(f func(key string, value interface{}) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently, Range may reflect any mapping for that key from any point during the Range call.

It is safe to modify the map while iterating it. However, the concurrent modification rule apply, i.e. the changes may be not reflected in the subsequently iterated entries.

func (*Map) Store added in v0.0.7

func (m *Map) Store(key string, value interface{})

Store sets the value for a key.

type Mutex added in v0.0.7

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

Mutex is a context-aware mutual exclusion lock. Mutexes can be created as part of other structures; the zero value for a Mutex is an unlocked mutex. See https://golang.org/pkg/sync/#Mutex Supports CLock and TryLock.

func (*Mutex) CLock added in v0.0.7

func (m *Mutex) CLock(ctx context.Context) error

CLock tries to lock the mutex. If the mutex is is already locked, the calling goroutine blocks until the mutex is available or the context expires. Returns nil if the lock was taken successfully.

func (*Mutex) Lock added in v0.0.7

func (m *Mutex) Lock()

Lock the mutex. If the mutex is is already locked, the calling goroutine blocks until the mutex is available.

func (*Mutex) TryLock added in v0.0.7

func (m *Mutex) TryLock() bool

TryLock tries to lock the mutex. Does not block. If the mutex is is already locked, returns false immediately. Returns true if the lock was taken successfully.

func (*Mutex) Unlock added in v0.0.7

func (m *Mutex) Unlock()

Unlock the mutex. Does not block. It is a run-time error (panic) if m is not locked on entry to Unlock.

A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.

type RBMutex added in v0.0.8

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

func (*RBMutex) Lock added in v0.0.8

func (m *RBMutex) Lock()

func (*RBMutex) RLock added in v0.0.8

func (m *RBMutex) RLock() *RToken

func (*RBMutex) RUnlock added in v0.0.8

func (m *RBMutex) RUnlock(t *RToken)

func (*RBMutex) Unlock added in v0.0.8

func (m *RBMutex) Unlock()

type RToken added in v0.0.8

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

RToken is a reader lock token.

type Semaphore

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

func NewSemaphore

func NewSemaphore(initialCount int) *Semaphore

func (*Semaphore) Acquire

func (s *Semaphore) Acquire()

func (*Semaphore) AcquireTimeout

func (s *Semaphore) AcquireTimeout(timeout time.Duration) bool

func (*Semaphore) Release

func (s *Semaphore) Release()

type UnboundedChan added in v0.0.6

type UnboundedChan struct {

	// Q      *gxqueue.Queue
	Q *deque.Deque
	// contains filtered or unexported fields
}

refer from redisgo/redis/pool.go

func NewUnboundedChan added in v0.0.6

func NewUnboundedChan() *UnboundedChan

func (*UnboundedChan) Close added in v0.0.6

func (q *UnboundedChan) Close()

func (*UnboundedChan) Len added in v0.0.6

func (q *UnboundedChan) Len() int

func (*UnboundedChan) Pop added in v0.0.6

func (q *UnboundedChan) Pop() interface{}

func (*UnboundedChan) Push added in v0.0.6

func (q *UnboundedChan) Push(v interface{})

func (*UnboundedChan) SetWaitOption added in v0.0.6

func (q *UnboundedChan) SetWaitOption(wait bool)

在pop时,如果没有资源,是否等待 即使用乐观锁还是悲观锁

func (*UnboundedChan) TryPop added in v0.0.6

func (q *UnboundedChan) TryPop() (interface{}, bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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