This repository was archived by the owner on Mar 15, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +10
-9
lines changed Expand file tree Collapse file tree 1 file changed +10
-9
lines changed Original file line number Diff line number Diff line change 1
1
require 'delegate'
2
+ require 'monitor'
2
3
3
4
# This class provides a trivial way to synchronize all calls to a given object
4
- # by wrapping it with a `Delegator` that performs `Mutex#lock/unlock ` calls
5
+ # by wrapping it with a `Delegator` that performs `Monitor#enter/exit ` calls
5
6
# around the delegated `#send`. Example:
6
7
#
7
8
# array = [] # not thread-safe on many impls
8
9
# array = SynchronizedDelegator.new([]) # thread-safe
9
10
#
10
- # A simple `Mutex ` provides a very coarse-grained way to synchronize a given
11
+ # A simple `Monitor ` provides a very coarse-grained way to synchronize a given
11
12
# object, in that it will cause synchronization for methods that have no
12
13
# need for it, but this is a trivial way to get thread-safety where none may
13
14
# exist currently on some implementations.
@@ -18,33 +19,33 @@ class SynchronizedDelegator < SimpleDelegator
18
19
19
20
def initialize ( obj )
20
21
__setobj__ ( obj )
21
- @mutex = Mutex . new
22
+ @monitor = Monitor . new
22
23
end
23
24
24
25
def method_missing ( method , *args , &block )
25
- mutex = @mutex
26
+ monitor = @monitor
26
27
begin
27
- mutex . lock
28
+ monitor . enter
28
29
super
29
30
ensure
30
- mutex . unlock
31
+ monitor . exit
31
32
end
32
33
end
33
34
34
35
# Work-around for 1.8 std-lib not passing block around to delegate.
35
36
# @private
36
37
def method_missing ( method , *args , &block )
37
- mutex = @mutex
38
+ monitor = @monitor
38
39
begin
39
- mutex . lock
40
+ monitor . enter
40
41
target = self . __getobj__
41
42
if target . respond_to? ( method )
42
43
target . __send__ ( method , *args , &block )
43
44
else
44
45
super ( method , *args , &block )
45
46
end
46
47
ensure
47
- mutex . unlock
48
+ monitor . exit
48
49
end
49
50
end if RUBY_VERSION [ 0 , 3 ] == '1.8'
50
51
You can’t perform that action at this time.
0 commit comments