Make granule of reuse the granule of release (avoid exporting 30 lines)

This commit is contained in:
Kristóf Tóth 2020-08-20 17:07:05 +02:00
parent ebbd1974df
commit 78c216486f
2 changed files with 10 additions and 7 deletions

View File

@ -1,7 +1,6 @@
package afterlock
import(
"after-lock/atomicflag"
"time"
"sync"
"errors"
@ -30,8 +29,8 @@ type AfterLock struct {
LoopDelay uint
keyboardDeviceNode string
wg sync.WaitGroup
stopFlag *atomicflag.AtomicFlag
keypressFlag *atomicflag.AtomicFlag
stopFlag *AtomicFlag
keypressFlag *AtomicFlag
Display Display
LockScreen Lockscreen
KeypressDetector KeypressDetector
@ -41,8 +40,8 @@ type AfterLock struct {
func New(keyboardDeviceNode string) *AfterLock {
return &AfterLock{
keyboardDeviceNode: keyboardDeviceNode,
stopFlag: atomicflag.New(false),
keypressFlag: atomicflag.New(false),
stopFlag: NewAtomicFlag(false),
keypressFlag: NewAtomicFlag(false),
}
}

View File

@ -1,25 +1,29 @@
package atomicflag
package afterlock
import (
"sync"
)
// AtomicFlag implements a simple, thread-safe boolean flag
type AtomicFlag struct {
value bool
mutex sync.RWMutex
}
func New(value bool) *AtomicFlag {
// NewAtomicFlag builds a new instance
func NewAtomicFlag(value bool) *AtomicFlag {
return &AtomicFlag{value: value}
}
// Get the flag
func (f *AtomicFlag) Get() bool {
f.mutex.RLock()
defer f.mutex.RUnlock()
return f.value
}
// Set the flag
func (f *AtomicFlag) Set(value bool) {
f.mutex.Lock()
defer f.mutex.Unlock()