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

View File

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