mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-17 20:32:48 +00:00
32789bb6e2
* Utils: move package utils/heart to internal/heart * Utils: move package utils/moreatomic to internal/moreatomic * Utils: move package utils/zlib to internal/zlib
53 lines
746 B
Go
53 lines
746 B
Go
package moreatomic
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type CtxMutex struct {
|
|
mut chan struct{}
|
|
}
|
|
|
|
func NewCtxMutex() *CtxMutex {
|
|
return &CtxMutex{
|
|
mut: make(chan struct{}, 1),
|
|
}
|
|
}
|
|
|
|
// func (m *CtxMutex) TryLock() bool {
|
|
// select {
|
|
// case m.mut <- struct{}{}:
|
|
// return true
|
|
// default:
|
|
// return false
|
|
// }
|
|
// }
|
|
|
|
// func (m *CtxMutex) IsBusy() bool {
|
|
// select {
|
|
// case m.mut <- struct{}{}:
|
|
// <-m.mut
|
|
// return false
|
|
// default:
|
|
// return true
|
|
// }
|
|
// }
|
|
|
|
func (m *CtxMutex) Lock(ctx context.Context) error {
|
|
select {
|
|
case m.mut <- struct{}{}:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
func (m *CtxMutex) Unlock() {
|
|
select {
|
|
case <-m.mut:
|
|
// return
|
|
default:
|
|
panic("Unlock of already unlocked mutex.")
|
|
}
|
|
}
|