Skip to content

Commit

Permalink
[GR-30728] [GR-30748] Various threading-related cleanups.
Browse files Browse the repository at this point in the history
PullRequest: graal/8748
  • Loading branch information
fmorcos committed Apr 17, 2021
2 parents 8a8a701 + e3e22e4 commit 69ca661
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public final class LLVMContext {
private final LLVMFunctionPointerRegistry functionPointerRegistry;

// we are not able to clean up ThreadLocals properly, so we are using maps instead
private final Map<Thread, Object> tls = new ConcurrentHashMap<>();
private final Map<Thread, LLVMPointer> tls = new ConcurrentHashMap<>();

// The symbol table for storing the symbols of each bitcode library.
// These two fields contain the same value, but have different CompilationFinal annotations:
Expand Down Expand Up @@ -752,19 +752,24 @@ public void initializeSymbolTable(int index, int globalLength) {
}

@TruffleBoundary
public Object getThreadLocalStorage() {
Object value = tls.get(Thread.currentThread());
public LLVMPointer getThreadLocalStorage() {
LLVMPointer value = tls.get(Thread.currentThread());
if (value != null) {
return value;
}
return LLVMNativePointer.createNull();
}

@TruffleBoundary
public void setThreadLocalStorage(Object value) {
public void setThreadLocalStorage(LLVMPointer value) {
tls.put(Thread.currentThread(), value);
}

@TruffleBoundary
public void setThreadLocalStorage(LLVMPointer value, Thread thread) {
tls.put(thread, value);
}

@TruffleBoundary
public LLVMFunctionDescriptor getFunctionDescriptor(LLVMNativePointer handle) {
return functionPointerRegistry.getDescriptor(handle);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -69,7 +69,7 @@ private long exec(long code, LLVMPointer addr, LLVMContext context, LLVMPointerS
context.setThreadLocalStorage(addr);
break;
case LLVMAMD64ArchPrctl.ARCH_GET_FS: {
Object tls = context.getThreadLocalStorage();
LLVMPointer tls = context.getThreadLocalStorage();
store.executeWithTarget(addr, tls);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ public final class LLVMPThreadStart {

static final class LLVMPThreadRunnable implements Runnable {

private final boolean isThread;
private final Object startRoutine;
private final Object arg;
private final LLVMContext context;

LLVMPThreadRunnable(Object startRoutine, Object arg, LLVMContext context, boolean isThread) {
LLVMPThreadRunnable(Object startRoutine, Object arg, LLVMContext context) {
this.startRoutine = startRoutine;
this.arg = arg;
this.context = context;
this.isThread = isThread;
}

@Override
Expand All @@ -86,19 +84,17 @@ public void run() {
throw t;
} finally {
// call destructors from key create
if (this.isThread) {
for (int key = 1; key <= pThreadContext.getNumberOfPthreadKeys(); key++) {
final LLVMPointer destructor = pThreadContext.getDestructor(key);
if (destructor != null && !destructor.isNull()) {
final LLVMPointer keyMapping = pThreadContext.getAndRemoveSpecificUnlessNull(key);
if (keyMapping != null) {
assert !keyMapping.isNull();
new LLVMPThreadRunnable(destructor, keyMapping, this.context, false).run();
}
for (int key = 1; key <= pThreadContext.getNumberOfPthreadKeys(); key++) {
final LLVMPointer destructor = pThreadContext.getDestructor(key);
if (destructor != null && !destructor.isNull()) {
final LLVMPointer keyMapping = pThreadContext.getAndRemoveSpecificUnlessNull(key);
if (keyMapping != null) {
assert !keyMapping.isNull();
pThreadContext.getPthreadCallTarget().call(destructor, keyMapping);
}
}
pThreadContext.clearThreadId();
}
pThreadContext.clearThreadId();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public abstract static class LLVMPThreadCreate extends LLVMBuiltin {
protected int doIntrinsic(LLVMPointer thread, LLVMPointer startRoutine, LLVMPointer arg,
@Cached LLVMI64StoreNode store,
@CachedContext(LLVMLanguage.class) LLVMContext context) {
LLVMPThreadStart.LLVMPThreadRunnable init = new LLVMPThreadStart.LLVMPThreadRunnable(startRoutine, arg, context, true);
LLVMPThreadStart.LLVMPThreadRunnable init = new LLVMPThreadStart.LLVMPThreadRunnable(startRoutine, arg, context);
final Thread t = context.getpThreadContext().createThread(init);
if (t == null) {
return LLVMAMD64Error.EAGAIN;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -97,7 +97,7 @@ protected LLVMPointer doDerefHandle(LLVMNativePointer addr,

@Specialization(limit = "3")
protected LLVMPointer doIndirectedForeign(LLVMManagedPointer addr,
@CachedLibrary("addr.getObject()") LLVMManagedReadLibrary nativeRead) {
return nativeRead.readPointer(addr.getObject(), addr.getOffset());
@CachedLibrary("addr.getObject()") LLVMManagedReadLibrary read) {
return read.readPointer(addr.getObject(), addr.getOffset());
}
}

0 comments on commit 69ca661

Please sign in to comment.