Class: Polyphony::Monitor

Inherits:
Mutex show all
Defined in:
lib/polyphony/core/sync.rb

Overview

Implements a fiber-aware Monitor class. This class replaces the stock Monitor class.

Instance Method Summary collapse

Methods inherited from Mutex

#conditional_reacquire, #conditional_release, #initialize, #lock, #locked?, #owned?, #sleep, #synchronize, #try_lock, #unlock

Constructor Details

This class inherits a constructor from Polyphony::Mutex

Instance Method Details

#enterObject Also known as: mon_enter



139
140
141
142
143
144
145
146
# File 'lib/polyphony/core/sync.rb', line 139

def enter
  if @holding_fiber == Fiber.current
    @holding_count += 1
  else
    lock
    @holding_count = 1
  end
end

#exitObject Also known as: mon_exit

Raises:

  • (ThreadError)


149
150
151
152
153
154
# File 'lib/polyphony/core/sync.rb', line 149

def exit
  raise ThreadError if !owned?

  @holding_count -= 1
  unlock if @holding_count == 0
end

#mon_check_ownerObject



157
158
159
160
161
162
163
# File 'lib/polyphony/core/sync.rb', line 157

def mon_check_owner
  if Fiber.current == @holding_fiber
    nil
  else
    raise ThreadError, 'current fiber not owner'
  end
end

#mon_locked?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/polyphony/core/sync.rb', line 165

def mon_locked?
  !!@holding_fiber
end

#mon_owned?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/polyphony/core/sync.rb', line 169

def mon_owned?
  @holding_fiber == Fiber.current
end

#new_condObject



175
176
177
# File 'lib/polyphony/core/sync.rb', line 175

def new_cond
  MonitorMixin::ConditionVariable.new(self)
end

#try_enterObject Also known as: try_mon_enter



179
180
181
182
183
184
185
# File 'lib/polyphony/core/sync.rb', line 179

def try_enter
  check_dead_holder
  return false if @holding_fiber

  enter
  true
end

#wait_for_cond(cond, timeout) ⇒ Object



188
189
190
# File 'lib/polyphony/core/sync.rb', line 188

def wait_for_cond(cond, timeout)
  cond.wait(self, timeout)
end