class Thread

Thread extensions

Attributes

backend[RW]
main_fiber[R]
result[R]

Public Class Methods

backend() click to toggle source
VALUE Thread_class_backend(VALUE _self) {
  return rb_ivar_get(rb_thread_current(), ID_ivar_backend);
}
new(*args, &block) click to toggle source
# File lib/polyphony/extensions/thread.rb, line 10
def initialize(*args, &block)
  @join_wait_queue = []
  @finalization_mutex = Mutex.new
  @args = args
  @block = block
  orig_initialize { execute }
end
Also aliased as: orig_initialize

Public Instance Methods

<<(value) click to toggle source
# File lib/polyphony/extensions/thread.rb, line 104
def <<(value)
  main_fiber << value
end
Also aliased as: send
await(timeout = nil)
Alias for: join
debug!() click to toggle source
VALUE Thread_debug(VALUE self) {
  rb_ivar_set(self, rb_intern("@__debug__"), Qtrue);
  return self;
}
execute() click to toggle source
# File lib/polyphony/extensions/thread.rb, line 18
def execute
  # backend must be created in the context of the new thread, therefore it
  # cannot be created in Thread#initialize
  @backend = Polyphony::Backend.new
  setup
  @ready = true
  result = @block.(*@args)
rescue Polyphony::MoveOn, Polyphony::Terminate => e
  result = e.value
rescue Exception => e
  result = e
ensure
  @ready = true
  finalize(result)
end
fiber_unschedule(p1) click to toggle source
VALUE Thread_fiber_unschedule(VALUE self, VALUE fiber) {
  Backend_unschedule_fiber(rb_ivar_get(self, ID_ivar_backend), fiber);
  return self;
}
finalize(result) click to toggle source
# File lib/polyphony/extensions/thread.rb, line 42
def finalize(result)
  unless Fiber.current.children.empty?
    Fiber.current.shutdown_all_children
  end
  @finalization_mutex.synchronize do
    @terminated = true
    @result = result
    signal_waiters(result)
  end
  @backend.finalize
end
idle_gc_period=(period) click to toggle source
# File lib/polyphony/extensions/thread.rb, line 109
def idle_gc_period=(period)
  backend.idle_gc_period = period
end
inspect() click to toggle source
# File lib/polyphony/extensions/thread.rb, line 92
def inspect
  return orig_inspect if self == Thread.main

  state = status || 'dead'
  "#<Thread:#{object_id} #{location} (#{state})>"
end
Also aliased as: orig_inspect, to_s
join(timeout = nil) click to toggle source
# File lib/polyphony/extensions/thread.rb, line 59
def join(timeout = nil)
  watcher = Fiber.current.auto_watcher

  @finalization_mutex.synchronize do
    if @terminated
      @result.is_a?(Exception) ? (raise @result) : (return @result)
    else
      @join_wait_queue << watcher
    end
  end
  timeout ? move_on_after(timeout) { watcher.await } : watcher.await
end
Also aliased as: orig_join, await
kill() click to toggle source
# File lib/polyphony/extensions/thread.rb, line 85
def kill
  return if @terminated

  raise Polyphony::Terminate
end
Also aliased as: orig_kill
location() click to toggle source
# File lib/polyphony/extensions/thread.rb, line 100
def location
  @block.source_location.join(':')
end
on_idle(&block) click to toggle source
# File lib/polyphony/extensions/thread.rb, line 113
def on_idle(&block)
  backend.idle_proc = block
end
orig_initialize(*args, &block)
Alias for: new
orig_inspect()
Alias for: inspect
orig_join(timeout = nil)
Alias for: join
orig_kill()
Alias for: kill
orig_raise(error = nil)
Alias for: raise
raise(error = nil) click to toggle source
# File lib/polyphony/extensions/thread.rb, line 74
def raise(error = nil)
  Thread.pass until @main_fiber
  error = RuntimeError.new if error.nil?
  error = RuntimeError.new(error) if error.is_a?(String)
  error = error.new if error.is_a?(Class)

  sleep 0.0001 until @ready
  main_fiber&.raise(error)
end
Also aliased as: orig_raise
schedule_and_wakeup(p1, p2) click to toggle source
VALUE Thread_fiber_schedule_and_wakeup(VALUE self, VALUE fiber, VALUE resume_obj) {
  if (fiber != Qnil) {
    Thread_schedule_fiber_with_priority(self, fiber, resume_obj);
  }

  if (Backend_wakeup(rb_ivar_get(self, ID_ivar_backend)) == Qnil) {
    // we're not inside the ev_loop, so we just do a switchpoint
    Thread_switch_fiber(self);
  }

  return self;
}
send(value)
Alias for: <<
setup() click to toggle source
# File lib/polyphony/extensions/thread.rb, line 36
def setup
  @main_fiber = Fiber.current
  @main_fiber.setup_main_fiber
  setup_fiber_scheduling
end
setup_fiber_scheduling() click to toggle source
static VALUE Thread_setup_fiber_scheduling(VALUE self) {
  rb_ivar_set(self, ID_ivar_main_fiber, rb_fiber_current());
  return self;
}
signal_waiters(result) click to toggle source
# File lib/polyphony/extensions/thread.rb, line 54
def signal_waiters(result)
  @join_wait_queue.each { |w| w.signal(result) }
end
switch_fiber() click to toggle source
VALUE Thread_switch_fiber(VALUE self) {
  return Backend_switch_fiber(rb_ivar_get(self, ID_ivar_backend));
}
to_s()
Alias for: inspect