photoprism/internal/mutex/busy.go
Michael Mayer b37d4472e4 Backend: Use original file if thumb size exceeds limit #172
Plus some mutex and config refactoring along the way...

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-01-08 19:51:21 +01:00

62 lines
748 B
Go

package mutex
import (
"errors"
"sync"
)
type Busy struct {
busy bool
canceled bool
mutex sync.Mutex
}
func (b *Busy) Busy() bool {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.busy
}
func (b *Busy) Start() error {
b.mutex.Lock()
defer b.mutex.Unlock()
if b.canceled {
return errors.New("still running")
}
if b.busy {
return errors.New("already running")
}
b.busy = true
b.canceled = false
return nil
}
func (b *Busy) Stop() {
b.mutex.Lock()
defer b.mutex.Unlock()
b.busy = false
b.canceled = false
}
func (b *Busy) Cancel() {
b.mutex.Lock()
defer b.mutex.Unlock()
if b.busy {
b.canceled = true
}
}
func (b *Busy) Canceled() bool {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.canceled
}