W3cubDocs

/Ruby 3

module MonitorMixin

Public Class Methods

extend_object(obj) Show source
# File ext/monitor/lib/monitor.rb, line 153
def self.extend_object(obj)
  super(obj)
  obj.__send__(:mon_initialize)
end
Calls superclass method
new(...) Show source
# File ext/monitor/lib/monitor.rb, line 223
def initialize(...)
  super
  mon_initialize
end

Use extend MonitorMixin or include MonitorMixin instead of this constructor. Have look at the examples above to understand how to use this module.

Calls superclass method

Public Instance Methods

mon_enter() Show source
# File ext/monitor/lib/monitor.rb, line 170
def mon_enter
  @mon_data.enter
end

Enters exclusive section.

mon_exit() Show source
# File ext/monitor/lib/monitor.rb, line 177
def mon_exit
  mon_check_owner
  @mon_data.exit
end

Leaves exclusive section.

mon_locked?() Show source
# File ext/monitor/lib/monitor.rb, line 185
def mon_locked?
  @mon_data.mon_locked?
end

Returns true if this monitor is locked by any thread

mon_owned?() Show source
# File ext/monitor/lib/monitor.rb, line 192
def mon_owned?
  @mon_data.mon_owned?
end

Returns true if this monitor is locked by current thread.

mon_synchronize(&b) Show source
# File ext/monitor/lib/monitor.rb, line 201
def mon_synchronize(&b)
  @mon_data.synchronize(&b)
end

Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin.

Also aliased as: synchronize
mon_try_enter() Show source
# File ext/monitor/lib/monitor.rb, line 161
def mon_try_enter
  @mon_data.try_enter
end

Attempts to enter exclusive section. Returns false if lock fails.

Also aliased as: try_mon_enter
new_cond() Show source
# File ext/monitor/lib/monitor.rb, line 210
def new_cond
  unless defined?(@mon_data)
    mon_initialize
    @mon_initialized_by_new_cond = true
  end
  return ConditionVariable.new(@mon_data)
end

Creates a new MonitorMixin::ConditionVariable associated with the Monitor object.

synchronize(&b)
Alias for: mon_synchronize
try_mon_enter()

For backward compatibility

Alias for: mon_try_enter

Private Instance Methods

mon_check_owner() Show source
# File ext/monitor/lib/monitor.rb, line 242
def mon_check_owner
  @mon_data.mon_check_owner
end
mon_initialize() Show source
# File ext/monitor/lib/monitor.rb, line 230
def mon_initialize
  if defined?(@mon_data)
    if defined?(@mon_initialized_by_new_cond)
      return # already initialized.
    elsif @mon_data_owner_object_id == self.object_id
      raise ThreadError, "already initialized"
    end
  end
  @mon_data = ::Monitor.new
  @mon_data_owner_object_id = self.object_id
end

Initializes the MonitorMixin after being included in a class or when an object has been extended with the MonitorMixin

Ruby Core © 1993–2020 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.