Skip to content

Commit 4920955

Browse files
committed
add mutex also to sh global var
1 parent d3b1886 commit 4920955

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

serial.go

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type serialhub struct {
3030

3131
// Unregister requests from connections.
3232
unregister chan *serport
33+
34+
mu sync.Mutex
3335
}
3436

3537
type SpPortList struct {
@@ -74,15 +76,19 @@ func (sh *serialhub) run() {
7476
for {
7577
select {
7678
case p := <-sh.register:
79+
sh.mu.Lock()
7780
//log.Print("Registering a port: ", p.portConf.Name)
7881
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + ",\"BufferType\":\"" + p.BufferType + "\"}")
7982
sh.ports[p] = true
83+
sh.mu.Unlock()
8084
case p := <-sh.unregister:
85+
sh.mu.Lock()
8186
//log.Print("Unregistering a port: ", p.portConf.Name)
8287
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + "}")
8388
delete(sh.ports, p)
8489
close(p.sendBuffered)
8590
close(p.sendNoBuf)
91+
sh.mu.Unlock()
8692
case wr := <-sh.write:
8793
// if user sent in the commands as one text mode line
8894
write(wr)

seriallist.go

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func GetList(network bool) ([]OsSerialPort, error) {
7171

7272
func findPortByName(portname string) (*serport, bool) {
7373
portnamel := strings.ToLower(portname)
74+
sh.mu.Lock()
75+
defer sh.mu.Unlock()
7476
for port := range sh.ports {
7577
if strings.ToLower(port.portConf.Name) == portnamel {
7678
// we found our port

0 commit comments

Comments
 (0)