File: synchronized_sorted_set.rb

package info (click to toggle)
ruby-bunny 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,644 kB
  • sloc: ruby: 10,256; sh: 70; makefile: 8
file content (56 lines) | stat: -rw-r--r-- 1,099 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require "set"
require "thread"

module Bunny
  module Concurrent
    # A SortedSet variation that synchronizes key mutation operations.
    #
    # @note This is NOT a complete SortedSet replacement. It only synchronizes operations needed by Bunny.
    # @api public
    class SynchronizedSortedSet < SortedSet
      def initialize(enum = nil)
        @mutex = Mutex.new

        super
      end

      def add(o)
        # avoid using Mutex#synchronize because of a Ruby 1.8.7-specific
        # bug that prevents super from being called from within a block. MK.
        @mutex.lock
        begin
          super
        ensure
          @mutex.unlock
        end
      end

      def delete(o)
        @mutex.lock
        begin
          super
        ensure
          @mutex.unlock
        end
      end

      def delete_if(&block)
        @mutex.lock
        begin
          super
        ensure
          @mutex.unlock
        end
      end

      def include?(o)
        @mutex.lock
        begin
          super
        ensure
          @mutex.unlock
        end
      end
    end
  end
end