-
Notifications
You must be signed in to change notification settings - Fork 24
/
thread_safe.rb
65 lines (56 loc) · 1.88 KB
/
thread_safe.rb
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
require 'thread_safe/version'
require 'thread_safe/synchronized_delegator'
module ThreadSafe
autoload :Cache, 'thread_safe/cache'
autoload :Util, 'thread_safe/util'
# Various classes within allows for +nil+ values to be stored, so a special +NULL+ token is required to indicate the "nil-ness".
NULL = Object.new
if defined?(JRUBY_VERSION)
require 'jruby/synchronized'
# A thread-safe subclass of Array. This version locks
# against the object itself for every method call,
# ensuring only one thread can be reading or writing
# at a time. This includes iteration methods like
# #each.
class Array < ::Array
include JRuby::Synchronized
end
# A thread-safe subclass of Hash. This version locks
# against the object itself for every method call,
# ensuring only one thread can be reading or writing
# at a time. This includes iteration methods like
# #each.
class Hash < ::Hash
include JRuby::Synchronized
end
elsif !defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby'
# Because MRI never runs code in parallel, the existing
# non-thread-safe structures should usually work fine.
Array = ::Array
Hash = ::Hash
elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
require 'monitor'
class Hash < ::Hash; end
class Array < ::Array; end
[Hash, Array].each do |klass|
klass.class_eval do
private
def _mon_initialize
@_monitor = Monitor.new unless @_monitor # avoid double initialisation
end
def self.allocate
obj = super
obj.send(:_mon_initialize)
obj
end
end
klass.superclass.instance_methods(false).each do |method|
klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{method}(*args)
@_monitor.synchronize { super }
end
RUBY_EVAL
end
end
end
end