Module: Zoidberg::HardShell

Defined in:
lib/zoidberg/shell.rb

Overview

Confined proxy based shell

Defined Under Namespace

Classes: AsyncProxy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/zoidberg/shell.rb', line 233

def self.included(klass)
  unless(klass.include?(::Zoidberg::Shell))
    klass.class_eval do
      include ::Zoidberg::Shell
    end
  end
end

Instance Method Details

#async(locked = false, &block) ⇒ AsyncProxy, NilClass

Perform an async action

Parameters:

  • (defaults to: false)

    lock when running

Returns:



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/zoidberg/shell.rb', line 206

def async(locked=false, &block)
  if(block)
    if(locked)
      current_self.instance_exec(&block)
    else
      current_self._async_request(locked ? :blocking : :nonblocking, :instance_exec, &block)
    end
  else
    ::Zoidberg::HardShell::AsyncProxy.new(current_self, locked)
  end
end

#defer { ... } ⇒ Object

Unlock current lock on instance and execute given block without locking

Yields:

  • block to execute without lock

Returns:

  • result of block



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/zoidberg/shell.rb', line 188

def defer(&block)
  if(current_self.threaded?)
    action = Task.new(:async, current_self){ block.call }
    current_self.task_defer(action)
    Thread.stop
    action.value
  else
    Fiber.yield
    if(block)
      ::Fiber.new(&block).resume
    end
  end
end

#sleep(length = nil) ⇒ Float

Provide a customized sleep behavior which will unlock the real instance while sleeping

Parameters:

  • (defaults to: nil)

Returns:



223
224
225
226
227
228
229
230
231
# File 'lib/zoidberg/shell.rb', line 223

def sleep(length=nil)
  start_time = ::Time.now.to_f
  if(length)
    defer{ ::Kernel.sleep(length) }
  else
    ::Thread.current[:root_fiber] == ::Fiber.current ? ::Kernel.sleep : ::Fiber.yield
  end
  ::Time.now.to_f - start_time
end