robustthread 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/robustthread.rb +49 -15
  2. metadata +2 -2
data/lib/robustthread.rb CHANGED
@@ -5,30 +5,64 @@
5
5
  # Copyright:: Copyright (c) 2009 Jared Kuolt
6
6
  # License:: MIT License
7
7
 
8
- module RobustThread
8
+ class RobustThread
9
+ # The Thread object, brah
10
+ attr_reader :thread
11
+ # If the Thread takes a poopie...
12
+ attr_reader :exception
13
+ @@exit_handler_initialized = false
14
+ @@exception_handler = nil
9
15
  # Usage:
10
16
  #
11
- # RobustThread.new(args) do |x, y|
17
+ # rt = RobustThread.new(args) do |x, y|
12
18
  # do_something(x, y)
13
19
  # end
14
20
  #
15
- def new(*args, &block)
16
- thread = Thread.new(*args) do |*targs|
17
- block.call(*targs)
21
+ # If necessary, you can access the actual thread from the +RobustThread+
22
+ # object via its +thread+ attribute.
23
+ #
24
+ # rt.thread
25
+ # => #<Thread:0x7fa1ea57ff88 run>
26
+ def initialize(*args, &block)
27
+ RobustThread.init_exit_handler
28
+ @thread = Thread.new(*args) do |*targs|
29
+ begin
30
+ block.call(*targs)
31
+ rescue => e
32
+ @exception = e
33
+ RobustThread.handle_exception(e)
34
+ end
18
35
  end
19
- thread[:real_ultimate_power] = true
20
- thread
36
+ @thread[:real_ultimate_power] = true
21
37
  end
22
38
 
23
- module_function :new
24
- end
39
+ # Set exception handler:
40
+ #
41
+ # RobustThread.exception_handler do |exception|
42
+ # handle_exception(exception)
43
+ # end
44
+ def RobustThread.exception_handler(&block)
45
+ unless block.arity == 1
46
+ raise ArgumentError, "Bad arity for exception handler. It may only accept a single argument"
47
+ end
48
+ @@exception_handler = block
49
+ end
25
50
 
26
- # We define the BEGUN constant only after we've setup the exit handler
27
- unless defined? RobustThread::BEGUN
28
- at_exit do
29
- Thread.list.each do |thread|
30
- thread.join if thread[:real_ultimate_power]
51
+ private
52
+ # Sets up the exit_handler unless @@exit_handler_initialized
53
+ def RobustThread.init_exit_handler
54
+ return if @@exit_handler_initialized
55
+ at_exit do
56
+ Thread.list.each do |thread|
57
+ thread.join if thread[:real_ultimate_power]
58
+ end
31
59
  end
60
+ @@exit_handler_initialized = true
61
+ end
62
+
63
+ # Calls exception handler if set (see RobustThread.exception_handler)
64
+ def RobustThread.handle_exception(exception)
65
+ return unless @@exception_handler
66
+ @@exception_handler.call(exception)
32
67
  end
33
- RobustThread::BEGUN = true
34
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: robustthread
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared Kuolt
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-02 00:00:00 -07:00
12
+ date: 2009-06-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15