From 213afc0481356e1393d89626a5d3bd3a1fa0aa90 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Sat, 15 Feb 2014 20:01:28 -0800 Subject: [PATCH] fix another race --- irc/channel.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/irc/channel.go b/irc/channel.go index 8a42be7f..fb934040 100644 --- a/irc/channel.go +++ b/irc/channel.go @@ -186,16 +186,22 @@ func (channel *Channel) ModeString() (str string) { return } -func (channel *Channel) Join(client *Client) { +func (channel *Channel) withMutex(f func()) { channel.mutex.Lock() - channel.members.Add(client) - if len(channel.members) == 1 { - channel.members[client][ChannelCreator] = true - channel.members[client][ChannelOperator] = true - } - channel.mutex.Unlock() + defer channel.mutex.Unlock() + f() +} + +func (channel *Channel) Join(client *Client) { + channel.withMutex(func() { + channel.members.Add(client) + if len(channel.members) == 1 { + channel.members[client][ChannelCreator] = true + channel.members[client][ChannelOperator] = true + } + client.channels.Add(channel) + }) - client.channels.Add(channel) channel.Reply(RplJoin(client, channel)) channel.GetTopic(client) channel.GetUsers(client)