additional lock/unlocking issues

This commit is contained in:
Liam Stanley 2016-11-13 05:46:44 -05:00
parent c78113dc51
commit f0541cd3c6
3 changed files with 17 additions and 17 deletions

@ -2,6 +2,9 @@ package girc
// handleEvent runs the necessary callbacks for the incoming event
func (c *Client) handleEvent(event *Event) {
// log the event
c.log.Print(event.String())
// wildcard callbacks first
if callbacks, ok := c.callbacks[ALLEVENTS]; ok {
for i := 0; i < len(callbacks); i++ {

@ -215,11 +215,7 @@ func (c *Client) Wait() {
for {
select {
case event := <-c.Events:
// log the event
c.log.Print(event.String())
// run in the background
go c.handleEvent(event)
c.handleEvent(event)
case <-c.quitChan:
return
}

@ -44,43 +44,43 @@ func NewState() *State {
// createChanIfNotExists creates the channel in state, if not already done
func (s *State) createChanIfNotExists(channel string) {
s.m.Lock()
defer s.m.Unlock()
channel = strings.ToLower(channel)
// not a valid channel
if !IsValidChannel(channel) {
return
}
s.m.Lock()
if _, ok := s.channels[channel]; !ok {
s.channels[channel] = &Channel{
Name: strings.ToLower(channel),
Name: channel,
users: make(map[string]*User),
Joined: time.Now(),
}
}
s.m.Unlock()
}
// deleteChannel removes the channel from state, if not already done
func (s *State) deleteChannel(channel string) {
s.m.Lock()
defer s.m.Unlock()
channel = strings.ToLower(channel)
s.createChanIfNotExists(channel)
s.m.Lock()
if _, ok := s.channels[channel]; ok {
delete(s.channels, channel)
}
s.m.Unlock()
}
// createUserIfNotExists creates the channel and user in state,
// if not already done
func (s *State) createUserIfNotExists(channel, nick, ident, host string) {
s.m.Lock()
defer s.m.Unlock()
channel = strings.ToLower(channel)
s.createChanIfNotExists(channel)
s.m.Lock()
if _, ok := s.channels[channel].users[nick]; !ok {
s.channels[channel].users[nick] = &User{
Nick: nick,
@ -89,18 +89,19 @@ func (s *State) createUserIfNotExists(channel, nick, ident, host string) {
FirstSeen: time.Now(),
}
}
s.m.Unlock()
}
// deleteUser removes the user from channel state
func (s *State) deleteUser(channel, nick string) {
s.m.Lock()
defer s.m.Unlock()
channel = strings.ToLower(channel)
s.createChanIfNotExists(channel)
s.m.Lock()
if _, ok := s.channels[channel].users[nick]; ok {
delete(s.channels[channel].users, nick)
}
s.m.Unlock()
}
// renameUser renames the user in state, in all locations where