3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +01:00

use mutexes more

This commit is contained in:
Jeremy Latt 2014-02-16 10:42:25 -08:00
parent 81a0f19390
commit 74b8221db7
2 changed files with 23 additions and 12 deletions

View File

@ -59,7 +59,10 @@ func (channel *Channel) Destroy() {
channel.destroyed = true
channel.members = make(ClientSet)
})
channel.server.channels.Remove(channel)
channel.server.withMutex(func() {
channel.server.channels.Remove(channel)
})
}
func (channel *Channel) Command(command ChannelCommand) {
@ -112,14 +115,14 @@ func (channel *Channel) receiveReplies(replies <-chan Reply) {
if DEBUG_CHANNEL {
log.Printf("%s ← %s %s", channel, reply.Source(), reply)
}
channel.mutex.Lock()
for client := range channel.members {
if IsPrivMsg(reply) && (reply.Source() == Identifier(client)) {
continue
channel.withMutex(func() {
for client := range channel.members {
if IsPrivMsg(reply) && (reply.Source() == Identifier(client)) {
continue
}
client.Reply(reply)
}
client.Reply(reply)
}
channel.mutex.Unlock()
})
}
}

View File

@ -51,6 +51,12 @@ func NewServer(config *Config) *Server {
return server
}
func (server *Server) withMutex(f func()) {
server.mutex.Lock()
defer server.mutex.Unlock()
f()
}
func (server *Server) receiveCommands() {
for command := range server.commands {
if DEBUG_SERVER {
@ -139,7 +145,9 @@ func (s *Server) GetOrMakeChannel(name string) *Channel {
if !ok {
channel = NewChannel(s, name)
s.channels[name] = channel
s.withMutex(func() {
s.channels[name] = channel
})
}
return channel
@ -331,9 +339,9 @@ func (m *QuitCommand) HandleServer(server *Server) {
iclients.Remove(client)
for channel := range client.channels {
channel.mutex.Lock()
channel.members.Remove(client)
channel.mutex.Unlock()
channel.withMutex(func() {
channel.members.Remove(client)
})
}
client.Reply(RplError(server, client))