Skip to content

Commit 7a4f0a3

Browse files
committed
Removed goselect dependency in favor of sys/unix
1 parent 92703ec commit 7a4f0a3

File tree

5 files changed

+38
-34
lines changed

5 files changed

+38
-34
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module go.bug.st/serial
33
go 1.17
44

55
require (
6-
github.com/creack/goselect v0.1.2
76
github.com/stretchr/testify v1.8.4
87
golang.org/x/sys v0.17.0
98
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
2-
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
31
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
42
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
53
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

serial_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
281281
port.acquireExclusiveAccess()
282282

283283
// This pipe is used as a signal to cancel blocking Read
284-
pipe := &unixutils.Pipe{}
285-
if err := pipe.Open(); err != nil {
284+
if pipe, err := unixutils.NewPipe(); err != nil {
286285
port.Close()
287286
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error opening signaling pipe: %w", err)}
287+
} else {
288+
port.closeSignal = pipe
288289
}
289-
port.closeSignal = pipe
290290

291291
return port, nil
292292
}

unixutils/pipe.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ package unixutils
1010

1111
import (
1212
"fmt"
13-
"syscall"
13+
14+
"golang.org/x/sys/unix"
1415
)
1516

1617
// Pipe represents a unix-pipe
@@ -20,16 +21,17 @@ type Pipe struct {
2021
wr int
2122
}
2223

23-
// Open creates a new pipe
24-
func (p *Pipe) Open() error {
24+
// NewPipe creates a new pipe
25+
func NewPipe() (*Pipe, error) {
2526
fds := []int{0, 0}
26-
if err := syscall.Pipe(fds); err != nil {
27-
return err
27+
if err := unix.Pipe(fds); err != nil {
28+
return nil, err
2829
}
29-
p.rd = fds[0]
30-
p.wr = fds[1]
31-
p.opened = true
32-
return nil
30+
return &Pipe{
31+
rd: fds[0],
32+
wr: fds[1],
33+
opened: true,
34+
}, nil
3335
}
3436

3537
// ReadFD returns the file handle for the read side of the pipe.
@@ -53,24 +55,24 @@ func (p *Pipe) Write(data []byte) (int, error) {
5355
if !p.opened {
5456
return 0, fmt.Errorf("Pipe not opened")
5557
}
56-
return syscall.Write(p.wr, data)
58+
return unix.Write(p.wr, data)
5759
}
5860

5961
// Read from the pipe into the data array. Returns the number of bytes read.
6062
func (p *Pipe) Read(data []byte) (int, error) {
6163
if !p.opened {
6264
return 0, fmt.Errorf("Pipe not opened")
6365
}
64-
return syscall.Read(p.rd, data)
66+
return unix.Read(p.rd, data)
6567
}
6668

6769
// Close the pipe
6870
func (p *Pipe) Close() error {
6971
if !p.opened {
7072
return fmt.Errorf("Pipe not opened")
7173
}
72-
err1 := syscall.Close(p.rd)
73-
err2 := syscall.Close(p.wr)
74+
err1 := unix.Close(p.rd)
75+
err2 := unix.Close(p.wr)
7476
p.opened = false
7577
if err1 != nil {
7678
return err1

unixutils/select.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ package unixutils
1111
import (
1212
"time"
1313

14-
"github.com/creack/goselect"
14+
"golang.org/x/sys/unix"
1515
)
1616

1717
// FDSet is a set of file descriptors suitable for a select call
1818
type FDSet struct {
19-
set goselect.FDSet
20-
max uintptr
19+
set unix.FdSet
20+
max int
2121
}
2222

2323
// NewFDSet creates a set of file descriptors suitable for a Select call.
@@ -30,34 +30,33 @@ func NewFDSet(fds ...int) *FDSet {
3030
// Add adds the file descriptors passed as parameter to the FDSet.
3131
func (s *FDSet) Add(fds ...int) {
3232
for _, fd := range fds {
33-
f := uintptr(fd)
34-
s.set.Set(f)
35-
if f > s.max {
36-
s.max = f
33+
s.set.Set(fd)
34+
if fd > s.max {
35+
s.max = fd
3736
}
3837
}
3938
}
4039

4140
// FDResultSets contains the result of a Select operation.
4241
type FDResultSets struct {
43-
readable *goselect.FDSet
44-
writeable *goselect.FDSet
45-
errors *goselect.FDSet
42+
readable *unix.FdSet
43+
writeable *unix.FdSet
44+
errors *unix.FdSet
4645
}
4746

4847
// IsReadable test if a file descriptor is ready to be read.
4948
func (r *FDResultSets) IsReadable(fd int) bool {
50-
return r.readable.IsSet(uintptr(fd))
49+
return r.readable.IsSet(fd)
5150
}
5251

5352
// IsWritable test if a file descriptor is ready to be written.
5453
func (r *FDResultSets) IsWritable(fd int) bool {
55-
return r.writeable.IsSet(uintptr(fd))
54+
return r.writeable.IsSet(fd)
5655
}
5756

5857
// IsError test if a file descriptor is in error state.
5958
func (r *FDResultSets) IsError(fd int) bool {
60-
return r.errors.IsSet(uintptr(fd))
59+
return r.errors.IsSet(fd)
6160
}
6261

6362
// Select performs a select system call,
@@ -68,7 +67,7 @@ func (r *FDResultSets) IsError(fd int) bool {
6867
// The function return an FDResultSets that contains all the file descriptor
6968
// that have a pending read/write/error event.
7069
func Select(rd, wr, er *FDSet, timeout time.Duration) (*FDResultSets, error) {
71-
max := uintptr(0)
70+
max := 0
7271
res := &FDResultSets{}
7372
if rd != nil {
7473
// fdsets are copied so the parameters are left untouched
@@ -96,6 +95,12 @@ func Select(rd, wr, er *FDSet, timeout time.Duration) (*FDResultSets, error) {
9695
}
9796
}
9897

99-
err := goselect.Select(int(max+1), res.readable, res.writeable, res.errors, timeout)
98+
var err error
99+
if timeout != -1 {
100+
t := unix.NsecToTimeval(timeout.Nanoseconds())
101+
_, err = unix.Select(max+1, res.readable, res.writeable, res.errors, &t)
102+
} else {
103+
_, err = unix.Select(max+1, res.readable, res.writeable, res.errors, nil)
104+
}
100105
return res, err
101106
}

0 commit comments

Comments
 (0)