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



194
195
196
197
198
199
200
# File 'lib/zoidberg/shell.rb', line 194

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:

  • locked (Truthy, Falsey) (defaults to: false)

    lock when running

Returns:



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/zoidberg/shell.rb', line 167

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:

  • (Object)

    result of block



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/zoidberg/shell.rb', line 149

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:

  • length (Numeric, NilClass) (defaults to: nil)

Returns:

  • (Float)


184
185
186
187
188
189
190
191
192
# File 'lib/zoidberg/shell.rb', line 184

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