Skip to content

Commit

Permalink
Mutexes not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Jul 17, 2024
1 parent 818c522 commit cec3daa
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions twin/screen-setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"

log "github.com/sirupsen/logrus"
Expand All @@ -21,16 +21,12 @@ type interruptableReaderImpl struct {
shutdownPipeReader *os.File
shutdownPipeWriter *os.File

lock sync.Mutex
interrupted bool
interrupted atomic.Bool
}

func (r *interruptableReaderImpl) Read(p []byte) (n int, err error) {
for {
r.lock.Lock()
interrupted := r.interrupted
r.lock.Unlock()
if interrupted {
if r.interrupted.Load() {
return 0, io.EOF
}

Expand Down Expand Up @@ -90,9 +86,7 @@ func (r *interruptableReaderImpl) read(p []byte) (n int, err error) {
}

func (r *interruptableReaderImpl) Interrupt() {
r.lock.Lock()
defer r.lock.Unlock()
r.interrupted = true
r.interrupted.Store(true)

err := r.shutdownPipeWriter.Close()
if err != nil {
Expand All @@ -104,7 +98,6 @@ func (r *interruptableReaderImpl) Interrupt() {
func newInterruptableReader(base *os.File) (interruptableReader, error) {
reader := interruptableReaderImpl{
base: base,
lock: sync.Mutex{},
}
pr, pw, err := os.Pipe()
if err != nil {
Expand Down

0 comments on commit cec3daa

Please sign in to comment.