Browse Source

fix more data races

tags/v0.1.0
Jeremy Latt 10 years ago
parent
commit
9600be82a3
2 changed files with 10 additions and 0 deletions
  1. 5
    0
      irc/channel.go
  2. 5
    0
      irc/server.go

+ 5
- 0
irc/channel.go View File

@@ -2,6 +2,7 @@ package irc
2 2
 
3 3
 import (
4 4
 	"log"
5
+	"sync"
5 6
 )
6 7
 
7 8
 type Channel struct {
@@ -10,6 +11,7 @@ type Channel struct {
10 11
 	destroyed bool
11 12
 	key       string
12 13
 	members   ClientSet
14
+	mutex     *sync.Mutex
13 15
 	name      string
14 16
 	noOutside bool
15 17
 	password  string
@@ -38,6 +40,7 @@ func NewChannel(s *Server, name string) *Channel {
38 40
 		banList:  make([]UserMask, 0),
39 41
 		commands: commands,
40 42
 		members:  make(ClientSet),
43
+		mutex:    &sync.Mutex{},
41 44
 		name:     name,
42 45
 		replies:  replies,
43 46
 		server:   s,
@@ -92,11 +95,13 @@ func (channel *Channel) receiveReplies(replies <-chan Reply) {
92 95
 		if DEBUG_CHANNEL {
93 96
 			log.Printf("%s ← %s %s", channel, reply.Source(), reply)
94 97
 		}
98
+		channel.mutex.Lock()
95 99
 		for client := range channel.members {
96 100
 			if reply.Source() != Identifier(client) {
97 101
 				client.Reply(reply)
98 102
 			}
99 103
 		}
104
+		channel.mutex.Unlock()
100 105
 	}
101 106
 }
102 107
 

+ 5
- 0
irc/server.go View File

@@ -9,6 +9,7 @@ import (
9 9
 	"log"
10 10
 	"net"
11 11
 	"os"
12
+	"sync"
12 13
 	"time"
13 14
 )
14 15
 
@@ -17,6 +18,7 @@ type Server struct {
17 18
 	commands  chan Command
18 19
 	ctime     time.Time
19 20
 	motdFile  string
21
+	mutex     *sync.Mutex
20 22
 	name      string
21 23
 	operators map[string]string
22 24
 	password  string
@@ -30,6 +32,7 @@ func NewServer(config *Config) *Server {
30 32
 		commands:  make(chan Command),
31 33
 		ctime:     time.Now(),
32 34
 		motdFile:  config.MOTD,
35
+		mutex:     &sync.Mutex{},
33 36
 		name:      config.Name,
34 37
 		operators: make(map[string]string),
35 38
 		password:  config.Password,
@@ -296,7 +299,9 @@ func (m *QuitCommand) HandleServer(server *Server) {
296 299
 	iclients.Remove(client)
297 300
 
298 301
 	for channel := range client.channels {
302
+		channel.mutex.Lock()
299 303
 		channel.members.Remove(client)
304
+		channel.mutex.Unlock()
300 305
 	}
301 306
 
302 307
 	client.Reply(RplError(server, client))

Loading…
Cancel
Save