Class: LightIO::Library::Thread

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, FallbackHelper
Defined in:
lib/lightio/library/mutex.rb,
lib/lightio/library/thread.rb

Defined Under Namespace

Modules: FallbackHelper Classes: Mutex

Constant Summary collapse

RAW_THREAD =
::Thread
ThreadError =

constants

::ThreadError
Queue =
LightIO::Library::Queue
Backtrace =
::Thread::Backtrace
SizedQueue =
LightIO::Library::SizedQueue

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FallbackHelper

fallback_main_thread_methods, included

Methods included from FallbackHelper::ClassMethods

#fallback_method, #fallback_thread_class_methods

Constructor Details

#initialize(*args, &blk) ⇒ Thread

Returns a new instance of Thread.



149
150
151
# File 'lib/lightio/library/thread.rb', line 149

def initialize(*args, &blk)
  init_core(*args, &blk)
end

Class Method Details

.currentObject



96
97
98
99
# File 'lib/lightio/library/thread.rb', line 96

def current
  return main if LightIO::Core::LightFiber.is_root?(Fiber.current)
  @current_thread || RAW_THREAD.current
end

.exclusive(&blk) ⇒ Object



101
102
103
# File 'lib/lightio/library/thread.rb', line 101

def exclusive(&blk)
  @thread_mutex.synchronize(&blk)
end

.finalizer(object_id) ⇒ Object



131
132
133
# File 'lib/lightio/library/thread.rb', line 131

def finalizer(object_id)
  proc {threads.delete(object_id)}
end

.fork(*args, &blk) ⇒ Object Also known as: start



84
85
86
87
88
# File 'lib/lightio/library/thread.rb', line 84

def fork(*args, &blk)
  obj = allocate
  obj.send(:init_core, *args, &blk)
  obj
end

.kill(thr) ⇒ Object



92
93
94
# File 'lib/lightio/library/thread.rb', line 92

def kill(thr)
  thr.kill
end

.listObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lightio/library/thread.rb', line 105

def list
  thread_list = []
  threads.keys.each {|id|
    begin
      thr = ObjectSpace._id2ref(id)
      unless thr.alive?
        # manually remove thr from threads
        thr.kill
        next
      end
      thread_list << thr
    rescue RangeError
      # mean object is recycled
      # just wait ruby GC call finalizer to remove it from threads
      next
    end
  }
  thread_list
end

.mainObject



135
136
137
# File 'lib/lightio/library/thread.rb', line 135

def main
  RAW_THREAD.main
end

.passObject Also known as: stop



125
126
127
# File 'lib/lightio/library/thread.rb', line 125

def pass
  LightIO::Beam.pass
end

Instance Method Details

#[](name) ⇒ Object



200
201
202
# File 'lib/lightio/library/thread.rb', line 200

def [](name)
  fiber_values[name.to_sym]
end

#[]=(name, val) ⇒ Object



204
205
206
# File 'lib/lightio/library/thread.rb', line 204

def []=(name, val)
  fiber_values[name.to_sym] = val
end

#groupObject



208
209
210
# File 'lib/lightio/library/thread.rb', line 208

def group
  @group
end

#inspectObject



212
213
214
# File 'lib/lightio/library/thread.rb', line 212

def inspect
  "#<LightIO::Library::Thread:0x00#{object_id.to_s(16)} #{status}>"
end

#join(limit = nil) ⇒ Object



216
217
218
# File 'lib/lightio/library/thread.rb', line 216

def join(limit=nil)
  @beam.join(limit) && self
end

#key?(sym) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/lightio/library/thread.rb', line 220

def key?(sym)
  fiber_values.has_key?(sym)
end

#keysObject



224
225
226
# File 'lib/lightio/library/thread.rb', line 224

def keys
  fiber_values.keys
end

#killObject Also known as: exit, terminate



167
168
169
# File 'lib/lightio/library/thread.rb', line 167

def kill
  @beam.kill && self
end

#raise(exception, message = nil, backtrace = nil) ⇒ Object



228
229
230
# File 'lib/lightio/library/thread.rb', line 228

def raise(exception, message=nil, backtrace=nil)
  @beam.raise(LightIO::Beam::BeamError.new(exception), message, backtrace)
end

#runObject Also known as: wakeup



232
233
234
235
# File 'lib/lightio/library/thread.rb', line 232

def run
  Kernel.raise ThreadError, 'killed thread' unless alive?
  Thread.pass
end

#statusObject



174
175
176
177
178
179
180
181
182
# File 'lib/lightio/library/thread.rb', line 174

def status
  if Thread.current == self
    'run'
  elsif alive?
    @beam.error.nil? ? 'sleep' : 'abouting'
  else
    @beam.error.nil? ? false : nil
  end
end

#stop?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/lightio/library/thread.rb', line 239

def stop?
  !alive? || status == 'sleep'
end

#thread_variable?(key) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/lightio/library/thread.rb', line 196

def thread_variable?(key)
  thread_values.key?(key)
end

#thread_variable_get(name) ⇒ Object



188
189
190
# File 'lib/lightio/library/thread.rb', line 188

def thread_variable_get(name)
  thread_values[name.to_sym]
end

#thread_variable_set(name, value) ⇒ Object



192
193
194
# File 'lib/lightio/library/thread.rb', line 192

def thread_variable_set(name, value)
  thread_values[name.to_sym] = value
end

#thread_variablesObject



184
185
186
# File 'lib/lightio/library/thread.rb', line 184

def thread_variables
  thread_values.keys
end