Skip to content

Commit

Permalink
Merge branch 'master' into truffle-head
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Jan 2, 2017
2 parents d4b77f3 + 70fdee6 commit 8845e1f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
17 changes: 7 additions & 10 deletions core/src/main/java/org/jruby/RubyArray.java
Expand Up @@ -2457,20 +2457,17 @@ protected RubyArray safeReverse() {
/** rb_ary_collect
*
*/
public IRubyObject collect(ThreadContext context, Block block) {
final Ruby runtime = context.runtime;
public RubyArray collect(ThreadContext context, Block block) {
if (!block.isGiven()) return makeShared();

final Ruby runtime = context.runtime;
IRubyObject[] arr = new IRubyObject[realLength];

return collectFrom(context, arr, 0, block);
}

protected IRubyObject collectFrom(ThreadContext context, IRubyObject[] arr, int i, Block block) {
for (i = 0; i < realLength; i++) {
int i = 0;
for (; i < realLength; i++) {
// Do not coarsen the "safe" check, since it will misinterpret AIOOBE from the yield
// See JRUBY-5434
arr[i] = block.yield(context, eltOk(i));
safeArraySet(runtime, arr, i, block.yield(context, eltOk(i))); // arr[i] = ...
}

// use iteration count as new size in case something was deleted along the way
Expand Down Expand Up @@ -2664,7 +2661,7 @@ public IRubyObject delete_at(int pos) {
modify();

IRubyObject nil = getRuntime().getNil();
IRubyObject obj = null; // should never return null below
IRubyObject obj;

try {
obj = values[begin + pos];
Expand All @@ -2681,7 +2678,7 @@ public IRubyObject delete_at(int pos) {
}

System.arraycopy(values, begin + pos + 1, values, begin + pos, len - (pos + 1));
values[begin + len - 1] = getRuntime().getNil();
values[begin + len - 1] = nil;
} catch (ArrayIndexOutOfBoundsException e) {
throw concurrentModification(getRuntime(), e);
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/util/unsafe/UnsafeHolder.java
Expand Up @@ -26,6 +26,8 @@
***** END LICENSE BLOCK *****/
package org.jruby.util.unsafe;

import com.headius.unsafe.fences.UnsafeFences;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

Expand Down Expand Up @@ -88,4 +90,7 @@ public static long fieldOffset(Class clazz, String name) {
return sun.misc.Unsafe.INVALID_FIELD_OFFSET;
}
}

@Deprecated
public static final boolean SUPPORTS_FENCES = UnsafeFences.SUPPORTS_FENCES;
}
17 changes: 17 additions & 0 deletions test/jruby/test_array.rb
Expand Up @@ -45,4 +45,21 @@ def test_range_to_a
str = '2202702806'; assert_equal false, (str..str).member?(str.to_i)
end

def test_collect_concurrency
arr = []

Thread.new do ; times = 0
loop { arr << Time.now.to_f; break if (times += 1) == 1000 }
end

1000.times do
begin
arr.collect { |f| f.to_i }.size
# expected not to raise a Java AIOoBE
rescue ConcurrencyError => e
puts "#{__method__} : #{e}" if $VERBOSE
end
end
end

end

0 comments on commit 8845e1f

Please sign in to comment.