Skip to content

Commit

Permalink
Showing 8 changed files with 100 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ matrix:
- env: PHASE='-Pjruby-jars'
jdk: openjdk7
- env: PHASE='-Pmain'
jdk: oraclejdk7
jdk: oraclejdk8
- env: PHASE='-Pcomplete'
jdk: oraclejdk8
- env: PHASE='-Posgi'
6 changes: 4 additions & 2 deletions core/src/main/java/org/jruby/ast/SymbolNode.java
Original file line number Diff line number Diff line change
@@ -55,8 +55,8 @@
* Represents a symbol (:symbol_name).
*/
public class SymbolNode extends Node implements ILiteralNode, INameNode, SideEffectFree {
private String name;
private Encoding encoding;
private final String name;
private final Encoding encoding;

// Interned ident path (e.g. [':', ident]).
public SymbolNode(ISourcePosition position, String name, Encoding encoding, int cr) {
@@ -79,6 +79,8 @@ public SymbolNode(ISourcePosition position, ByteList value) {
int size = value.realSize();
this.encoding = value.getEncoding().strLength(value.unsafeBytes(), value.begin(), size) == size ?
USASCIIEncoding.INSTANCE : value.getEncoding();
} else {
this.encoding = USASCIIEncoding.INSTANCE;
}
}

20 changes: 14 additions & 6 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/QueueNodes.java
Original file line number Diff line number Diff line change
@@ -17,15 +17,17 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.runtime.Visibility;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.cast.BooleanCastWithDefaultNodeGen;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.subsystems.ThreadManager.BlockingAction;
import org.jruby.util.unsafe.UnsafeHolder;
import org.jruby.truffle.runtime.util.MethodHandleUtils;

import java.lang.invoke.MethodHandle;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
@@ -230,21 +232,27 @@ public Object marshal_dump(DynamicObject self) {
@CoreMethod(names = "num_waiting")
public abstract static class NumWaitingNode extends CoreMethodArrayArgumentsNode {

private static final long LOCK_FIELD_OFFSET = UnsafeHolder.fieldOffset(LinkedBlockingQueue.class, "takeLock");
private static final long NOT_EMPTY_CONDITION_FIELD_OFFSET = UnsafeHolder.fieldOffset(LinkedBlockingQueue.class, "notEmpty");
private static final MethodHandle TAKE_LOCK_FIELD_GETTER = MethodHandleUtils.getPrivateGetter(LinkedBlockingQueue.class, "takeLock");
private static final MethodHandle NOT_EMPTY_CONDITION_FIELD_GETTER = MethodHandleUtils.getPrivateGetter(LinkedBlockingQueue.class, "notEmpty");

public NumWaitingNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@SuppressWarnings("restriction")
@Specialization
public int num_waiting(DynamicObject self) {
final BlockingQueue<Object> queue = Layouts.QUEUE.getQueue(self);

final LinkedBlockingQueue<Object> linkedBlockingQueue = (LinkedBlockingQueue<Object>) queue;
final ReentrantLock lock = (ReentrantLock) UnsafeHolder.U.getObject(linkedBlockingQueue, LOCK_FIELD_OFFSET);
final Condition notEmptyCondition = (Condition) UnsafeHolder.U.getObject(linkedBlockingQueue, NOT_EMPTY_CONDITION_FIELD_OFFSET);

final ReentrantLock lock;
final Condition notEmptyCondition;
try {
lock = (ReentrantLock) TAKE_LOCK_FIELD_GETTER.invokeExact(linkedBlockingQueue);
notEmptyCondition = (Condition) NOT_EMPTY_CONDITION_FIELD_GETTER.invokeExact(linkedBlockingQueue);
} catch (Throwable e) {
throw new RuntimeException(e);
}

getContext().getThreadManager().runUntilResult(this, new BlockingAction<Boolean>() {
@Override
Original file line number Diff line number Diff line change
@@ -17,15 +17,16 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.runtime.Visibility;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.cast.BooleanCastWithDefaultNodeGen;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.subsystems.ThreadManager.BlockingAction;
import org.jruby.util.unsafe.UnsafeHolder;

import org.jruby.truffle.runtime.util.MethodHandleUtils;
import java.lang.invoke.MethodHandle;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.Condition;
@@ -264,23 +265,30 @@ public DynamicObject clear(DynamicObject self) {
@CoreMethod(names = "num_waiting")
public abstract static class NumWaitingNode extends CoreMethodArrayArgumentsNode {

private static final long LOCK_FIELD_OFFSET = UnsafeHolder.fieldOffset(ArrayBlockingQueue.class, "lock");
private static final long NOT_EMPTY_CONDITION_FIELD_OFFSET = UnsafeHolder.fieldOffset(ArrayBlockingQueue.class, "notEmpty");
private static final long NOT_FULL_CONDITION_FIELD_OFFSET = UnsafeHolder.fieldOffset(ArrayBlockingQueue.class, "notFull");
private static final MethodHandle LOCK_FIELD_GETTER = MethodHandleUtils.getPrivateGetter(ArrayBlockingQueue.class, "lock");
private static final MethodHandle NOT_EMPTY_CONDITION_FIELD_GETTER = MethodHandleUtils.getPrivateGetter(ArrayBlockingQueue.class, "notEmpty");
private static final MethodHandle NOT_FULL_CONDITION_FIELD_GETTER = MethodHandleUtils.getPrivateGetter(ArrayBlockingQueue.class, "notFull");

public NumWaitingNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@SuppressWarnings("restriction")
@Specialization
public int num_waiting(DynamicObject self) {
final BlockingQueue<Object> queue = Layouts.SIZED_QUEUE.getQueue(self);

final ArrayBlockingQueue<Object> arrayBlockingQueue = (ArrayBlockingQueue<Object>) queue;
final ReentrantLock lock = (ReentrantLock) UnsafeHolder.U.getObject(arrayBlockingQueue, LOCK_FIELD_OFFSET);
final Condition notEmptyCondition = (Condition) UnsafeHolder.U.getObject(arrayBlockingQueue, NOT_EMPTY_CONDITION_FIELD_OFFSET);
final Condition notFullCondition = (Condition) UnsafeHolder.U.getObject(arrayBlockingQueue, NOT_FULL_CONDITION_FIELD_OFFSET);

final ReentrantLock lock;
final Condition notEmptyCondition;
final Condition notFullCondition;
try {
lock = (ReentrantLock) LOCK_FIELD_GETTER.invokeExact(arrayBlockingQueue);
notEmptyCondition = (Condition) NOT_EMPTY_CONDITION_FIELD_GETTER.invokeExact(arrayBlockingQueue);
notFullCondition = (Condition) NOT_FULL_CONDITION_FIELD_GETTER.invokeExact(arrayBlockingQueue);
} catch (Throwable e) {
throw new RuntimeException(e);
}

getContext().getThreadManager().runUntilResult(this, new BlockingAction<Boolean>() {
@Override
23 changes: 8 additions & 15 deletions truffle/src/main/java/org/jruby/truffle/nodes/ext/ZlibNodes.java
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.truffle.nodes.core.CoreClass;
import org.jruby.truffle.nodes.core.CoreMethod;
import org.jruby.truffle.nodes.core.CoreMethodArrayArgumentsNode;
@@ -22,10 +23,11 @@
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.util.MethodHandleUtils;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;

import java.lang.reflect.Field;
import java.lang.invoke.MethodHandle;
import java.util.zip.CRC32;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
@@ -153,16 +155,7 @@ public DynamicObject inflate(DynamicObject message) {
@CoreMethod(names = "adler32", isModuleFunction = true, required = 0, optional = 2, lowerFixnumParameters = 1)
public abstract static class Adler32Node extends CoreMethodArrayArgumentsNode {

private static final Field ADLER_PRIVATE_FIELD;

static {
try {
ADLER_PRIVATE_FIELD = Adler32.class.getDeclaredField("adler");
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
ADLER_PRIVATE_FIELD.setAccessible(true);
}
private static final MethodHandle ADLER_FIELD_SETTER = MethodHandleUtils.getPrivateSetter(Adler32.class, "adler");

public Adler32Node(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
@@ -178,7 +171,7 @@ public long adler32(NotProvided string, NotProvided adler) {
public long adler32(DynamicObject string, NotProvided adler) {
final ByteList bytes = Layouts.STRING.getByteList(string);
final Adler32 adler32 = new Adler32();
adler32.update(bytes.unsafeBytes());
adler32.update(bytes.unsafeBytes(), bytes.begin(), bytes.realSize());
return adler32.getValue();
}

@@ -189,12 +182,12 @@ public long adler32(DynamicObject string, int adler) {
final Adler32 adler32 = new Adler32();

try {
ADLER_PRIVATE_FIELD.setInt(adler32, adler);
} catch (IllegalAccessException e) {
ADLER_FIELD_SETTER.invokeExact(adler32, adler);
} catch (Throwable e) {
throw new RuntimeException(e);
}

adler32.update(bytes.unsafeBytes());
adler32.update(bytes.unsafeBytes(), bytes.begin(), bytes.realSize());
return adler32.getValue();
}

Original file line number Diff line number Diff line change
@@ -11,20 +11,17 @@
package org.jruby.truffle.runtime.sockets;

import jnr.ffi.Pointer;
import jnr.ffi.provider.MemoryManager;

public class LinuxFDSet implements FDSet {

private final static int MAX_FDS = 1024;
private final static int FIELD_SIZE_IN_BYTES = 4;
private final static int FIELD_SIZE_IN_BITS = FIELD_SIZE_IN_BYTES * 8;

private static final MemoryManager memoryManager = jnr.ffi.Runtime.getSystemRuntime().getMemoryManager();

private final Pointer bitmap;

public LinuxFDSet() {
bitmap = memoryManager.allocateDirect(MAX_FDS / 8);
bitmap = jnr.ffi.Runtime.getSystemRuntime().getMemoryManager().allocateDirect(MAX_FDS / 8);
}

@Override
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.runtime.util;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;

public abstract class MethodHandleUtils {

public static MethodHandle getPrivateGetter(Class<?> klass, String fieldName) {
final Field field = getPrivateField(klass, fieldName);
try {
return MethodHandles.lookup().unreflectGetter(field);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public static MethodHandle getPrivateSetter(final Class<?> klass, final String fieldName) {
final Field field = getPrivateField(klass, fieldName);
try {
return MethodHandles.lookup().unreflectSetter(field);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

private static Field getPrivateField(final Class<?> klass, final String fieldName) {
return AccessController.doPrivileged(new PrivilegedAction<Field>() {
@Override
public Field run() {
final Field field;
try {
field = klass.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
});
}

}
Original file line number Diff line number Diff line change
@@ -563,31 +563,7 @@ public RubyNode translateRubiniusSingleBlockArg(SourceSection sourceSection, Cal
}

private RubyNode translateRubiniusCheckFrozen(SourceSection sourceSection) {
/*
* Translate
*
* Rubinius.check_frozen
*
* into
*
* raise RuntimeError.new("can't modify frozen ClassName") if frozen?
*
* TODO(CS, 30-Jan-15) usual questions about monkey patching of the methods we're using
*/

final RubyNode frozen = new RubyCallNode(context, sourceSection, "frozen?", new SelfNode(context, sourceSection), null, false);

final RubyNode constructException = new RubyCallNode(context, sourceSection, "new",
new LiteralNode(context, sourceSection, context.getCoreLibrary().getRuntimeErrorClass()),
null, false,
new StringLiteralNode(context, sourceSection, ByteList.create("FrozenError: can't modify frozen TODO"), StringSupport.CR_UNKNOWN));

final RubyNode raise = new RubyCallNode(context, sourceSection, "raise", new SelfNode(context, sourceSection), null, false, true, constructException);

return new IfNode(context, sourceSection,
frozen,
raise,
nilNode(sourceSection));
return new RaiseIfFrozenNode(new SelfNode(context, sourceSection));
}

/**

0 comments on commit 0d1202c

Please sign in to comment.