Changeset 229 in code for trunk/ring.go


Ignore:
Timestamp:
Apr 6, 2020, 4:18:50 PM (5 years ago)
Author:
contact
Message:

Remove locks from ring buffer

Everything is now accessed from the user goroutine.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ring.go

    r228 r229  
    33import (
    44        "fmt"
    5         "sync"
    65
    76        "gopkg.in/irc.v3"
     
    1413        cap    uint64
    1514
    16         lock      sync.Mutex
    1715        cur       uint64
    1816        consumers []*RingConsumer
     
    3028// Produce appends a new message to the ring buffer.
    3129func (r *Ring) Produce(msg *irc.Message) {
    32         r.lock.Lock()
    33         defer r.lock.Unlock()
    34 
    3530        if r.closed {
    3631                panic("soju: Ring.Produce called after Close")
     
    4338
    4439func (r *Ring) Close() {
    45         r.lock.Lock()
    46         defer r.lock.Unlock()
    47 
    4840        if r.closed {
    4941                panic("soju: Ring.Close called twice")
     
    6355        consumer := &RingConsumer{ring: r}
    6456
    65         r.lock.Lock()
    6657        if seq != nil {
    6758                consumer.cur = *seq
     
    7061        }
    7162        r.consumers = append(r.consumers, consumer)
    72         r.lock.Unlock()
    7363
    7464        return consumer
     
    9686                panic("soju: RingConsumer.Peek called after Close")
    9787        }
    98 
    99         rc.ring.lock.Lock()
    100         defer rc.ring.lock.Unlock()
    10188
    10289        diff := rc.diff()
     
    130117// argument to Ring.NewConsumer to resume the message stream.
    131118func (rc *RingConsumer) Close() uint64 {
    132         rc.ring.lock.Lock()
    133119        for i := range rc.ring.consumers {
    134120                if rc.ring.consumers[i] == rc {
     
    137123                }
    138124        }
    139         rc.ring.lock.Unlock()
    140125
    141126        rc.closed = true
Note: See TracChangeset for help on using the changeset viewer.