Skip to content

Commit d6e8b03

Browse files
committed
More thread_safe cleanups
1 parent d58679c commit d6e8b03

17 files changed

+134
-104
lines changed

ext/com/concurrent_ruby/ext/JRubyMapBackendLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class JRubyMapBackendLibrary implements Library {
2626
public void load(Ruby runtime, boolean wrap) throws IOException {
2727

2828
RubyModule concurrentMod = runtime.defineModule("Concurrent");
29-
RubyModule thread_safeMod = concurrentMod.defineModuleUnder("ThreadSafe");
29+
RubyModule thread_safeMod = concurrentMod.defineModuleUnder("Collection");
3030
RubyClass jrubyRefClass = thread_safeMod.defineClassUnder("JRubyMapBackend", runtime.getObject(), BACKEND_ALLOCATOR);
3131
jrubyRefClass.setAllocator(BACKEND_ALLOCATOR);
3232
jrubyRefClass.defineAnnotatedMethods(JRubyMapBackend.class);

lib/concurrent.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,29 @@
1010

1111
require 'concurrent/atomic/atomic_reference'
1212
require 'concurrent/atom'
13-
require 'concurrent/array'
13+
require 'concurrent/collection/array'
14+
require 'concurrent/collection/hash'
15+
require 'concurrent/collection/map'
16+
require 'concurrent/collection/tuple'
1417
require 'concurrent/async'
1518
require 'concurrent/dataflow'
1619
require 'concurrent/delay'
1720
require 'concurrent/future'
18-
require 'concurrent/hash'
1921
require 'concurrent/immutable_struct'
2022
require 'concurrent/ivar'
21-
require 'concurrent/map'
2223
require 'concurrent/maybe'
2324
require 'concurrent/mutable_struct'
2425
require 'concurrent/mvar'
2526
require 'concurrent/promise'
2627
require 'concurrent/scheduled_task'
2728
require 'concurrent/settable_struct'
2829
require 'concurrent/timer_task'
29-
require 'concurrent/tuple'
3030
require 'concurrent/tvar'
3131

32+
require 'concurrent/thread_safe/synchronized_delegator'
33+
require 'concurrent/thread_safe/util'
34+
35+
3236
# @!macro [new] internal_implementation_note
3337
#
3438
# @note **Private Implementation:** This abstraction is a private, internal
@@ -121,4 +125,9 @@
121125
# * Be small, lean, and loosely coupled
122126
module Concurrent
123127

128+
# Various classes within allows for +nil+ values to be stored,
129+
# so a special +NULL+ token is required to indicate the "nil-ness".
130+
# @!visibility private
131+
NULL = Object.new
132+
124133
end

lib/concurrent/array.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/concurrent/collection/array.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'concurrent/utility/engine'
2+
require 'concurrent/thread_safe/util'
3+
4+
module Concurrent
5+
if Concurrent.on_cruby?
6+
7+
# Because MRI never runs code in parallel, the existing
8+
# non-thread-safe structures should usually work fine.
9+
10+
# @!macro [attach] concurrent_array
11+
#
12+
# A thread-safe subclass of Array. This version locks against the object
13+
# itself for every method call, ensuring only one thread can be reading
14+
# or writing at a time. This includes iteration methods like `#each`.
15+
#
16+
# @see http://ruby-doc.org/core-2.2.0/Array.html Ruby standard library `Array`
17+
class Array < ::Array;
18+
end
19+
20+
elsif Concurrent.on_jruby?
21+
require 'jruby/synchronized'
22+
23+
# @!macro concurrent_array
24+
class Array < ::Array
25+
include JRuby::Synchronized
26+
end
27+
28+
elsif Concurrent.on_rbx?
29+
require 'monitor'
30+
31+
# @!macro concurrent_array
32+
class Array < ::Array
33+
end
34+
35+
ThreadSafe::Util.make_synchronized_on_rbx Array
36+
end
37+
end
38+

lib/concurrent/collection/hash.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'concurrent/utility/engine'
2+
require 'concurrent/thread_safe/util'
3+
4+
module Concurrent
5+
if Concurrent.on_cruby?
6+
7+
# @!macro [attach] concurrent_hash
8+
#
9+
# A thread-safe subclass of Hash. This version locks against the object
10+
# itself for every method call, ensuring only one thread can be reading
11+
# or writing at a time. This includes iteration methods like `#each`.
12+
#
13+
# @see http://ruby-doc.org/core-2.2.0/Hash.html Ruby standard library `Hash`
14+
class Hash < ::Hash;
15+
end
16+
17+
elsif Concurrent.on_jruby?
18+
require 'jruby/synchronized'
19+
20+
# @!macro concurrent_hash
21+
class Hash < ::Hash
22+
include JRuby::Synchronized
23+
end
24+
25+
elsif Concurrent.on_rbx?
26+
require 'monitor'
27+
28+
# @!macro concurrent_hash
29+
class Hash < ::Hash
30+
end
31+
32+
ThreadSafe::Util.make_synchronized_on_rbx Hash
33+
34+
end
35+
end

lib/concurrent/map.rb renamed to lib/concurrent/collection/map.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22

33
module Concurrent
44
# @!visibility private
5-
module ThreadSafe
5+
module Collection
66

77
# @!visibility private
8-
MapBackend = if defined?(RUBY_ENGINE)
8+
MapImplementation = if defined?(RUBY_ENGINE)
99
case RUBY_ENGINE
1010
when 'jruby'
11+
# noinspection RubyResolve
1112
JRubyMapBackend
1213
when 'ruby'
13-
require 'concurrent/thread_safe/mri_map_backend'
14+
require 'concurrent/collection/map/mri_map_backend'
1415
MriMapBackend
1516
when 'rbx'
16-
require 'concurrent/thread_safe/atomic_reference_map_backend'
17+
require 'concurrent/collection/map/atomic_reference_map_backend'
1718
AtomicReferenceMapBackend
1819
else
1920
warn 'Concurrent::Map: unsupported Ruby engine, using a fully synchronized Concurrent::Map implementation' if $VERBOSE
20-
require 'concurrent/thread_safe/synchronized_map_backend'
21+
require 'concurrent/collection/map/synchronized_map_backend'
2122
SynchronizedMapBackend
2223
end
2324
else
@@ -36,7 +37,7 @@ module ThreadSafe
3637
# >
3738
# > map = Concurrent::Map.new
3839

39-
class Map < ThreadSafe::MapBackend
40+
class Map < Collection::MapImplementation
4041
def initialize(options = nil, &block)
4142
if options.kind_of?(::Hash)
4243
validate_options_hash!(options)

lib/concurrent/thread_safe/atomic_reference_map_backend.rb renamed to lib/concurrent/collection/map/atomic_reference_map_backend.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Concurrent
44

55
# @!visibility private
6-
module ThreadSafe
6+
module Collection
77

88
# A Ruby port of the Doug Lea's jsr166e.ConcurrentHashMapV8 class version 1.59
99
# available in public domain.
@@ -186,7 +186,7 @@ module ThreadSafe
186186
#
187187
# @!visibility private
188188
class AtomicReferenceMapBackend
189-
189+
190190
# @!visibility private
191191
class Table < Concurrent::ThreadSafe::Util::PowerOfTwoTuple
192192
def cas_new_node(i, hash, key, value)

lib/concurrent/thread_safe/mri_map_backend.rb renamed to lib/concurrent/collection/map/mri_map_backend.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
require 'thread'
2-
require 'concurrent/thread_safe/non_concurrent_map_backend'
2+
require 'concurrent/collection/map/non_concurrent_map_backend'
33

44
module Concurrent
55

66
# @!visibility private
7-
module ThreadSafe
7+
module Collection
88

99
# @!visibility private
1010
class MriMapBackend < NonConcurrentMapBackend

lib/concurrent/thread_safe/non_concurrent_map_backend.rb renamed to lib/concurrent/collection/map/non_concurrent_map_backend.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Concurrent
22

33
# @!visibility private
4-
module ThreadSafe
4+
module Collection
55

66
# @!visibility private
77
class NonConcurrentMapBackend

lib/concurrent/thread_safe/synchronized_map_backend.rb renamed to lib/concurrent/collection/map/synchronized_map_backend.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
require 'concurrent/thread_safe/non_concurrent_map_backend'
1+
require 'concurrent/collection/map/non_concurrent_map_backend'
22

33
module Concurrent
44

55
# @!visibility private
6-
module ThreadSafe
6+
module Collection
77

88
# @!visibility private
99
class SynchronizedMapBackend < NonConcurrentMapBackend

0 commit comments

Comments
 (0)