Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
33b5c4a
[wasm] Bump emscripten to 3.1.34
radekdoulik Mar 27, 2023
dff44d4
Update emsdk deps
radekdoulik Mar 27, 2023
ce1a265
Update icu deps
radekdoulik Mar 27, 2023
ed96a04
Use new images
radekdoulik Mar 27, 2023
8e6a28c
Use emscripten_main_runtime_thread_id
radekdoulik Mar 27, 2023
81672f4
Ignore ExitStatus exceptions
radekdoulik Mar 28, 2023
c2cbb94
Handle UnhandledPromiseRejection for ExitStatus
radekdoulik Mar 28, 2023
f3edd6e
Merge branch 'main' into pr-wasm-emscripten-3-1-34
radekdoulik Mar 28, 2023
4c0cb9e
[wasm][threads] flip YieldFromDispatchLoop; specialize PortableThread…
lambdageek Mar 15, 2023
3d88608
[mono] Implement a LifoJSSemaphore
lambdageek Mar 20, 2023
55b4649
Make managed LowLevelJSSemaphore
lambdageek Mar 20, 2023
8c74dea
copy-paste PortableThreadPool.WorkerThread for threaded WASM
lambdageek Mar 20, 2023
b323f0e
fixup native code for lifo semaphore
lambdageek Mar 21, 2023
51b1fbf
fixup managed code for LowLevelJSSemaphore
lambdageek Mar 21, 2023
3727ff1
Implement PortableThreadPool loop using semaphore callbacks
lambdageek Mar 21, 2023
763edab
manage emscripten event loop from PortableThreadPool.WorkerThread
lambdageek Mar 22, 2023
b45d001
FIXME: thread equality assertion in timeout callback
lambdageek Mar 22, 2023
1f7620c
XXX REVERT ME - minimal async timeout test
lambdageek Mar 22, 2023
06a3cc1
BUGFIX: &wait_entry ===> wait_entry
lambdageek Mar 23, 2023
8574368
nit: log thread id as hex in .ts
lambdageek Mar 23, 2023
c86cb26
XXX minimal sample - fetch on a background thread works
lambdageek Mar 23, 2023
c3cad74
fix non-wasm non-threads builds
lambdageek Mar 24, 2023
e57b262
Add WebWorkerEventLoop internal class to managed event loop keepalive
lambdageek Mar 24, 2023
07eabcd
Start threadpool threads with keepalive checks
lambdageek Mar 24, 2023
474607e
HACK: kind of work around the emscripten_runtime_keepalive_push/pop n…
lambdageek Mar 24, 2023
9f45167
support JS Semaphore with --print-icall-table cross compiler
lambdageek Mar 27, 2023
2e1f31f
make minimal FetchBackground sample more like a unit test
lambdageek Mar 27, 2023
dacc0cb
Share PortableThreadPool.WorkerThread common code
lambdageek Mar 29, 2023
10ca330
make both kinds of lifo semaphore share a base struct
lambdageek Mar 30, 2023
f4a2c02
Unify LowLevelLifoSemaphore for normal and async waiting
lambdageek Mar 30, 2023
42388d8
WebWorkerEventLoop: remove dead code, update comments
lambdageek Mar 31, 2023
853938e
remove unused arg from async wait semaphore
lambdageek Mar 31, 2023
cb8b168
rename native semaphore to LifoSemaphoreAsyncWait
lambdageek Mar 31, 2023
3e8fee4
Rename managed file to LowLevelLifoSemaphore.AsyncWait.Browser.Thread…
lambdageek Mar 31, 2023
552f9a5
Remove unnecessary indirections and allocations from managed AsyncWai…
lambdageek Mar 31, 2023
812524f
fix non-browser+threads builds
lambdageek Mar 31, 2023
50c0f1a
Keep track of unsettled JS interop promises in threadpool workers
lambdageek Apr 3, 2023
c8afaba
change minimal sample's fetch helper to artificially delay
lambdageek Apr 3, 2023
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
Prev Previous commit
Next Next commit
manage emscripten event loop from PortableThreadPool.WorkerThread
make sure to keep the thread alive after setting up the semaphore
wait.
Cleanup the thread when exiting
  • Loading branch information
lambdageek committed Mar 28, 2023
commit 763edab3835f730863c0fa01466e6a6df59acfa8
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;

namespace System.Threading
{
Expand Down Expand Up @@ -65,9 +67,25 @@ private static void WorkerThreadStart()

LowLevelLock threadAdjustmentLock = threadPoolInstance._threadAdjustmentLock;
SemaphoreWaitState state = new(threadPoolInstance, threadAdjustmentLock) { SpinWait = true };
// set up the callbacks for semaphore waits, tell
// emscripten to keep the thread alive, and return to
// the JS event loop.
WaitForWorkLoop(s_semaphore, state);
EmscriptenKeepalivePush();
EmscriptenUnwindToJsEventLoop();
}

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void EmscriptenKeepalivePush();
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void EmscriptenKeepalivePop();
[DoesNotReturn]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void EmscriptenUnwindToJsEventLoop();
[DoesNotReturn]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void MonoThreadExit(); // FIXME: this is not Emscripten, it's just mono_thread_exit();

private static readonly Action<LowLevelJSSemaphore, object?> s_WorkLoopSemaphoreSuccess = new(WorkLoopSemaphoreSuccess);
private static readonly Action<LowLevelJSSemaphore, object?> s_WorkLoopSemaphoreTimedOut = new(WorkLoopSemaphoreTimedOut);

