Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock represents a lock of the semaphore. After the lock is not needed any more it should be released.
type Simple ¶
type Simple struct {
// contains filtered or unexported fields
}
Simple provides a way to lock concurrent access to a resource. It only allows one caller to gain access at a time.
func (*Simple) Acquire ¶
Acquire acquires a Lock, blocking until the previous ticket is released. Ticket needs to be discarded after the call to Acquire. The Lock has to be supplied to Release after it is not needed anymore and discarded afterwards. Acquire is safe for concurrent use.
func (*Simple) Enqueue ¶
Enqueue reserves the next place in the queue and returns a Ticket used to acquire access to the Lock when it's the callers turn. The Ticket has to be supplied to Acquire and discarded afterwards. Enqueue is not safe for concurrent use.
Example ¶
ExampleSimple demonstrates how different goroutines can be orchestrated to acquire locks in the same order as the tickets enqueued in the semaphore.
package main import ( "fmt" "sync" "time" "github.com/conduitio/conduit/pkg/foundation/semaphore" ) func main() { var sem semaphore.Simple var wg sync.WaitGroup // t2 is enqueued after t1, it can be acquired only after t1 t1 := sem.Enqueue() t2 := sem.Enqueue() wg.Add(2) go func() { defer wg.Done() time.Sleep(time.Millisecond) // simulate delay in acquiring fmt.Println("routine 1: try acquiring the lock") lock := sem.Acquire(t1) fmt.Println("routine 1: acquired the lock") sem.Release(lock) }() go func() { defer wg.Done() fmt.Println("routine 2: try acquiring the lock") lock := sem.Acquire(t2) // acquire will block because t1 needs to be acquired first fmt.Println("routine 2: acquired the lock") sem.Release(lock) }() wg.Wait() }
Output: routine 2: try acquiring the lock routine 1: try acquiring the lock routine 1: acquired the lock routine 2: acquired the lock