From 9f3eca617115a264b848a62f37028e147e997e98 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 6 Mar 2013 11:58:38 +0100 Subject: [PATCH] license, tests, lib. --- Gemfile.lock | 50 ++++++++++++++++++++++++++++++++++++++++ README.md | 22 ++++++++++++++++++ lib/timeout_interrupt.rb | 49 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 Gemfile.lock create mode 100644 README.md create mode 100644 lib/timeout_interrupt.rb diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..80a8c93 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,50 @@ +GEM + remote: http://rubygems.org/ + specs: + activesupport (3.2.12) + i18n (~> 0.6) + multi_json (~> 1.0) + bourne (1.1.2) + mocha (= 0.10.5) + ffi (1.1.0) + ffi-libc (0.0.5) + ffi (>= 0.6.0, <= 1.1.0) + git (1.2.5) + i18n (0.6.4) + jeweler (1.8.4) + bundler (~> 1.0) + git (>= 1.2.5) + rake + rdoc + json (1.7.7) + metaclass (0.0.1) + mocha (0.10.5) + metaclass (~> 0.0.1) + multi_json (1.6.1) + rake (10.0.3) + rdoc (4.0.0) + json (~> 1.4) + shoulda (3.3.2) + shoulda-context (~> 1.0.1) + shoulda-matchers (~> 1.4.1) + shoulda-context (1.0.2) + shoulda-matchers (1.4.2) + activesupport (>= 3.0.0) + bourne (~> 1.1.2) + simplecov (0.7.1) + multi_json (~> 1.0) + simplecov-html (~> 0.7.1) + simplecov-html (0.7.1) + yard (0.8.5.2) + +PLATFORMS + ruby + +DEPENDENCIES + bundler + ffi-libc + jeweler + rdoc + shoulda + simplecov + yard diff --git a/README.md b/README.md new file mode 100644 index 0000000..f85b548 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +ruby-timeout-interrupt +====================== + +Description goes here. + +Contributing to ruby-timeout-interrupt +====================================== + +* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. +* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. +* Fork the project. +* Start a feature/bugfix branch. +* Commit and push until you are happy with your contribution. +* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. +* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. + +Copyright +========= + +Copyright (c) 2013 Denis Knauf. See LICENSE.txt for +further details. + diff --git a/lib/timeout_interrupt.rb b/lib/timeout_interrupt.rb new file mode 100644 index 0000000..f484130 --- /dev/null +++ b/lib/timeout_interrupt.rb @@ -0,0 +1,49 @@ +require 'ffi/libc' +require 'timeout' + +module FFI + module LibC + attach_function :alarm, [:uint], :uint unless FFI::LibC.respond_to? :alarm + end +end + +module TimeoutInterrupt + def self.timeouts + @timeouts ||= {} + end + + def self.alarm_trap sig + key, (at, bt) = TimeoutInterrupt.timeouts.min_by {|key,(at,bt)| at } + return if Time.now < at + raise Timeout::Error, 'execution expired', bt + end + + def self.setup_timeout + if TimeoutInterrupt.timeouts.empty? + Signal.trap( 'ALRM') {} + FFI::LibC.alarm 0 + else + key, (at, bt) = TimeoutInterrupt.timeouts.min_by {|key,(at,bt)| at } + secs = (at - Time.now).to_i+1 + TimeoutInterrupt.alarm_trap if 1 > secs + Signal.trap 'ALRM', &TimeoutInterrupt.method( :alarm_trap) + FFI::LibC.alarm secs + end + end + + def self.timeout seconds + seconds = seconds.to_i + raise Timeout::Error, "Timeout must be longer than '0' seconds." unless 0 < seconds + return lambda {|&e| self.timeout seconds, &e } unless block_given? + at = Time.now + seconds + key, bt = Random.rand( 2**64-1), Kernel.caller + begin + TimeoutInterrupt.timeouts[key] = [at, bt] + TimeoutInterrupt.setup_timeout + yield + ensure + TimeoutInterrupt.timeouts.delete key + TimeoutInterrupt.setup_timeout + end + end +end