Skip to content

Commit b764c86

Browse files
committed
Review usages of defined?
1 parent 45bcb66 commit b764c86

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed

lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,46 @@
22
require 'monitor'
33

44
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
2826

29-
def teardown
30-
Thread.abort_on_exception = @old_abort
31-
end
27+
def teardown
28+
Thread.abort_on_exception = @old_abort
29+
end
3230

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
3735

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
4643
end
47-
4844
end
45+
4946
end
5047
end

spec/concurrent/atomic/atomic_reference_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ module Concurrent
180180
end
181181

182182
RSpec.describe AtomicReference do
183+
if RUBY_ENGINE != 'ruby'
184+
it 'does not load the C extension' do
185+
expect(defined?(Concurrent::CAtomicReference)).to be_falsey
186+
end
187+
end
188+
183189
if Concurrent.on_jruby?
184190
it 'inherits from JavaAtomicReference' do
185191
expect(described_class.ancestors).to include(Concurrent::JavaAtomicReference)

spec/concurrent/exchanger_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ module Concurrent
239239
end
240240
end
241241

242-
if defined? JavaExchanger
242+
if Concurrent.on_jruby?
243243
RSpec.describe JavaExchanger do
244244
it_behaves_like :exchanger
245245
end

spec/concurrent/thread_safe/no_unsafe_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
if defined?(JRUBY_VERSION) && ENV['TEST_NO_UNSAFE']
1+
require 'concurrent/utility/engine'
2+
3+
if Concurrent.on_jruby? && ENV['TEST_NO_UNSAFE']
24
# to be used like this: rake test TEST_NO_UNSAFE=true
35
load 'test/package.jar'
46
java_import 'thread_safe.SecurityManager'

0 commit comments

Comments
 (0)