• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-concurrency / concurrent-ruby / #2743

04 Mar 2015 02:59AM UTC coverage: 45.152% (-50.4%) from 95.548%
#2743

push

jdantonio
Merge pull request #258 from ruby-concurrency/clock_time

Closes #256

23 of 69 new or added lines in 7 files covered. (33.33%)

1563 existing lines in 88 files now uncovered.

1425 of 3156 relevant lines covered (45.15%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.89
/lib/concurrent/collection/blocking_ring_buffer.rb
1
require 'concurrent/atomic/condition'
1✔
2

3
module Concurrent
1✔
4
  class BlockingRingBuffer
1✔
5

6
    def initialize(capacity)
1✔
UNCOV
7
      @buffer = RingBuffer.new(capacity)
×
UNCOV
8
      @first = @last = 0
×
UNCOV
9
      @count = 0
×
UNCOV
10
      @mutex = Mutex.new
×
UNCOV
11
      @condition = Condition.new
×
12
    end
13

14
    # @return [Integer] the capacity of the buffer
15
    def capacity
1✔
UNCOV
16
      @mutex.synchronize { @buffer.capacity }
×
17
    end
18

19
    # @return [Integer] the number of elements currently in the buffer
20
    def count
1✔
UNCOV
21
      @mutex.synchronize { @buffer.count }
×
22
    end
23

24
    # @return [Boolean] true if buffer is empty, false otherwise
25
    def empty?
1✔
UNCOV
26
      @mutex.synchronize { @buffer.empty? }
×
27
    end
28

29
    # @return [Boolean] true if buffer is full, false otherwise
30
    def full?
1✔
UNCOV
31
      @mutex.synchronize { @buffer.full? }
×
32
    end
33

34
    # @param [Object] value the value to be inserted
35
    # @return [Boolean] true if value has been inserted, false otherwise
36
    def put(value)
1✔
UNCOV
37
      @mutex.synchronize do
×
UNCOV
38
        wait_while_full
×
UNCOV
39
        @buffer.offer(value)
×
UNCOV
40
        @condition.signal
×
UNCOV
41
        true
×
42
      end
43
    end
44

45
    # @return [Object] the first available value and removes it from the buffer.
46
    #   If buffer is empty it blocks until an element is available
47
    def take
1✔
UNCOV
48
      @mutex.synchronize do
×
UNCOV
49
        wait_while_empty
×
UNCOV
50
        result = @buffer.poll
×
UNCOV
51
        @condition.signal
×
UNCOV
52
        result
×
53
      end
54
    end
55

56
    # @return [Object] the first available value and without removing it from
57
    #   the buffer. If buffer is empty returns nil
58
    def peek
1✔
UNCOV
59
      @mutex.synchronize { @buffer.peek }
×
60
    end
61

62
    private
1✔
63

64
    def wait_while_full
1✔
UNCOV
65
      @condition.wait(@mutex) while @buffer.full?
×
66
    end
67

68
    def wait_while_empty
1✔
UNCOV
69
      @condition.wait(@mutex) while @buffer.empty?
×
70
    end
71

72
  end
73
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc