3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-26 13:59:44 +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.destroyed = true
channel.members = make(ClientSet) channel.members = make(ClientSet)
}) })
channel.server.withMutex(func() {
channel.server.channels.Remove(channel) channel.server.channels.Remove(channel)
})
} }
func (channel *Channel) Command(command ChannelCommand) { func (channel *Channel) Command(command ChannelCommand) {
@ -112,14 +115,14 @@ func (channel *Channel) receiveReplies(replies <-chan Reply) {
if DEBUG_CHANNEL { if DEBUG_CHANNEL {
log.Printf("%s ← %s %s", channel, reply.Source(), reply) log.Printf("%s ← %s %s", channel, reply.Source(), reply)
} }
channel.mutex.Lock() channel.withMutex(func() {
for client := range channel.members { for client := range channel.members {
if IsPrivMsg(reply) && (reply.Source() == Identifier(client)) { if IsPrivMsg(reply) && (reply.Source() == Identifier(client)) {
continue continue
} }
client.Reply(reply) client.Reply(reply)
} }
channel.mutex.Unlock() })
} }
} }

View File

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