1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-09-19 00:19:59 +00:00
arikawa/utils/moreatomic/mutex.go

38 lines
547 B
Go

package moreatomic
import (
"context"
"golang.org/x/sync/semaphore"
)
type BusyMutex struct {
sema semaphore.Weighted
}
func NewBusyMutex() *BusyMutex {
return &BusyMutex{
sema: *semaphore.NewWeighted(1),
}
}
func (m *BusyMutex) TryLock() bool {
return m.sema.TryAcquire(1)
}
func (m *BusyMutex) IsBusy() bool {
if !m.sema.TryAcquire(1) {
return false
}
m.sema.Release(1)
return true
}
func (m *BusyMutex) Lock(ctx context.Context) error {
return m.sema.Acquire(ctx, 1)
}
func (m *BusyMutex) Unlock() {
m.sema.Release(1)
}