Skip to content

Commit

Permalink
Defer mutex unlock in Receive().
Browse files Browse the repository at this point in the history
  • Loading branch information
awoodbeck committed Jun 8, 2016
1 parent 638b5a8 commit 3c45fd3
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions mailbox.go
Expand Up @@ -578,7 +578,7 @@ func (m *mailboxes) newLocalMailbox() (Address, *Mailbox) {
cond := sync.NewCond(&mutex)

nextID := mailboxID(atomic.AddUint64((*uint64)(&m.nextMailboxID), 1))
m.nextMailboxID += 1
m.nextMailboxID++
id := nextID<<8 + mailboxID(m.nodeID)

mailbox := &Mailbox{
Expand Down Expand Up @@ -716,17 +716,16 @@ func (m *Mailbox) ReceiveNext() interface{} {
// regardless of the behavior of the matcher.
func (m *Mailbox) Receive(matcher func(interface{}) bool) interface{} {
m.cond.L.Lock()
defer m.cond.L.Unlock()

if m.terminated {
m.cond.L.Unlock()
return MailboxTerminated(m.id)
}

// see if there are any messages that match
for i, v := range m.messages {
if matcher(v.msg) {
m.messages = append(m.messages[:i], m.messages[i+1:]...)
m.cond.L.Unlock()
return v.msg
}
}
Expand All @@ -743,15 +742,13 @@ func (m *Mailbox) Receive(matcher func(interface{}) bool) interface{} {
}

if m.terminated {
m.cond.L.Unlock()
return MailboxTerminated(m.id)
}

for ; lastIdx < len(m.messages); lastIdx++ {
if matcher(m.messages[lastIdx].msg) {
match := m.messages[lastIdx].msg
m.messages = append(m.messages[:lastIdx], m.messages[lastIdx+1:]...)
m.cond.L.Unlock()
return match
}
}
Expand Down

0 comments on commit 3c45fd3

Please sign in to comment.