File: mri_lockable_object.rb

package info (click to toggle)
ruby-concurrent 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,872 kB
  • ctags: 3,093
  • sloc: ruby: 26,166; java: 6,028; ansic: 282; makefile: 4
file content (71 lines) | stat: -rw-r--r-- 1,489 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Concurrent
  module Synchronization

    # @!visibility private
    # @!macro internal_implementation_note
    class MriLockableObject < AbstractLockableObject
      protected

      def ns_signal
        @__condition__.signal
        self
      end

      def ns_broadcast
        @__condition__.broadcast
        self
      end
    end


    # @!visibility private
    # @!macro internal_implementation_note
    class MriMutexLockableObject < MriLockableObject
      safe_initialization!

      def initialize(*defaults)
        super(*defaults)
        @__lock__      = ::Mutex.new
        @__condition__ = ::ConditionVariable.new
      end

      protected

      def synchronize
        if @__lock__.owned?
          yield
        else
          @__lock__.synchronize { yield }
        end
      end

      def ns_wait(timeout = nil)
        @__condition__.wait @__lock__, timeout
        self
      end
    end

    # @!visibility private
    # @!macro internal_implementation_note
    class MriMonitorLockableObject < MriLockableObject
      safe_initialization!

      def initialize(*defaults)
        super(*defaults)
        @__lock__      = ::Monitor.new
        @__condition__ = @__lock__.new_cond
      end

      protected

      def synchronize # TODO may be a problem with lock.synchronize { lock.wait }
        @__lock__.synchronize { yield }
      end

      def ns_wait(timeout = nil)
        @__condition__.wait timeout
        self
      end
    end
  end
end