Sha256: c70f00659a022bd2d01f3d9cfe721a90043c34ace7996e61d9679bc1aa19d979

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

#######################################################
#
# ruboto/util/stack.rb
#
# Utility methods for running code in a separate 
# thread with a larger stack.
#
#######################################################

class Object
  def with_large_stack(opts = {}, &block)
    opts = {:size => opts} if opts.is_a? Integer
    opts = {:name => opts} if opts.is_a? String
    opts = {:name => 'Block with large stack'}.update(opts)
    exception = nil
    result = nil
    t = Thread.with_large_stack(opts, &proc{result = block.call rescue exception = $!})
    t.join
    raise exception if exception
    result
  end
end

class Thread
  def self.with_large_stack(opts = {}, &block)
    opts = {:size => opts} if opts.is_a? Integer
    opts = {:name => opts} if opts.is_a? String
    stack_size_kb = opts.delete(:size) || 64
    name = opts.delete(:name) || 'Thread with large stack'
    raise "Unknown option(s): #{opts.inspect}" unless opts.empty?
    block_with_logging = proc do
      begin
        block.call
      rescue Exception
        STDERR.puts "Exception in #{name}: #{$!}\n#{$!.backtrace.join("\n")}"
        raise
      end
    end
    t = java.lang.Thread.new(nil, block_with_logging, name, stack_size_kb * 1024)
    t.start
    t
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruboto-1.5.0 assets/src/ruboto/util/stack.rb