Class: Synapse::DisposableLock Private

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/common/concurrency/disposable_lock.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeundefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
19
20
21
22
23
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 16

def initialize
  @mutex = Mutex.new

  @closed = false
  @hold_count = 0
  @owner = nil
  @waiters = Array.new
end

Instance Attribute Details

#closedBoolean (readonly) Also known as: closed?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


5
6
7
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 5

def closed
  @closed
end

#ownerThread (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Thread)


10
11
12
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 10

def owner
  @owner
end

#waitersArray (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array)


13
14
15
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 13

def waiters
  @waiters
end

Instance Method Details

#lockBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns False if lock has been closed.

Returns:

  • (Boolean)

    False if lock has been closed



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
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 42

def lock
  @mutex.synchronize do
    unless owned?
      if @hold_count == 0
        @owner = Thread.current
      else
        @waiters.push Thread.current

        begin
          wait_for_lock
        ensure
          @waiters.delete Thread.current
        end

        return unless owned?
      end
    end

    @hold_count += 1
  end

  if closed?
    unlock
    return false
  end

  return true
end

#locked?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


37
38
39
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 37

def locked?
  @hold_count > 0
end

#owned?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


26
27
28
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 26

def owned?
  @owner == Thread.current
end

#owned_by?(thread) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • thread (Thread)

Returns:

  • (Boolean)


32
33
34
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 32

def owned_by?(thread)
  @owner == thread
end

#try_closeBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns True if lock was closed.

Returns:

  • (Boolean)

    True if lock was closed



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 103

def try_close
  return false unless try_lock

  begin
    if @hold_count == 1
      @closed = true
      return true
    end

    return false
  ensure
    unlock
  end
end

#try_lockBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 72

def try_lock
  @mutex.synchronize do
    unless owned?
      if @hold_count == 0
        @owner = Thread.current
      else
        return false
      end
    end

    @hold_count += 1
    return true
  end
end

#unlockundefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (undefined)

Raises:

  • (RuntimeError)

    If caller does not own the lock



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/synapse/common/concurrency/disposable_lock.rb', line 89

def unlock
  @mutex.synchronize do
    raise RuntimeError unless owned?

    @hold_count -= 1

    if @hold_count == 0
      @owner = nil
      wakeup_next_waiter
    end
  end
end