Module: Zoidberg::SoftShell

Included in:
Signal, Timer
Defined in:
lib/zoidberg/shell.rb

Overview

Librated proxy based shell

Defined Under Namespace

Classes: AsyncProxy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/zoidberg/shell.rb', line 104

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

Instance Method Details

#_zoidberg_thread(thread) ⇒ TrueClass

Register a running thread for this instance. Registered threads are tracked and killed on cleanup

Parameters:

  • thread (Thread)

Returns:

  • (TrueClass)


72
73
74
75
# File 'lib/zoidberg/shell.rb', line 72

def _zoidberg_thread(thread)
  _zoidberg_proxy._raw_threads[self.object_id].push(thread)
  true
end

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

Perform an async action

Parameters:

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

    lock when running

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zoidberg/shell.rb', line 51

def async(locked=false, &block)
  if(block_given?)
    unless(locked)
      thread = ::Thread.new do
        self.instance_exec(&block)
      end
    else
      thread = ::Thread.new{ current_self.instance_exec(&block) }
    end
    _zoidberg_thread(thread)
    nil
  else
    ::Zoidberg::SoftShell::AsyncProxy.new(locked ? current_self : self)
  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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zoidberg/shell.rb', line 35

def defer
  _zoidberg_proxy._release_lock!
  begin
    result = yield if block_given?
    _zoidberg_proxy._aquire_lock!
    result
  rescue Exception => e
    _zoidberg_proxy._aquire_lock!
    raise e
  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)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zoidberg/shell.rb', line 82

def sleep(length=nil)
  if(_zoidberg_proxy._locker == ::Thread.current)
    defer do
      start_time = ::Time.now.to_f
      if(length)
        ::Kernel.sleep(length)
      else
        ::Kernel.sleep
      end
      ::Time.now.to_f - start_time
    end
  else
    start_time = ::Time.now.to_f
    if(length)
      ::Kernel.sleep(length)
    else
      ::Kernel.sleep
    end
    ::Time.now.to_f - start_time
  end
end