|
2 | 2 | require 'monitor'
|
3 | 3 |
|
4 | 4 | module Concurrent
|
5 |
| - unless defined?(SynchronizedDelegator) |
6 |
| - |
7 |
| - # This class provides a trivial way to synchronize all calls to a given object |
8 |
| - # by wrapping it with a `Delegator` that performs `Monitor#enter/exit` calls |
9 |
| - # around the delegated `#send`. Example: |
10 |
| - # |
11 |
| - # array = [] # not thread-safe on many impls |
12 |
| - # array = SynchronizedDelegator.new([]) # thread-safe |
13 |
| - # |
14 |
| - # A simple `Monitor` provides a very coarse-grained way to synchronize a given |
15 |
| - # object, in that it will cause synchronization for methods that have no need |
16 |
| - # for it, but this is a trivial way to get thread-safety where none may exist |
17 |
| - # currently on some implementations. |
18 |
| - # |
19 |
| - # This class is currently being considered for inclusion into stdlib, via |
20 |
| - # https://bugs.ruby-lang.org/issues/8556 |
21 |
| - # |
22 |
| - # @!visibility private |
23 |
| - class SynchronizedDelegator < SimpleDelegator |
24 |
| - def setup |
25 |
| - @old_abort = Thread.abort_on_exception |
26 |
| - Thread.abort_on_exception = true |
27 |
| - end |
| 5 | + # This class provides a trivial way to synchronize all calls to a given object |
| 6 | + # by wrapping it with a `Delegator` that performs `Monitor#enter/exit` calls |
| 7 | + # around the delegated `#send`. Example: |
| 8 | + # |
| 9 | + # array = [] # not thread-safe on many impls |
| 10 | + # array = SynchronizedDelegator.new([]) # thread-safe |
| 11 | + # |
| 12 | + # A simple `Monitor` provides a very coarse-grained way to synchronize a given |
| 13 | + # object, in that it will cause synchronization for methods that have no need |
| 14 | + # for it, but this is a trivial way to get thread-safety where none may exist |
| 15 | + # currently on some implementations. |
| 16 | + # |
| 17 | + # This class is currently being considered for inclusion into stdlib, via |
| 18 | + # https://bugs.ruby-lang.org/issues/8556 |
| 19 | + # |
| 20 | + # @!visibility private |
| 21 | + class SynchronizedDelegator < SimpleDelegator |
| 22 | + def setup |
| 23 | + @old_abort = Thread.abort_on_exception |
| 24 | + Thread.abort_on_exception = true |
| 25 | + end |
28 | 26 |
|
29 |
| - def teardown |
30 |
| - Thread.abort_on_exception = @old_abort |
31 |
| - end |
| 27 | + def teardown |
| 28 | + Thread.abort_on_exception = @old_abort |
| 29 | + end |
32 | 30 |
|
33 |
| - def initialize(obj) |
34 |
| - __setobj__(obj) |
35 |
| - @monitor = Monitor.new |
36 |
| - end |
| 31 | + def initialize(obj) |
| 32 | + __setobj__(obj) |
| 33 | + @monitor = Monitor.new |
| 34 | + end |
37 | 35 |
|
38 |
| - def method_missing(method, *args, &block) |
39 |
| - monitor = @monitor |
40 |
| - begin |
41 |
| - monitor.enter |
42 |
| - super |
43 |
| - ensure |
44 |
| - monitor.exit |
45 |
| - end |
| 36 | + def method_missing(method, *args, &block) |
| 37 | + monitor = @monitor |
| 38 | + begin |
| 39 | + monitor.enter |
| 40 | + super |
| 41 | + ensure |
| 42 | + monitor.exit |
46 | 43 | end
|
47 |
| - |
48 | 44 | end
|
| 45 | + |
49 | 46 | end
|
50 | 47 | end
|
0 commit comments