Compare commits
No commits in common. "c6f54927b0730a2db0a4e9dead16537bbc9894e2" and "09aa19e7c11c5d22944017ed954cd45081653834" have entirely different histories.
c6f54927b0
...
09aa19e7c1
@ -1,36 +0,0 @@
|
|||||||
package usync
|
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
// Locker guards access to a value
|
|
||||||
type Locker[T any] struct {
|
|
||||||
value T
|
|
||||||
mutex sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewLocker creates a new locker with the specified value.
|
|
||||||
func NewLocker[T any] (value T) Locker[T] {
|
|
||||||
return Locker[T] {
|
|
||||||
value: value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets the value of the locker.
|
|
||||||
func (this *Locker[T]) Set (value T) {
|
|
||||||
this.mutex.Lock()
|
|
||||||
defer this.mutex.Unlock()
|
|
||||||
this.value = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Borrow borrows the value from the locker, and returns a function that must
|
|
||||||
// immediately be deferred, like this:
|
|
||||||
//
|
|
||||||
// value, done := locker.Borrow()
|
|
||||||
// defer done()
|
|
||||||
//
|
|
||||||
// From the time Borrow is called to the time the done function is called, it is
|
|
||||||
// safe to access the locked object from within the current goroutine.
|
|
||||||
func (this *Locker[T]) Borrow () (T, func ()) {
|
|
||||||
this.mutex.Lock()
|
|
||||||
return this.value, this.mutex.Unlock
|
|
||||||
}
|
|
@ -4,15 +4,9 @@ import "slices"
|
|||||||
import "context"
|
import "context"
|
||||||
import "reflect"
|
import "reflect"
|
||||||
|
|
||||||
// ChannelRecv is a type constraint that includes all channel types that can be
|
|
||||||
// recieved from.
|
|
||||||
type ChannelRecv[T any] interface {
|
|
||||||
~chan T | ~<- chan T
|
|
||||||
}
|
|
||||||
|
|
||||||
// A type-safe wrapper around reflect.Select. Taken from:
|
// A type-safe wrapper around reflect.Select. Taken from:
|
||||||
// https://stackoverflow.com/questions/19992334
|
// https://stackoverflow.com/questions/19992334
|
||||||
func Select[T any, C ChannelRecv[T]] (ctx context.Context, channels ...C) (int, T, bool) {
|
func Select[T any] (ctx context.Context, channels ...chan T) (int, T, bool) {
|
||||||
var zero T
|
var zero T
|
||||||
|
|
||||||
// add all channels as select cases
|
// add all channels as select cases
|
||||||
@ -45,12 +39,11 @@ func Select[T any, C ChannelRecv[T]] (ctx context.Context, channels ...C) (int,
|
|||||||
return chosen, ret, true
|
return chosen, ret, true
|
||||||
}
|
}
|
||||||
return chosen, zero, false
|
return chosen, zero, false
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine returns a channel that continuously returns the result of the select
|
// Combine returns a channel that continuously returns the result of the select
|
||||||
// until all input sources are exhauste, or the context is canceled.
|
// until all input sources are exhauste, or the context is canceled.
|
||||||
func Combine[T any, C ChannelRecv[T]] (ctx context.Context, channels ...C) <- chan T {
|
func Combine[T any] (ctx context.Context, channels ...chan T) <- chan T {
|
||||||
channel := make(chan T)
|
channel := make(chan T)
|
||||||
// our silly slection routine
|
// our silly slection routine
|
||||||
go func () {
|
go func () {
|
||||||
|
Reference in New Issue
Block a user