semaphore

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package semaphore provides synchronisation primitives for controlling access to shared resources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Semaphore

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

Semaphore provides a synchronisation primitive for controlling access to shared resources using a spinlock mechanism. It supports both exclusive and read locks with context-aware and blocking acquisition methods.

func (*Semaphore) Lock

func (s *Semaphore) Lock()

Lock acquires an exclusive lock. Blocks until the lock is acquired and cannot be cancelled. Panics if the lock cannot be acquired.

func (*Semaphore) LockContext

func (s *Semaphore) LockContext(ctx context.Context) error

LockContext attempts to acquire an exclusive lock with a context. Blocks until the lock is acquired or the context is cancelled. Returns an error if the context is cancelled before acquisition.

func (*Semaphore) RLock

func (s *Semaphore) RLock()

RLock acquires a read lock. Blocks until the lock is acquired and cannot be cancelled. Panics if the semaphore is nil.

func (*Semaphore) RLockContext

func (s *Semaphore) RLockContext(ctx context.Context) error

RLockContext attempts to acquire a read lock with a context. Blocks until the lock is acquired or the context is cancelled. Returns an error if the context is cancelled before acquisition.

func (*Semaphore) RUnlock

func (s *Semaphore) RUnlock()

RUnlock releases a read lock, allowing other readers or writers to acquire the lock. Panics if the lock is not held or cannot be released.

func (*Semaphore) TryLock

func (s *Semaphore) TryLock() bool

TryLock attempts to acquire an exclusive lock without blocking. Returns immediately with a boolean indicating success. Returns true if the lock was successfully acquired, false otherwise.

func (*Semaphore) TryRLock

func (s *Semaphore) TryRLock() bool

TryRLock attempts to acquire a read lock without blocking. Returns immediately with a boolean indicating success. Returns true if the lock was successfully acquired, false otherwise.

func (*Semaphore) Unlock

func (s *Semaphore) Unlock()

Unlock releases an exclusive lock, allowing other writers or readers to acquire the lock. Panics if the lock is not held or cannot be released.

Jump to

Keyboard shortcuts

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