Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ public IRubyObject getName() {

@JRubyMethod(meta = true)
public static RubyThread current(IRubyObject recv) {
return recv.getRuntime().getCurrentContext().getThread();
return recv.getRuntime().getCurrentContext().getFiberCurrentThread();
}

@JRubyMethod(meta = true)
Expand Down
13 changes: 11 additions & 2 deletions core/src/main/java/org/jruby/ext/thread/Mutex.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
@JRubyClass(name = "Mutex")
public class Mutex extends RubyObject implements DataType {
final ReentrantLock lock = new ReentrantLock();
RubyThread heldBy;

@JRubyMethod(name = "new", rest = true, meta = true)
public static Mutex newInstance(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
Expand Down Expand Up @@ -113,6 +114,8 @@ public IRubyObject lock(ThreadContext context) {
}
}

heldBy = context.getFiberCurrentThread();

return this;
}

Expand All @@ -121,10 +124,12 @@ public IRubyObject unlock(ThreadContext context) {
if (!isLocked()) {
throw context.runtime.newThreadError("Mutex is not locked");
}
if (!lock.isHeldByCurrentThread()) {
if (!isHeldByCurrentThread(context)) {
throw context.runtime.newThreadError("Mutex is not owned by calling thread");
}

heldBy = null;

boolean hasQueued = lock.hasQueuedThreads();
context.getThread().unlock(lock);
return hasQueued ? context.nil : this;
Expand Down Expand Up @@ -180,9 +185,13 @@ public IRubyObject owned_p(ThreadContext context) {
}

private void checkRelocking(ThreadContext context) {
if (lock.isHeldByCurrentThread()) {
if (isHeldByCurrentThread(context)) {
throw context.runtime.newThreadError("Mutex relocking by same thread");
}
}

private boolean isHeldByCurrentThread(ThreadContext context) {
return context.getFiberCurrentThread() == heldBy;
}

}