Skip to content

Commit

Permalink
add mutex also to sh global var
Browse files Browse the repository at this point in the history
  • Loading branch information
umbynos committed Oct 11, 2021
1 parent d3b1886 commit 4920955
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type serialhub struct {

// Unregister requests from connections.
unregister chan *serport

mu sync.Mutex
}

type SpPortList struct {
Expand Down Expand Up @@ -74,15 +76,19 @@ func (sh *serialhub) run() {
for {
select {
case p := <-sh.register:
sh.mu.Lock()
//log.Print("Registering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + ",\"BufferType\":\"" + p.BufferType + "\"}")
sh.ports[p] = true
sh.mu.Unlock()
case p := <-sh.unregister:
sh.mu.Lock()
//log.Print("Unregistering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + "}")
delete(sh.ports, p)
close(p.sendBuffered)
close(p.sendNoBuf)
sh.mu.Unlock()
case wr := <-sh.write:
// if user sent in the commands as one text mode line
write(wr)
Expand Down
2 changes: 2 additions & 0 deletions seriallist.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func GetList(network bool) ([]OsSerialPort, error) {

func findPortByName(portname string) (*serport, bool) {
portnamel := strings.ToLower(portname)
sh.mu.Lock()
defer sh.mu.Unlock()
for port := range sh.ports {
if strings.ToLower(port.portConf.Name) == portnamel {
// we found our port
Expand Down

0 comments on commit 4920955

Please sign in to comment.