concurrent-ruby 1.0.5 → 1.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +42 -0
  3. data/Gemfile +39 -0
  4. data/{LICENSE.txt → LICENSE.md} +2 -0
  5. data/README.md +203 -105
  6. data/Rakefile +278 -0
  7. data/ext/concurrent-ruby/ConcurrentRubyService.java +17 -0
  8. data/ext/concurrent-ruby/com/concurrent_ruby/ext/AtomicReferenceLibrary.java +175 -0
  9. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JRubyMapBackendLibrary.java +248 -0
  10. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicBooleanLibrary.java +93 -0
  11. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +113 -0
  12. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +159 -0
  13. data/ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java +304 -0
  14. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMap.java +31 -0
  15. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMapV8.java +3863 -0
  16. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/LongAdder.java +203 -0
  17. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/Striped64.java +342 -0
  18. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/ConcurrentHashMapV8.java +3800 -0
  19. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/LongAdder.java +204 -0
  20. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/Striped64.java +291 -0
  21. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166y/ThreadLocalRandom.java +199 -0
  22. data/lib/concurrent-ruby.rb +1 -0
  23. data/lib/concurrent.rb +24 -20
  24. data/lib/concurrent/agent.rb +7 -7
  25. data/lib/concurrent/array.rb +59 -32
  26. data/lib/concurrent/async.rb +4 -4
  27. data/lib/concurrent/atom.rb +9 -9
  28. data/lib/concurrent/atomic/atomic_boolean.rb +24 -20
  29. data/lib/concurrent/atomic/atomic_fixnum.rb +27 -23
  30. data/lib/concurrent/atomic/atomic_markable_reference.rb +164 -0
  31. data/lib/concurrent/atomic/atomic_reference.rb +176 -33
  32. data/lib/concurrent/atomic/count_down_latch.rb +6 -6
  33. data/lib/concurrent/atomic/cyclic_barrier.rb +1 -1
  34. data/lib/concurrent/atomic/event.rb +1 -1
  35. data/lib/concurrent/atomic/java_count_down_latch.rb +6 -5
  36. data/lib/concurrent/atomic/mutex_count_down_latch.rb +1 -0
  37. data/lib/concurrent/atomic/read_write_lock.rb +2 -1
  38. data/lib/concurrent/atomic/reentrant_read_write_lock.rb +3 -1
  39. data/lib/concurrent/atomic/semaphore.rb +8 -8
  40. data/lib/concurrent/atomic/thread_local_var.rb +7 -7
  41. data/lib/concurrent/atomic_reference/mutex_atomic.rb +3 -8
  42. data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +1 -1
  43. data/lib/concurrent/atomics.rb +0 -43
  44. data/lib/concurrent/collection/lock_free_stack.rb +127 -0
  45. data/lib/concurrent/collection/map/atomic_reference_map_backend.rb +3 -3
  46. data/lib/concurrent/collection/map/non_concurrent_map_backend.rb +1 -2
  47. data/lib/concurrent/collection/non_concurrent_priority_queue.rb +29 -29
  48. data/lib/concurrent/concern/dereferenceable.rb +1 -1
  49. data/lib/concurrent/concern/logging.rb +6 -1
  50. data/lib/concurrent/concern/observable.rb +7 -7
  51. data/lib/concurrent/concurrent_ruby.jar +0 -0
  52. data/lib/concurrent/configuration.rb +1 -6
  53. data/lib/concurrent/constants.rb +1 -1
  54. data/lib/concurrent/dataflow.rb +2 -1
  55. data/lib/concurrent/delay.rb +9 -7
  56. data/lib/concurrent/exchanger.rb +13 -21
  57. data/lib/concurrent/executor/abstract_executor_service.rb +2 -2
  58. data/lib/concurrent/executor/cached_thread_pool.rb +1 -1
  59. data/lib/concurrent/executor/executor_service.rb +15 -15
  60. data/lib/concurrent/executor/fixed_thread_pool.rb +18 -18
  61. data/lib/concurrent/executor/java_thread_pool_executor.rb +10 -7
  62. data/lib/concurrent/executor/single_thread_executor.rb +2 -2
  63. data/lib/concurrent/executor/thread_pool_executor.rb +6 -6
  64. data/lib/concurrent/executor/timer_set.rb +1 -1
  65. data/lib/concurrent/future.rb +4 -1
  66. data/lib/concurrent/hash.rb +53 -30
  67. data/lib/concurrent/ivar.rb +5 -6
  68. data/lib/concurrent/map.rb +20 -25
  69. data/lib/concurrent/maybe.rb +1 -1
  70. data/lib/concurrent/mutable_struct.rb +15 -14
  71. data/lib/concurrent/mvar.rb +2 -2
  72. data/lib/concurrent/promise.rb +53 -21
  73. data/lib/concurrent/promises.rb +1938 -0
  74. data/lib/concurrent/re_include.rb +58 -0
  75. data/lib/concurrent/set.rb +66 -0
  76. data/lib/concurrent/settable_struct.rb +1 -0
  77. data/lib/concurrent/synchronization.rb +4 -5
  78. data/lib/concurrent/synchronization/abstract_lockable_object.rb +5 -5
  79. data/lib/concurrent/synchronization/abstract_struct.rb +6 -4
  80. data/lib/concurrent/synchronization/lockable_object.rb +6 -6
  81. data/lib/concurrent/synchronization/{mri_lockable_object.rb → mutex_lockable_object.rb} +19 -14
  82. data/lib/concurrent/synchronization/object.rb +8 -4
  83. data/lib/concurrent/synchronization/truffleruby_object.rb +46 -0
  84. data/lib/concurrent/synchronization/volatile.rb +11 -9
  85. data/lib/concurrent/thread_safe/util/data_structures.rb +55 -0
  86. data/lib/concurrent/thread_safe/util/striped64.rb +9 -4
  87. data/lib/concurrent/timer_task.rb +5 -2
  88. data/lib/concurrent/tuple.rb +1 -1
  89. data/lib/concurrent/tvar.rb +2 -2
  90. data/lib/concurrent/utility/at_exit.rb +1 -1
  91. data/lib/concurrent/utility/engine.rb +2 -2
  92. data/lib/concurrent/utility/monotonic_time.rb +3 -3
  93. data/lib/concurrent/utility/native_extension_loader.rb +31 -33
  94. data/lib/concurrent/utility/processor_counter.rb +0 -2
  95. data/lib/concurrent/version.rb +2 -2
  96. metadata +35 -21
  97. data/lib/concurrent/atomic_reference/concurrent_update_error.rb +0 -8
  98. data/lib/concurrent/atomic_reference/direct_update.rb +0 -81
  99. data/lib/concurrent/atomic_reference/jruby+truffle.rb +0 -2
  100. data/lib/concurrent/atomic_reference/jruby.rb +0 -16
  101. data/lib/concurrent/atomic_reference/rbx.rb +0 -22
  102. data/lib/concurrent/atomic_reference/ruby.rb +0 -32
  103. data/lib/concurrent/edge.rb +0 -26
  104. data/lib/concurrent/lazy_register.rb +0 -81
  105. data/lib/concurrent/synchronization/truffle_lockable_object.rb +0 -9
  106. data/lib/concurrent/synchronization/truffle_object.rb +0 -31
  107. data/lib/concurrent/thread_safe/util/array_hash_rbx.rb +0 -30
