Skip to content

Commit 9540432

Browse files
committed
[GR-30472] [GR-27863] [GR-18878] [GR-30937] [GR-30728] [GR-30833] [GR-30804] Managed pthreads & synchronization primitives.
PullRequest: graal/8208
2 parents 9667495 + 2836f32 commit 9540432

File tree

7 files changed

+55
-21
lines changed

7 files changed

+55
-21
lines changed

sulong/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Fixes:
44

55
* Fix LLVM toolchain not working correctly for C++ on MacOS 11.3.
66

7+
* Fix exceptions being swallowed and not printed out on Sulong context disposal.
8+
9+
* Thread return values are now correctly removed from the context when joining the
10+
respective thread.
11+
712
New features:
813

914
* Support implicitly typed XADD instructions in inline assembly.
@@ -12,6 +17,10 @@ New features:
1217
supported instructions are: xchg, cmpxchg and xadd along with their explicitly typed
1318
variants, and various unary instructions including incb/w/l/q and decb/w/l/q.
1419

20+
* System calls that return pointers are now supported.
21+
22+
* Support passing pointers as operands to inline assembly instructions.
23+
1524
# Version 21.1.0
1625

1726
New features:

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/LLVMLanguage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ public static LLVMContext getContext() {
201201
return getCurrentContext(LLVMLanguage.class);
202202
}
203203

204+
@Override
205+
protected void initializeThread(LLVMContext context, Thread thread) {
206+
getCapability(PlatformCapability.class).initializeThread(context, thread);
207+
}
208+
204209
/**
205210
* Do not use this on fast-path.
206211
*/

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/PlatformCapability.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ protected PlatformCapability(Class<S> cls) {
6868
valueToSysCall = initTable(cls);
6969
}
7070

71+
public void initializeThread(@SuppressWarnings("unused") LLVMContext context,
72+
@SuppressWarnings("unused") Thread thread) {
73+
// Nothing needs to be done in Sulong for native thread initialization.
74+
}
75+
7176
@SuppressWarnings("unchecked")
7277
private S[] initTable(Class<S> cls) {
7378
S[] constants = cls.getEnumConstants();

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/nodes/asm/syscall/LLVMAMD64SyscallArchPrctlNode.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,19 @@ protected long doOp(long code, LLVMPointer addr,
6565

6666
private long exec(long code, LLVMPointer addr, LLVMContext context, LLVMPointerStoreNode store) throws AssertionError {
6767
switch (profile.profile((int) code)) {
68-
case LLVMAMD64ArchPrctl.ARCH_SET_FS:
68+
case LLVMAMD64ArchPrctl.ARCH_SET_FS: {
6969
context.setThreadLocalStorage(addr);
70-
break;
70+
return 0;
71+
}
7172
case LLVMAMD64ArchPrctl.ARCH_GET_FS: {
7273
LLVMPointer tls = context.getThreadLocalStorage();
7374
store.executeWithTarget(addr, tls);
74-
break;
75+
return 0;
7576
}
76-
default:
77+
default: {
7778
CompilerDirectives.transferToInterpreter();
7879
throw new AssertionError(String.format("not implemented: arch_prcntl(0x%04x)", code));
80+
}
7981
}
80-
return 0;
8182
}
8283
}

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/nodes/intrinsics/multithreading/LLVMPThreadStart.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public void run() {
9494
}
9595
}
9696
}
97-
pThreadContext.clearThreadId();
9897
}
9998
}
10099
}
@@ -129,6 +128,11 @@ public static LLVMPThreadFunctionRootNode create(LLVMLanguage language, NodeFact
129128
return new LLVMPThreadFunctionRootNode(language, descriptor, functionSlot, argSlot, nodeFactory);
130129
}
131130

131+
@Override
132+
public boolean isInternal() {
133+
return false;
134+
}
135+
132136
@Override
133137
public Object execute(VirtualFrame frame) {
134138
if (ctxRef == null) {

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/nodes/intrinsics/multithreading/LLVMPThreadThreadIntrinsics.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public abstract static class LLVMPThreadJoin extends LLVMBuiltin {
9292

9393
@Specialization
9494
@TruffleBoundary
95-
protected Object doIntrinsic(long threadId,
95+
protected Object doIntrinsic(long threadID,
9696
@CachedContext(LLVMLanguage.class) LLVMContext context) {
97-
final Thread thread = context.getpThreadContext().getThread(threadId);
97+
final Thread thread = context.getpThreadContext().getThread(threadID);
9898
if (thread != null) {
9999
try {
100100
thread.join();
@@ -104,7 +104,11 @@ protected Object doIntrinsic(long threadId,
104104
}
105105
}
106106

107-
return context.getpThreadContext().getThreadReturnValue(threadId);
107+
LLVMPThreadContext pthreadContext = context.getpThreadContext();
108+
Object threadReturnValue = pthreadContext.getThreadReturnValue(threadID);
109+
pthreadContext.clearThreadReturnValue(threadID);
110+
pthreadContext.clearThreadID(threadID);
111+
return threadReturnValue;
108112
}
109113
}
110114

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/pthread/LLVMPThreadContext.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public final class LLVMPThreadContext {
5151
private final Object threadLock;
5252

5353
/**
54-
* TODO GR-30833: At pthread_join, return values need to be cleared from the map of thread
55-
* return values.
54+
* At pthread_join, return values shall be cleared from this map of return values.
5655
*/
5756
private final ConcurrentMap<Long, Object> threadReturnValueStorage;
5857

@@ -96,14 +95,16 @@ public LLVMPThreadContext(TruffleLanguage.Env env, LLVMLanguage language, DataLa
9695

9796
@TruffleBoundary
9897
public void joinAllThreads() {
99-
final Collection<WeakReference<Thread>> threadsToJoin;
98+
final Collection<WeakReference<Thread>> threads;
99+
100100
synchronized (threadLock) {
101101
this.isCreateThreadAllowed = false;
102-
threadsToJoin = threadStorage.values();
102+
threads = threadStorage.values();
103103
}
104-
for (WeakReference<Thread> createdThread : threadsToJoin) {
104+
105+
for (WeakReference<Thread> thread : threads) {
105106
try {
106-
Thread t = createdThread.get();
107+
Thread t = thread.get();
107108
if (t != null) {
108109
t.join();
109110
}
@@ -209,18 +210,23 @@ public Thread getThread(long threadID) {
209210
}
210211

211212
@TruffleBoundary
212-
public void clearThreadId() {
213-
threadStorage.remove(Thread.currentThread().getId());
213+
public void clearThreadID(long threadID) {
214+
threadStorage.remove(threadID);
215+
}
216+
217+
@TruffleBoundary
218+
public void setThreadReturnValue(long threadID, Object value) {
219+
threadReturnValueStorage.put(threadID, value);
214220
}
215221

216222
@TruffleBoundary
217-
public void setThreadReturnValue(long threadId, Object value) {
218-
threadReturnValueStorage.put(threadId, value);
223+
public Object getThreadReturnValue(long threadID) {
224+
return threadReturnValueStorage.get(threadID);
219225
}
220226

221227
@TruffleBoundary
222-
public Object getThreadReturnValue(long threadId) {
223-
return threadReturnValueStorage.get(threadId);
228+
public void clearThreadReturnValue(long threadID) {
229+
threadReturnValueStorage.remove(threadID);
224230
}
225231

226232
public CallTarget getPthreadCallTarget() {

0 commit comments

Comments
 (0)