Module: Zoidberg::SoftShell

Included in:
Pool, Registry, Signal, Supervisor, 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



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

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)


138
139
140
141
# File 'lib/zoidberg/shell.rb', line 138

def _zoidberg_thread(thread)
  _zoidberg_proxy._zoidberg_thread(thread)
  true
end

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

Perform an async action

Parameters:

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

    lock when running

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/zoidberg/shell.rb', line 87

def async(locked=false, &block)
  if(block_given?)
    unless(locked)
      thread = ::Thread.new do
        begin
          self.instance_exec(&block)
        rescue Zoidberg::DeadException => e
          if(e.origin_object_id == object_id)
            raise
          else
            _zoidberg_proxy._zoidberg_unexpected_error(e)
            raise
          end
        rescue ::StandardError, ::ScriptError => e
          _zoidberg_proxy._zoidberg_unexpected_error(e)
          raise
        end
      end
    else
      thread = ::Thread.new do
        _zoidberg_proxy._aquire_lock!
        begin
          got_lock = true
          self.instance_exec(&block)
        rescue Zoidberg::DeadException => e
          if(e.origin_object_id == object_id)
            got_lock = false
          else
            _zoidberg_proxy._zoidberg_unexpected_error(e)
          end
          raise
        rescue ::StandardError, ::ScriptError => e
          _zoidberg_proxy._zoidberg_unexpected_error(e)
          raise
        ensure
          _zoidberg_proxy._release_lock! if got_lock
        end
      end
    end
    _zoidberg_thread(thread)
    nil
  else
    ::Zoidberg::SoftShell::AsyncProxy.new(locked, _zoidberg_proxy)
  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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/zoidberg/shell.rb', line 68

def defer
  re_lock = _zoidberg_proxy._release_lock!
  begin
    result = yield if block_given?
    result
  rescue ::Zoidberg::DeadException => e
    re_lock = false if e.origin_object_id == object_id
    raise
  rescue ::StandardError, ::ScriptError => e
    raise e
  ensure
    _zoidberg_proxy._aquire_lock! if re_lock
  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)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/zoidberg/shell.rb', line 148

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