blob: e784556abfc0e35cdff0ba7f869c70bbdef3399f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
require 'active_support/callbacks'
module ActiveSupport
class ExecutionWrapper
include ActiveSupport::Callbacks
define_callbacks :run
define_callbacks :complete
def self.to_run(*args, &block)
set_callback(:run, *args, &block)
end
def self.to_complete(*args, &block)
set_callback(:complete, *args, &block)
end
# Run this execution.
#
# Returns an instance, whose +complete!+ method *must* be invoked
# after the work has been performed.
#
# Where possible, prefer +wrap+.
def self.run!
new.tap(&:run!)
end
# Perform the work in the supplied block as an execution.
def self.wrap
return yield if active?
state = run!
begin
yield
ensure
state.complete!
end
end
class << self # :nodoc:
attr_accessor :active
end
def self.inherited(other) # :nodoc:
super
other.active = Concurrent::Hash.new(0)
end
self.active = Concurrent::Hash.new(0)
def self.active? # :nodoc:
@active[Thread.current] > 0
end
def run! # :nodoc:
self.class.active[Thread.current] += 1
run_callbacks(:run)
end
# Complete this in-flight execution. This method *must* be called
# exactly once on the result of any call to +run!+.
#
# Where possible, prefer +wrap+.
def complete!
run_callbacks(:complete)
self.class.active.delete Thread.current if (self.class.active[Thread.current] -= 1) == 0
end
end
end
|