Skip to content

Commit bdcd61f

Browse files
committed
Revert previous changes for TruffleRuby
* use same approach as for JRuby * use non public TruffleRuby API for simplicity
1 parent 04db895 commit bdcd61f

File tree

5 files changed

+79
-36
lines changed

5 files changed

+79
-36
lines changed

lib/concurrent/array.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
require 'concurrent/thread_safe/util'
33

44
module Concurrent
5-
if Concurrent.on_cruby?
5+
case
6+
when Concurrent.on_cruby?
67

78
# Because MRI never runs code in parallel, the existing
89
# non-thread-safe structures should usually work fine.
@@ -21,26 +22,40 @@ module Concurrent
2122
# may be lost. Use `#concat` instead.
2223
#
2324
# @see http://ruby-doc.org/core-2.2.0/Array.html Ruby standard library `Array`
24-
class Array < ::Array;
25+
class Array < ::Array
2526
end
2627

27-
elsif Concurrent.on_jruby?
28+
when Concurrent.on_jruby?
2829
require 'jruby/synchronized'
2930

3031
# @!macro concurrent_array
3132
class Array < ::Array
3233
include JRuby::Synchronized
3334
end
3435

35-
elsif Concurrent.on_rbx? || Concurrent.on_truffleruby?
36+
when Concurrent.on_rbx?
3637
require 'monitor'
37-
require 'concurrent/thread_safe/util/array_hash_rbx'
38+
require 'concurrent/thread_safe/util/data_structures'
3839

3940
# @!macro concurrent_array
4041
class Array < ::Array
4142
end
4243

4344
ThreadSafe::Util.make_synchronized_on_rbx Concurrent::Array
45+
46+
when Concurrent.on_truffleruby?
47+
require 'concurrent/thread_safe/util/data_structures'
48+
49+
# @!macro concurrent_array
50+
class Array < ::Array
51+
end
52+
53+
ThreadSafe::Util.make_synchronized_on_truffleruby Concurrent::Array
54+
55+
else
56+
warn 'Possibly unsupported Ruby implementation'
57+
class Array < ::Array
58+
end
4459
end
4560
end
4661

lib/concurrent/hash.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
require 'concurrent/thread_safe/util'
33

44
module Concurrent
5-
if Concurrent.on_cruby?
5+
case
6+
when Concurrent.on_cruby?
67

78
# @!macro [attach] concurrent_hash
89
#
@@ -12,25 +13,41 @@ module Concurrent
1213
# which takes the lock repeatedly when reading an item.
1314
#
1415
# @see http://ruby-doc.org/core-2.2.0/Hash.html Ruby standard library `Hash`
15-
class Hash < ::Hash;
16+
class Hash < ::Hash
1617
end
1718

18-
elsif Concurrent.on_jruby?
19+
when Concurrent.on_jruby?
1920
require 'jruby/synchronized'
2021

2122
# @!macro concurrent_hash
2223
class Hash < ::Hash
2324
include JRuby::Synchronized
2425
end
2526

26-
elsif Concurrent.on_rbx? || Concurrent.on_truffleruby?
27+
when Concurrent.on_rbx?
2728
require 'monitor'
28-
require 'concurrent/thread_safe/util/array_hash_rbx'
29+
require 'concurrent/thread_safe/util/data_structures'
2930

3031
# @!macro concurrent_hash
3132
class Hash < ::Hash
3233
end
3334

3435
ThreadSafe::Util.make_synchronized_on_rbx Concurrent::Hash
36+
37+
when Concurrent.on_truffleruby?
38+
require 'concurrent/thread_safe/util/data_structures'
39+
40+
# @!macro concurrent_hash
41+
class Hash < ::Hash
42+
end
43+
44+
ThreadSafe::Util.make_synchronized_on_truffleruby Concurrent::Hash
45+
46+
else
47+
warn 'Possibly unsupported Ruby implementation'
48+
class Hash < ::Hash
49+
end
50+
3551
end
3652
end
53+

lib/concurrent/set.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
require 'set'
44

55
module Concurrent
6-
if Concurrent.on_cruby?
6+
case
7+
when Concurrent.on_cruby?
78

