Skip to content

Commit

Permalink
[Truffle] Add Process.{getpriority,setpriority}.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed May 27, 2015
1 parent 196f43e commit b9e6b83
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
4 changes: 0 additions & 4 deletions spec/truffle/tags/core/process/getpriority_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/truffle/tags/core/process/setpriority_tags.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public static abstract class ExceptionErrnoErrorPrimitiveNode extends RubiniusPr
protected final static int ENOENT = Errno.ENOENT.intValue();
protected final static int EBADF = Errno.EBADF.intValue();
protected final static int EEXIST = Errno.EEXIST.intValue();
protected final static int EFAULT = Errno.EFAULT.intValue();
protected final static int EACCES = Errno.EACCES.intValue();
protected final static int ENOTDIR = Errno.ENOTDIR.intValue();

public static boolean isExceptionSupported(int errno) {
return errno == ENOENT || errno == EBADF || errno == EEXIST || errno == EACCES || errno == ENOTDIR;
return errno == ENOENT || errno == EBADF || errno == EEXIST || errno == EACCES || errno == ENOTDIR || errno == EFAULT;
}

public ExceptionErrnoErrorPrimitiveNode(RubyContext context, SourceSection sourceSection) {
Expand Down Expand Up @@ -70,6 +71,21 @@ public RubyException eacces(RubyString message, int errno) {
return getContext().getCoreLibrary().permissionDeniedError(message.toString(), this);
}

@Specialization(guards = {"errno == EACCES", "isNil(message)"})
public RubyException eacces(Object message, int errno) {
return getContext().getCoreLibrary().permissionDeniedError("nil", this);
}

@Specialization(guards = "errno == EFAULT")
public RubyException efault(RubyString message, int errno) {
return getContext().getCoreLibrary().badAddressError(this);
}

@Specialization(guards = {"errno == EFAULT", "isNil(message)"})
public RubyException efault(Object message, int errno) {
return getContext().getCoreLibrary().badAddressError(this);
}

@Specialization(guards = "errno == ENOTDIR")
public RubyException enotdir(RubyString message, int errno) {
return getContext().getCoreLibrary().notDirectoryError(message.toString(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,34 @@ public int chdir(RubyString path) {

}

@CoreMethod(names = "getpriority", isModuleFunction = true, required = 2, lowerFixnumParameters = {0, 1})
public abstract static class GetPriorityNode extends CoreMethodArrayArgumentsNode {

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

@Specialization
public int getpriority(int kind, int id) {
return posix().getpriority(kind, id);
}

}

@CoreMethod(names = "setpriority", isModuleFunction = true, required = 3, lowerFixnumParameters = {0, 1, 2})
public abstract static class SetPriorityNode extends CoreMethodArrayArgumentsNode {

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

@Specialization
public int setpriority(int kind, int id, int priority) {
return posix().setpriority(kind, id, priority);
}

}

@CoreMethod(names = "flock", isModuleFunction = true, required = 2, lowerFixnumParameters = {0, 1})
public abstract static class FlockNode extends CoreMethodArrayArgumentsNode {

Expand Down Expand Up @@ -441,6 +469,21 @@ public int errno() {

}

@CoreMethod(names = "errno=", isModuleFunction = true, required = 1)
public abstract static class ErrnoAssignNode extends CoreMethodArrayArgumentsNode {

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

@Specialization
public int errno(int errno) {
posix().errno(errno);
return 0;
}

}

@CoreMethod(names = "fcntl", isModuleFunction = true, required = 3)
public abstract static class FcntlNode extends CoreMethodArrayArgumentsNode {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,12 @@ public RubyException ioError(String fileName, Node currentNode) {
return new RubyException(ioErrorClass, context.makeString(String.format("Error reading file - %s", fileName)), RubyCallStack.getBacktrace(currentNode));
}

public RubyException badAddressError(Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(getErrnoClass(Errno.EFAULT), context.makeString("Bad address"), RubyCallStack.getBacktrace(currentNode));
}


public RubyException badFileDescriptor(Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(getErrnoClass(Errno.EBADF), context.makeString("Bad file descriptor"), RubyCallStack.getBacktrace(currentNode));
Expand Down
23 changes: 23 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

module Process
module Constants
PRIO_PGRP = Rubinius::Config['rbx.platform.process.PRIO_PGRP']
PRIO_PROCESS = Rubinius::Config['rbx.platform.process.PRIO_PROCESS']
PRIO_USER = Rubinius::Config['rbx.platform.process.PRIO_USER']
WNOHANG = 1
end
include Constants
Expand Down Expand Up @@ -70,6 +73,26 @@ def self.egid
ret
end

def self.getpriority(kind, id)
kind = Rubinius::Type.coerce_to kind, Integer, :to_int
id = Rubinius::Type.coerce_to id, Integer, :to_int

FFI::Platform::POSIX.errno = 0
ret = FFI::Platform::POSIX.getpriority(kind, id)
Errno.handle
ret
end

def self.setpriority(kind, id, priority)
kind = Rubinius::Type.coerce_to kind, Integer, :to_int
id = Rubinius::Type.coerce_to id, Integer, :to_int
priority = Rubinius::Type.coerce_to priority, Integer, :to_int

ret = FFI::Platform::POSIX.setpriority(kind, id, priority)
Errno.handle if ret == -1
ret
end

def self.groups
g = []
FFI::MemoryPointer.new(:int, @maxgroups) { |p|
Expand Down

0 comments on commit b9e6b83

Please sign in to comment.