class LightIO::Library::Thread

Constants

Backtrace
Queue
SizedQueue
ThreadError

constants

Public Class Methods

new(*args, &blk) click to toggle source
# File lib/lightio/library/thread.rb, line 48
def initialize(*args, &blk)
  init_core(*args, &blk)
end

Private Class Methods

method_missing(*args) click to toggle source
# File lib/lightio/library/thread.rb, line 203
def method_missing(*args)
  ::Thread.__send__(*args)
end
respond_to?(*args) click to toggle source
# File lib/lightio/library/thread.rb, line 207
def respond_to?(*args)
  ::Thread.respond_to?(*args)
end
respond_to_missing?(method, *) click to toggle source
# File lib/lightio/library/thread.rb, line 211
def respond_to_missing?(method, *)
  ::Thread.respond_to?(method)
end
thread_mutex() click to toggle source
# File lib/lightio/library/thread.rb, line 223
def thread_mutex
  mutex = Thread.instance_variable_get(:@thread_mutex)
  mutex || Thread.instance_variable_set(:@thread_mutex, LightIO::Library::Mutex.new)
end
threads() click to toggle source

threads and threads variables

# File lib/lightio/library/thread.rb, line 218
def threads
  thrs = Thread.instance_variable_get(:@threads)
  thrs || Thread.instance_variable_set(:@threads, {})
end

Public Instance Methods

[](name) click to toggle source
# File lib/lightio/library/thread.rb, line 98
def [](name)
  fiber_values[name.to_sym]
end
[]=(name, val) click to toggle source
# File lib/lightio/library/thread.rb, line 102
def []=(name, val)
  fiber_values[name.to_sym] = val
end
exit()
Alias for: kill
group() click to toggle source
# File lib/lightio/library/thread.rb, line 106
def group
  @group
end
inspect() click to toggle source
# File lib/lightio/library/thread.rb, line 110
def inspect
  "#<LightIO::Library::Thread:0x00#{object_id.to_s(16)} #{status}>"
end
join(limit=nil) click to toggle source
# File lib/lightio/library/thread.rb, line 114
def join(limit=nil)
  @beam.join(limit) && self
end
key?(sym) click to toggle source
# File lib/lightio/library/thread.rb, line 118
def key?(sym)
  fiber_values.has_key?(sym)
end
keys() click to toggle source
# File lib/lightio/library/thread.rb, line 122
def keys
  fiber_values.keys
end
kill() click to toggle source
# File lib/lightio/library/thread.rb, line 65
def kill
  @beam.kill && self
end
Also aliased as: exit, terminate
raise(exception, message=nil, backtrace=nil) click to toggle source
# File lib/lightio/library/thread.rb, line 126
def raise(exception, message=nil, backtrace=nil)
  @beam.raise(LightIO::Beam::BeamError.new(exception), message, backtrace)
end
run() click to toggle source
# File lib/lightio/library/thread.rb, line 130
def run
  Kernel.raise ThreadError, 'killed thread' unless alive?
  Thread.pass
end
Also aliased as: wakeup
status() click to toggle source
# File lib/lightio/library/thread.rb, line 72
def status
  if self.class.current == self
    'run'
  elsif alive?
    @beam.error.nil? ? 'sleep' : 'abouting'
  else
    @beam.error.nil? ? false : nil
  end
end
stop?() click to toggle source
# File lib/lightio/library/thread.rb, line 137
def stop?
  !alive? || status == 'sleep'
end
terminate()
Alias for: kill
thread_variable?(key) click to toggle source
# File lib/lightio/library/thread.rb, line 94
def thread_variable?(key)
  thread_values.key?(key)
end
thread_variable_get(name) click to toggle source
# File lib/lightio/library/thread.rb, line 86
def thread_variable_get(name)
  thread_values[name.to_sym]
end
thread_variable_set(name, value) click to toggle source
# File lib/lightio/library/thread.rb, line 90
def thread_variable_set(name, value)
  thread_values[name.to_sym] = value
end
thread_variables() click to toggle source
# File lib/lightio/library/thread.rb, line 82
def thread_variables
  thread_values.keys
end
wakeup()
Alias for: run

Private Instance Methods

add_to_group(group) click to toggle source

add self to thread group

# File lib/lightio/library/thread.rb, line 155
def add_to_group(group)
  # remove from old group
  remove_from_group
  @group = group
  @group.send(:threads) << self
end
fiber_values() click to toggle source
# File lib/lightio/library/thread.rb, line 184
def fiber_values
  beam_or_fiber = LightIO::Beam.current
  # only consider non-root fiber
  if !beam_or_fiber.instance_of?(::Fiber) || LightIO::LightFiber.is_root?(beam_or_fiber)
    beam_or_fiber = @beam
  end
  fibers_and_values[beam_or_fiber] ||= {}
end
fibers_and_values() click to toggle source
# File lib/lightio/library/thread.rb, line 180
def fibers_and_values
  @fibers_and_values ||= {}
end
init_core(*args, &blk) click to toggle source
# File lib/lightio/library/thread.rb, line 142
def init_core(*args, &blk)
  @beam = LightIO::Beam.new(*args, &blk)
  @beam.on_dead = proc {on_dead}
  @beam.on_transfer = proc {|from, to| on_transfer(from, to)}
  # register this thread
  thread_values
  # add self to ThreadGroup::Default
  add_to_group(LightIO::Library::ThreadGroup::Default)
  # remove thread and thread variables
  ObjectSpace.define_finalizer(self, LightIO::Library::Thread.finalizer(self.object_id))
end
on_dead() click to toggle source
# File lib/lightio/library/thread.rb, line 167
def on_dead
  # release references
  remove_from_group
end
on_transfer(from, to) click to toggle source
# File lib/lightio/library/thread.rb, line 172
def on_transfer(from, to)
  Thread.instance_variable_set(:@current_thread, self)
end
remove_from_group() click to toggle source

remove thread from group when dead

# File lib/lightio/library/thread.rb, line 163
def remove_from_group
  @group.send(:threads).delete(self) if @group
end
thread_values() click to toggle source
# File lib/lightio/library/thread.rb, line 176
def thread_values
  Thread.send(:threads)[object_id] ||= {}
end