89
# Because MRI never runs code in parallel, the existing
910
# non-thread-safe structures should usually work fine.
@@ -25,23 +26,37 @@ module Concurrent
2526
class Set < ::Set;
2627
end
2728

28-
elsif Concurrent.on_jruby?
29+
when Concurrent.on_jruby?
2930
require 'jruby/synchronized'
3031

3132
# @!macro concurrent_Set
3233
class Set < ::Set
3334
include JRuby::Synchronized
3435
end
3536

36-
elsif Concurrent.on_rbx? || Concurrent.on_truffleruby?
37+
when Concurrent.on_rbx?
3738
require 'monitor'
38-
require 'concurrent/thread_safe/util/array_hash_rbx'
39+
require 'concurrent/thread_safe/util/data_structures'
3940

4041
# @!macro concurrent_Set
4142
class Set < ::Set
4243
end
4344

44-
ThreadSafe::Util.make_synchronized_on_rbx Set
45+
ThreadSafe::Util.make_synchronized_on_rbx Concurrent::Set
46+
47+
when Concurrent.on_truffleruby?
48+
require 'concurrent/thread_safe/util/data_structures'
49+
50+
# @!macro concurrent_array
51+
class Set < ::Set
52+
end
53+
54+
ThreadSafe::Util.make_synchronized_on_truffleruby Concurrent::Set
55+
56+
else
57+
warn 'Possibly unsupported Ruby implementation'
58+
class Set < ::Set
59+
end
4560
end
4661
end
4762

lib/concurrent/thread_safe/util/array_hash_rbx.rb renamed to lib/concurrent/thread_safe/util/data_structures.rb

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,13 @@ module Util
66
def self.make_synchronized_on_rbx(klass)
77
klass.class_eval do
88
private
9+
910
def _mon_initialize
1011
@_monitor = Monitor.new unless @_monitor # avoid double initialisation
1112
end
1213

13-
def initialize(*args)
14-
_mon_initialize
15-
super
16-
end
17-
18-
def self.allocate
19-
obj = super
20-
obj.send(:_mon_initialize)
21-
obj
22-
end
23-
24-
def self.[](*args)
25-
obj = super
14+
def self.new(*args)
15+
obj = super(*args)
2616
obj.send(:_mon_initialize)
2717
obj
2818
end
@@ -42,18 +32,24 @@ def #{method}(*args)
4232
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
4333
def #{method}(*args)
4434
monitor = @_monitor
45-
46-
unless monitor
47-
raise("BUG: Internal monitor was not properly initialized. Please report this to the "\
48-
"concurrent-ruby developers.")
49-
end
50-
35+
monitor or raise("BUG: Internal monitor was not properly initialized. Please report this to the concurrent-ruby developers.")
5136
monitor.synchronize { super }
5237
end
5338
RUBY
5439
end
5540
end
5641
end
42+
43+
def self.make_synchronized_on_truffleruby(klass)
44+
klass.superclass.instance_methods(false).each do |method|
45+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
46+
def #{method}(*args, &block)
47+
# TODO (pitr-ch 01-Jul-2018): don't use internal TruffleRuby APIs
48+
Truffle::System.synchronized(self) { super(*args, &block) }
49+
end
50+
RUBY
51+
end
52+
end
5753
end
5854
end
5955
end

spec/concurrent/array_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module Concurrent
6868
context 'concurrency' do
6969
it do
7070
(1..Concurrent::ThreadSafe::Test::THREADS).map do |i|
71-
in_thread do
71+
in_thread(ary) do |ary|
7272
1000.times do
7373
ary << i
7474
ary.each { |x| x * 2 }
@@ -82,7 +82,7 @@ module Concurrent
8282
end
8383

8484
describe '#slice' do
85-
# This is mostly relevant on Rubinius and Truffle
85+
# This is mostly relevant on Rubinius and TruffleRuby
8686
it 'correctly initializes the monitor' do
8787
ary.concat([0, 1, 2, 3, 4, 5, 6, 7, 8])
8888

0 commit comments

Comments
 (0)