@@ -0,0 +1,278 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require_relative 'lib/concurrent/version'
4
+ require_relative 'lib/concurrent/utility/engine'
5
+
6
+ core_gemspec = Gem::Specification.load File.join(__dir__, 'concurrent-ruby.gemspec')
7
+ ext_gemspec = Gem::Specification.load File.join(__dir__, 'concurrent-ruby-ext.gemspec')
8
+ edge_gemspec = Gem::Specification.load File.join(__dir__, 'concurrent-ruby-edge.gemspec')
9
+
10
+ require 'rake/javaextensiontask'
11
+
12
+ Rake::JavaExtensionTask.new('concurrent_ruby', core_gemspec) do |ext|
13
+ ext.ext_dir = 'ext/concurrent-ruby'
14
+ ext.lib_dir = 'lib/concurrent'
15
+ end
16
+
17
+ unless Concurrent.on_jruby?
18
+ require 'rake/extensiontask'
19
+
20
+ Rake::ExtensionTask.new('concurrent_ruby_ext', ext_gemspec) do |ext|
21
+ ext.ext_dir = 'ext/concurrent-ruby-ext'
22
+ ext.lib_dir = 'lib/concurrent'
23
+ ext.source_pattern = '*.{c,h}'
24
+
25
+ ext.cross_compile = true
26
+ ext.cross_platform = ['x86-mingw32', 'x64-mingw32']
27
+ end
28
+ end
29
+
30
+ require 'rake_compiler_dock'
31
+ namespace :repackage do
32
+ desc '* with Windows fat distributions'
33
+ task :all do
34
+ Dir.chdir(__dir__) do
35
+ sh 'bundle package'
36
+ Rake::Task['lib/concurrent/concurrent_ruby.jar'].invoke
37
+ RakeCompilerDock.exec 'support/cross_building.sh'
38
+ end
39
+ end
40
+ end
41
+
42
+ require 'rubygems'
43
+ require 'rubygems/package_task'
44
+
45
+ Gem::PackageTask.new(core_gemspec) {} if core_gemspec
46
+ Gem::PackageTask.new(ext_gemspec) {} if ext_gemspec && !Concurrent.on_jruby?
47
+ Gem::PackageTask.new(edge_gemspec) {} if edge_gemspec
48
+
49
+ CLEAN.include('lib/concurrent/2.*', 'lib/concurrent/*.jar')
50
+
51
+ begin
52
+ require 'rspec'
53
+ require 'rspec/core/rake_task'
54
+
55
+ RSpec::Core::RakeTask.new(:spec)
56
+
57
+ options = %w[ --color
58
+ --backtrace
59
+ --seed 1
60
+ --format documentation
61
+ --tag ~notravis ]
62
+
63
+ namespace :spec do
64
+ desc '* Configured for ci'
65
+ RSpec::Core::RakeTask.new(:ci) do |t|
66
+ t.rspec_opts = [*options].join(' ')
67
+ end
68
+
69
+ desc '* test packaged and installed gems instead of local files'
70
+ task :installed do
71
+ Dir.chdir(__dir__) do
72
+ sh 'gem install pkg/concurrent-ruby-1.1.0.pre1.gem'
73
+ sh 'gem install pkg/concurrent-ruby-ext-1.1.0.pre1.gem' if Concurrent.on_cruby?
74
+ sh 'gem install pkg/concurrent-ruby-edge-0.4.0.pre1.gem'
75
+ ENV['NO_PATH'] = 'true'
76
+ sh 'bundle update'
77
+ sh 'bundle exec rake spec:ci'
78
+ end
79
+ end
80
+ end
81
+
82
+ desc 'executed in CI'
83
+ task :ci => [:compile, 'spec:ci']
84
+
85
+ task :default => [:clobber, :compile, :spec]
86
+ rescue LoadError => e
87
+ puts 'RSpec is not installed, skipping test task definitions: ' + e.message
88
+ end
89
+
90
+ current_yard_version_name = Concurrent::VERSION.split('.')[0..2].join('.')
91
+
92
+ begin
93
+ require 'yard'
94
+ require 'md_ruby_eval'
95
+ require_relative 'support/yard_full_types'
96
+
97
+ common_yard_options = ['--no-yardopts',
98
+ '--no-document',
99
+ '--no-private',
100
+ '--embed-mixins',
101
+ '--markup', 'markdown',
102
+ '--title', 'Concurrent Ruby',
103
+ '--template', 'default',
104
+ '--template-path', 'yard-template',
105
+ '--default-return', 'undocumented']
106
+
107
+ desc 'Generate YARD Documentation (signpost, master)'
108
+ task :yard => ['yard:signpost', 'yard:master']
109
+
110
+ namespace :yard do
111
+
112
+ desc '* eval markdown files'
113
+ task :eval_md do
114
+ Dir.chdir File.join(__dir__, 'docs-source') do
115
+ sh 'bundle exec md-ruby-eval --auto'
116
+ end
117
+ end
118
+
119
+ task :update_readme do
120
+ Dir.chdir __dir__ do
121
+ content = File.read(File.join('README.md')).
122
+ gsub(/\[([\w ]+)\]\(http:\/\/ruby-concurrency\.github\.io\/concurrent-ruby\/master\/.*\)/) do |_|
123
+ case $1
124
+ when 'LockFreeLinkedSet'
125
+ "{Concurrent::Edge::#{$1} #{$1}}"
126
+ when '.dataflow'
127
+ '{Concurrent.dataflow Concurrent.dataflow}'
128
+ when 'thread pool'
129
+ '{file:thread_pools.md thread pool}'
130
+ else
131
+ "{Concurrent::#{$1} #{$1}}"
132
+ end
133
+ end
134
+ File.write 'tmp/README.md', content
135
+ end
136
+ end
137
+
138
+ define_yard_task = -> name do
139
+ desc "* of #{name} into subdir #{name}"
140
+ YARD::Rake::YardocTask.new(name) do |yard|
141
+ yard.options.push(
142
+ '--output-dir', "docs/#{name}",
143
+ '--main', 'tmp/README.md',
144
+ *common_yard_options)
145
+ yard.files = ['./lib/**/*.rb',
146
+ './lib-edge/**/*.rb',
147
+ './ext/concurrent_ruby_ext/**/*.c',
148
+ '-',
149
+ 'docs-source/thread_pools.md',
150
+ 'docs-source/promises.out.md',
151
+ 'LICENSE.md',
152
+ 'CHANGELOG.md']
153
+ end
154
+ Rake::Task[name].prerequisites.push 'yard:eval_md', 'yard:update_readme'
155
+ end
156
+
157
+ define_yard_task.call current_yard_version_name
158
+ define_yard_task.call 'master'
159
+
160
+ desc "* signpost for versions"
161
+ YARD::Rake::YardocTask.new(:signpost) do |yard|
162
+ yard.options.push(
163
+ '--output-dir', 'docs',
164
+ '--main', 'docs-source/signpost.md',
165
+ *common_yard_options)
166
+ yard.files = ['no-lib']
167
+ end
168
+
169
+ define_uptodate_task = -> name do
170
+ namespace name do
171
+ desc "** ensure that #{name} generated documentation is matching the source code"
172
+ task :uptodate do
173
+ Dir.chdir(__dir__) do
174
+ begin
175
+ FileUtils.cp_r 'docs', 'docs-copy', verbose: true
176
+ Rake::Task["yard:#{name}"].invoke
177
+ sh 'diff -r docs/ docs-copy/'
178
+ ensure
179
+ FileUtils.rm_rf 'docs-copy', verbose: true
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
185
+
186
+ define_uptodate_task.call current_yard_version_name
187
+ define_uptodate_task.call 'master'
188
+ end
189
+
190
+ rescue LoadError => e
191
+ puts 'YARD is not installed, skipping documentation task definitions: ' + e.message
192
+ end
193
+
194
+ desc 'build, test, and publish the gem'
195
+ task :release => ['release:checks', 'release:build', 'release:test']
196
+
197
+ namespace :release do
198
+ # Depends on environment of @pitr-ch
199
+
200
+ mri_version = '2.5.1'
201
+ jruby_version = 'jruby-9.1.17.0'
202
+
203
+ task :checks => "yard:#{current_yard_version_name}:uptodate" do
204
+ Dir.chdir(__dir__) do
205
+ begin
206
+ STDOUT.puts "Is this a final release build? (Do git checks?) (y/n)"
207
+ input = STDIN.gets.strip.downcase
208
+ end until %w(y n).include?(input)
209
+ if input == 'y'
210
+ sh 'test -z "$(git status --porcelain)"'
211
+ sh 'git fetch'
212
+ sh 'test $(git show-ref --verify --hash refs/heads/master) = $(git show-ref --verify --hash refs/remotes/github/master)'
213
+ end
214
+ end
215
+ end
216
+
217
+ desc '* build all *.gem files necessary for release'
218
+ task :build => 'repackage:all'
219
+
220
+ desc '* test actual installed gems instead of cloned repository on MRI and JRuby'
221
+ task :test do
222
+ Dir.chdir(__dir__) do
223
+ old = ENV['RBENV_VERSION']
224
+
225
+ ENV['RBENV_VERSION'] = mri_version
226
+ sh 'rbenv version'
227
+ sh 'bundle exec rake spec:installed'
228
+
229
+ ENV['RBENV_VERSION'] = jruby_version
230
+ sh 'rbenv version'
231
+ sh 'bundle exec rake spec:installed'
232
+
233
+ puts 'Windows build is untested'
234
+
235
+ ENV['RBENV_VERSION'] = old
236
+ end
237
+ end
238
+
239
+ desc '* do all nested steps'
240
+ task :publish => ['publish:ask', 'publish:tag', 'publish:rubygems', 'publish:post_steps']
241
+
242
+ namespace :publish do
243
+ task :ask do
244
+ begin
245
+ STDOUT.puts "Do you want to publish? (y/n)"
246
+ input = STDIN.gets.strip.downcase
247
+ end until %w(y n).include?(input)
248
+ raise 'reconsidered' if input == 'n'
249
+ end
250
+
251
+ desc '** tag HEAD with current version and push to github'
252
+ task :tag do
253
+ Dir.chdir(__dir__) do
254
+ sh "git tag v#{Concurrent::VERSION}"
255
+ sh "git tag edge-v#{Concurrent::EDGE_VERSION}"
256
+ sh "git push github v#{Concurrent::VERSION} edge-v#{Concurrent::EDGE_VERSION}"
257
+ end
258
+ end
259
+
260
+ desc '** push all *.gem files to rubygems'
261
+ task :rubygems do
262
+ Dir.chdir(__dir__) do
263
+ sh "gem push pkg/concurrent-ruby-#{Concurrent::VERSION}.gem"
264
+ sh "gem push pkg/concurrent-ruby-edge-#{Concurrent::EDGE_VERSION}.gem"
265
+ sh "gem push pkg/concurrent-ruby-ext-#{Concurrent::VERSION}.gem"
266
+ sh "gem push pkg/concurrent-ruby-ext-#{Concurrent::VERSION}-x64-mingw32.gem"
267
+ sh "gem push pkg/concurrent-ruby-ext-#{Concurrent::VERSION}-x86-mingw32.gem"
268
+ end
269
+ end
270
+
271
+ desc '** print post release steps'
272
+ task :post_steps do
273
+ puts 'Manually: create a release on GitHub with relevant changelog part'
274
+ puts 'Manually: send email same as release with relevant changelog part'
275
+ puts 'Manually: tweet'
276
+ end
277
+ end
278
+ end
@@ -0,0 +1,17 @@
1
+ import org.jruby.Ruby;
2
+ import org.jruby.runtime.load.BasicLibraryService;
3
+
4
+ import java.io.IOException;
5
+
6
+ public class ConcurrentRubyService implements BasicLibraryService {
7
+
8
+ public boolean basicLoad(final Ruby runtime) throws IOException {
9
+ new com.concurrent_ruby.ext.AtomicReferenceLibrary().load(runtime, false);
10
+ new com.concurrent_ruby.ext.JavaAtomicBooleanLibrary().load(runtime, false);
11
+ new com.concurrent_ruby.ext.JavaAtomicFixnumLibrary().load(runtime, false);
12
+ new com.concurrent_ruby.ext.JavaSemaphoreLibrary().load(runtime, false);
13
+ new com.concurrent_ruby.ext.SynchronizationLibrary().load(runtime, false);
14
+ new com.concurrent_ruby.ext.JRubyMapBackendLibrary().load(runtime, false);
15
+ return true;
16
+ }
17
+ }
@@ -0,0 +1,175 @@
1
+ package com.concurrent_ruby.ext;
2
+
3
+ import java.lang.reflect.Field;
4
+ import java.io.IOException;
5
+ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
6
+ import org.jruby.Ruby;
7
+ import org.jruby.RubyClass;
8
+ import org.jruby.RubyModule;
9
+ import org.jruby.RubyNumeric;
10
+ import org.jruby.RubyObject;
11
+ import org.jruby.anno.JRubyClass;
12
+ import org.jruby.anno.JRubyMethod;
13
+ import org.jruby.runtime.ObjectAllocator;
14
+ import org.jruby.runtime.ThreadContext;
15
+ import org.jruby.runtime.builtin.IRubyObject;
16
+ import org.jruby.runtime.load.Library;
17
+
18
+ /**
19
+ * This library adds an atomic reference type to JRuby for use in the atomic
20
+ * library. We do a native version to avoid the implicit value coercion that
21
+ * normally happens through JI.
22
+ *
23
+ * @author headius
24
+ */
25
+ public class AtomicReferenceLibrary implements Library {
26
+ public void load(Ruby runtime, boolean wrap) throws IOException {
27
+ RubyModule concurrentMod = runtime.defineModule("Concurrent");
28
+ RubyClass atomicCls = concurrentMod.defineClassUnder("JavaAtomicReference", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR);
29
+ try {
30
+ sun.misc.Unsafe.class.getMethod("getAndSetObject", Object.class);
31
+ atomicCls.setAllocator(JRUBYREFERENCE8_ALLOCATOR);
32
+ } catch (Exception e) {
33
+ // leave it as Java 6/7 version
34
+ }
35
+ atomicCls.defineAnnotatedMethods(JRubyReference.class);
36
+ }
37
+
38
+ private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() {
39
+ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
40
+ return new JRubyReference(runtime, klazz);
41
+ }
42
+ };
43
+
44
+ private static final ObjectAllocator JRUBYREFERENCE8_ALLOCATOR = new ObjectAllocator() {
45
+ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
46
+ return new JRubyReference8(runtime, klazz);
47
+ }
48
+ };
49
+
50
+ @JRubyClass(name="JRubyReference", parent="Object")
51
+ public static class JRubyReference extends RubyObject {
52
+ volatile IRubyObject reference;
53
+
54
+ static final sun.misc.Unsafe UNSAFE;
55
+ static final long referenceOffset;
56
+
57
+ static {
58
+ try {
59
+ UNSAFE = UnsafeHolder.U;
60
+ Class k = JRubyReference.class;
61
+ referenceOffset = UNSAFE.objectFieldOffset(k.getDeclaredField("reference"));
62
+ } catch (Exception e) {
63
+ throw new RuntimeException(e);
64
+ }
65
+ }
66
+
67
+ public JRubyReference(Ruby runtime, RubyClass klass) {
68
+ super(runtime, klass);
69
+ }
70
+
71
+ @JRubyMethod
72
+ public IRubyObject initialize(ThreadContext context) {
73
+ UNSAFE.putObject(this, referenceOffset, context.nil);
74
+ return context.nil;
75
+ }
76
+
77
+ @JRubyMethod
78
+ public IRubyObject initialize(ThreadContext context, IRubyObject value) {
79
+ UNSAFE.putObject(this, referenceOffset, value);
80
+ return context.nil;
81
+ }
82
+
83
+ @JRubyMethod(name = {"get", "value"})
84
+ public IRubyObject get() {
85
+ return reference;
86
+ }
87
+
88
+ @JRubyMethod(name = {"set", "value="})
89
+ public IRubyObject set(IRubyObject newValue) {
90
+ UNSAFE.putObjectVolatile(this, referenceOffset, newValue);
91
+ return newValue;
92
+ }
93
+
94
+ @JRubyMethod(name = {"compare_and_set", "compare_and_swap"})
95
+ public IRubyObject compare_and_set(ThreadContext context, IRubyObject expectedValue, IRubyObject newValue) {
96
+ Ruby runtime = context.runtime;
97
+
98
+ if (expectedValue instanceof RubyNumeric) {
99
+ // numerics are not always idempotent in Ruby, so we need to do slower logic
100
+ return compareAndSetNumeric(context, expectedValue, newValue);
101
+ }
102
+
103
+ return runtime.newBoolean(UNSAFE.compareAndSwapObject(this, referenceOffset, expectedValue, newValue));
104
+ }
105
+
106
+ @JRubyMethod(name = {"get_and_set", "swap"})
107
+ public IRubyObject get_and_set(ThreadContext context, IRubyObject newValue) {
108
+ // less-efficient version for Java 6 and 7
109
+ while (true) {
110
+ IRubyObject oldValue = get();
111
+ if (UNSAFE.compareAndSwapObject(this, referenceOffset, oldValue, newValue)) {
112
+ return oldValue;
113
+ }
114
+ }
115
+ }
116
+
117
+ private IRubyObject compareAndSetNumeric(ThreadContext context, IRubyObject expectedValue, IRubyObject newValue) {
118
+ Ruby runtime = context.runtime;
119
+
120
+ // loop until:
121
+ // * reference CAS would succeed for same-valued objects
122
+ // * current and expected have different values as determined by #equals
123
+ while (true) {
124
+ IRubyObject current = reference;
125
+
126
+ if (!(current instanceof RubyNumeric)) {
127
+ // old value is not numeric, CAS fails
128
+ return runtime.getFalse();
129
+ }
130
+
131
+ RubyNumeric currentNumber = (RubyNumeric)current;
132
+ if (!currentNumber.equals(expectedValue)) {
133
+ // current number does not equal expected, fail CAS
134
+ return runtime.getFalse();
135
+ }
136
+
137
+ // check that current has not changed, or else allow loop to repeat
138
+ boolean success = UNSAFE.compareAndSwapObject(this, referenceOffset, current, newValue);
139
+ if (success) {
140
+ // value is same and did not change in interim...success
141
+ return runtime.getTrue();
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ private static final class UnsafeHolder {
148
+ private UnsafeHolder(){}
149
+
150
+ public static final sun.misc.Unsafe U = loadUnsafe();
151
+
152
+ private static sun.misc.Unsafe loadUnsafe() {
153
+ try {
154
+ Class unsafeClass = Class.forName("sun.misc.Unsafe");
155
+ Field f = unsafeClass.getDeclaredField("theUnsafe");
156
+ f.setAccessible(true);
157
+ return (sun.misc.Unsafe) f.get(null);
158
+ } catch (Exception e) {
159
+ return null;
160
+ }
161
+ }
162
+ }
163
+
164
+ public static class JRubyReference8 extends JRubyReference {
165
+ public JRubyReference8(Ruby runtime, RubyClass klass) {
166
+ super(runtime, klass);
167
+ }
168
+
169
+ @Override
170
+ public IRubyObject get_and_set(ThreadContext context, IRubyObject newValue) {
171
+ // efficient version for Java 8
172
+ return (IRubyObject)UNSAFE.getAndSetObject(this, referenceOffset, newValue);
173
+ }
174
+ }
175
+ }