Expand All @@ -90,8 +108,8 @@ private static void WorkLoopSemaphoreTimedOut(LowLevelJSSemaphore semaphore, obj
if (WorkerTimedOutMaybeStop(state.ThreadPoolInstance, state.ThreadAdjustmentLock)) {
// we're done, kill the thread.

// emscriptenKeepalivePop();
// emscriptenThreadExit();
EmscriptenKeepalivePop();
MonoThreadExit();
return;
} else {
// more work showed up while we were shutting down, go around one more time
Expand Down
4 changes: 4 additions & 0 deletions src/mono/mono/metadata/icall-decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ ICALL_EXPORT gpointer ves_icall_System_Threading_LowLevelJSSemaphore_InitInterna
ICALL_EXPORT void ves_icall_System_Threading_LowLevelJSSemaphore_DeleteInternal (gpointer sem_ptr);
ICALL_EXPORT void ves_icall_System_Threading_LowLevelJSSemaphore_PrepareWaitInternal (gpointer sem_ptr, gint32 timeout_ms, gpointer success_cb, gpointer timeout_cb, gpointer gchandle, gpointer user_data);
ICALL_EXPORT void ves_icall_System_Threading_LowLevelJSSemaphore_ReleaseInternal (gpointer sem_ptr, gint32 count);

ICALL_EXPORT void ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenKeepalivePop (void);
ICALL_EXPORT void ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenKeepalivePush (void);
ICALL_EXPORT void ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenUnwindToJsEventLoop (void);
#endif

#ifdef TARGET_AMD64
Expand Down
12 changes: 10 additions & 2 deletions src/mono/mono/metadata/icall-def.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ NOHANDLES(ICALL(ILOCK_23, "Read(long&)", ves_icall_System_Threading_Interlocked_
ICALL_TYPE(JSSEM, "System.Threading.LowLevelJSSemaphore", JSSEM_1)
NOHANDLES(ICALL(JSSEM_1, "DeleteInternal", ves_icall_System_Threading_LowLevelJSSemaphore_DeleteInternal))
NOHANDLES(ICALL(JSSEM_2, "InitInternal", ves_icall_System_Threading_LowLevelJSSemaphore_InitInternal))
NOHANDLES(ICALL(JSSEM_3, "ReleaseInternal", ves_icall_System_Threading_LowLevelJSSemaphore_ReleaseInternal))
NOHANDLES(ICALL(JSSEM_4, "PrepareWaitInternal", ves_icall_System_Threading_LowLevelJSSemaphore_PrepareWaitInternal))
NOHANDLES(ICALL(JSSEM_3, "PrepareWaitInternal", ves_icall_System_Threading_LowLevelJSSemaphore_PrepareWaitInternal))
NOHANDLES(ICALL(JSSEM_4, "ReleaseInternal", ves_icall_System_Threading_LowLevelJSSemaphore_ReleaseInternal))
#endif

ICALL_TYPE(LIFOSEM, "System.Threading.LowLevelLifoSemaphore", LIFOSEM_1)
Expand All @@ -590,6 +590,14 @@ HANDLES(MONIT_7, "Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait
NOHANDLES(ICALL(MONIT_8, "get_LockContentionCount", ves_icall_System_Threading_Monitor_Monitor_LockContentionCount))
HANDLES(MONIT_9, "try_enter_with_atomic_var", ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var, void, 4, (MonoObject, guint32, MonoBoolean, MonoBoolean_ref))

#if defined(HOST_BROWSER) && !defined(DISABLE_THREADS)
ICALL_TYPE(TPOOL_WORKER, "System.Threading.PortableThreadPool/WorkerThread", TPOOL_WORKER_1)
NOHANDLES(ICALL(TPOOL_WORKER_1, "EmscriptenKeepalivePop", ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenKeepalivePop))
NOHANDLES(ICALL(TPOOL_WORKER_2, "EmscriptenKeepalivePush", ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenKeepalivePush))
NOHANDLES(ICALL(TPOOL_WORKER_3, "EmscriptenUnwindToJsEventLoop", ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenUnwindToJsEventLoop))
NOHANDLES(ICALL(TPOOL_WORKER_4, "MonoThreadExit", mono_thread_exit))
#endif

ICALL_TYPE(THREAD, "System.Threading.Thread", THREAD_1)
HANDLES(THREAD_1, "ClrState", ves_icall_System_Threading_Thread_ClrState, void, 2, (MonoInternalThread, guint32))
HANDLES(ITHREAD_2, "FreeInternal", ves_icall_System_Threading_InternalThread_Thread_free_internal, void, 1, (MonoInternalThread))
Expand Down
23 changes: 23 additions & 0 deletions src/mono/mono/metadata/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ mono_native_thread_join_handle (HANDLE thread_handle, gboolean close_handle);
#include <errno.h>
#endif

#if defined(HOST_BROWSER) && !defined(DISABLE_THREADS)
#include <emscripten/eventloop.h>
#endif

#include "icall-decl.h"

/*#define THREAD_DEBUG(a) do { a; } while (0)*/
Expand Down Expand Up @@ -4991,4 +4995,23 @@ ves_icall_System_Threading_LowLevelJSSemaphore_ReleaseInternal (gpointer sem_ptr
LifoJSSemaphore *sem = (LifoJSSemaphore *)sem_ptr;
mono_lifo_js_semaphore_release (sem, count);
}

void
ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenKeepalivePop (void)
{
emscripten_runtime_keepalive_pop();
}

void
ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenKeepalivePush (void)
{
emscripten_runtime_keepalive_push();
}

void
ves_icall_System_Threading_PortableThreadPool_WorkerThread_EmscriptenUnwindToJsEventLoop (void)
{
emscripten_unwind_to_js_event_loop();
}

#endif /* HOST_BROWSER && !DISABLE_THREADS */