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
Remove unnecessary indirections and allocations from managed AsyncWai…
…t semaphore
  • Loading branch information
lambdageek committed Mar 31, 2023
commit 552f9a5414c8a268969e81ee359de65842aefc95
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,43 @@ namespace System.Threading;
// </summary>
internal sealed partial class LowLevelLifoSemaphore : IDisposable
{
internal static LowLevelLifoSemaphore CreateAsyncJS (int initialSignalCount, int maximumSignalCount, int spinCount, Action onWait)
public static LowLevelLifoSemaphore CreateAsyncWaitSemaphore (int initialSignalCount, int maximumSignalCount, int spinCount, Action onWait)
{
return new LowLevelLifoSemaphore(initialSignalCount, maximumSignalCount, spinCount, onWait, asyncJS: true);
return new LowLevelLifoSemaphore(initialSignalCount, maximumSignalCount, spinCount, onWait, asyncWait: true);
}

private LowLevelLifoSemaphore(int initialSignalCount, int maximumSignalCount, int spinCount, Action onWait, bool asyncJS)
private LowLevelLifoSemaphore(int initialSignalCount, int maximumSignalCount, int spinCount, Action onWait, bool asyncWait)
{
Debug.Assert(initialSignalCount >= 0);
Debug.Assert(initialSignalCount <= maximumSignalCount);
Debug.Assert(maximumSignalCount > 0);
Debug.Assert(spinCount >= 0);
Debug.Assert(asyncJS);
Debug.Assert(asyncWait);

_separated = default;
_separated._counts.SignalCount = (uint)initialSignalCount;
_maximumSignalCount = maximumSignalCount;
_spinCount = spinCount;
_onWait = onWait;

CreateAsyncJS(maximumSignalCount);
CreateAsyncWait(maximumSignalCount);
}

#pragma warning disable IDE0060
private void CreateAsyncJS(int maximumSignalCount)
private void CreateAsyncWait(int maximumSignalCount)
{
_kind = LifoSemaphoreKind.AsyncJS;
_kind = LifoSemaphoreKind.AsyncWait;
lifo_semaphore = InitInternal((int)_kind);
}
#pragma warning restore IDE0060

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern unsafe void PrepareAsyncWaitInternal(IntPtr semaphore,
int timeoutMs,
/*delegate* unmanaged<IntPtr, IntPtr, void> successCallback*/ void* successCallback,
/*delegate* unmanaged<IntPtr, IntPtr, void> timeoutCallback*/ void* timeoutCallback,
IntPtr userData);

private sealed record WaitEntry (LowLevelLifoSemaphore Semaphore, Action<LowLevelLifoSemaphore, object?> OnSuccess, Action<LowLevelLifoSemaphore, object?> OnTimeout, object? State);
private sealed record WaitEntry (LowLevelLifoSemaphore Semaphore, int TimeoutMs, Action<LowLevelLifoSemaphore, object?> OnSuccess, Action<LowLevelLifoSemaphore, object?> OnTimeout, object? State);

internal void PrepareAsyncWait(int timeoutMs, Action<LowLevelLifoSemaphore, object?> onSuccess, Action<LowLevelLifoSemaphore, object?> onTimeout, object? state)
public void PrepareAsyncWait(int timeoutMs, Action<LowLevelLifoSemaphore, object?> onSuccess, Action<LowLevelLifoSemaphore, object?> onTimeout, object? state)
{
//FIXME(ak): the async wait never spins. Shoudl we spin a little?
Debug.Assert(timeoutMs >= -1);
ThrowIfInvalidSemaphoreKind(LifoSemaphoreKind.AsyncWait);

// Try to acquire the semaphore or
// [[a) register as a spinner if false and timeoutMs > 0]]
Expand Down Expand Up @@ -136,27 +130,22 @@ private void PrepareAsyncWaitForSignal(int timeoutMs, Action<LowLevelLifoSemapho

_onWait();

PrepareAsyncWaitCore(timeoutMs, s_InternalAsyncWaitSuccess, s_InternalAsyncWaitTimeout, new InternalWait(timeoutMs, onSuccess, onTimeout, state));
PrepareAsyncWaitCore(timeoutMs, new WaitEntry(this, timeoutMs, onSuccess, onTimeout, state));
// on success calls InternalAsyncWaitSuccess, on timeout calls InternalAsyncWaitTimeout
}

private static readonly Action<LowLevelLifoSemaphore, object?> s_InternalAsyncWaitSuccess = InternalAsyncWaitSuccess;

private static readonly Action<LowLevelLifoSemaphore, object?> s_InternalAsyncWaitTimeout = InternalAsyncWaitTimeout;

internal sealed record InternalWait(int TimeoutMs, Action<LowLevelLifoSemaphore, object?> OnSuccess, Action<LowLevelLifoSemaphore, object?> OnTimeout, object? State);

private static void InternalAsyncWaitTimeout(LowLevelLifoSemaphore self, object? internalWaitObj)
private static void InternalAsyncWaitTimeout(LowLevelLifoSemaphore self, WaitEntry internalWaitEntry)
{
InternalWait i = (InternalWait)internalWaitObj!;
WaitEntry we = internalWaitEntry!;
// Unregister the waiter. The wait subsystem used above guarantees that a thread that wakes due to a timeout does
// not observe a signal to the object being waited upon.
self._separated._counts.InterlockedDecrementWaiterCount();
i.OnTimeout(self, i.State);
we.OnTimeout(self, we.State);
}

private static void InternalAsyncWaitSuccess(LowLevelLifoSemaphore self, object? internalWaitObj)
private static void InternalAsyncWaitSuccess(LowLevelLifoSemaphore self, WaitEntry internalWaitEntry)
{
InternalWait i = (InternalWait)internalWaitObj!;
WaitEntry we = internalWaitEntry!;
// Unregister the waiter if this thread will not be waiting anymore, and try to acquire the semaphore
Counts counts = self._separated._counts;
while (true)
Expand All @@ -180,7 +169,7 @@ private static void InternalAsyncWaitSuccess(LowLevelLifoSemaphore self, object?
{
if (counts.SignalCount != 0)
{
i.OnSuccess(self, i.State);
we.OnSuccess(self, we.State);
return;
}
break;
Expand All @@ -191,37 +180,43 @@ private static void InternalAsyncWaitSuccess(LowLevelLifoSemaphore self, object?
// if we get here, we need to keep waiting because the SignalCount above was 0 after we did
// the CompareExchange - someone took the signal before us.
// FIXME(ak): why is the timeoutMs the same as before? wouldn't we starve? why does LowLevelLifoSemaphore.WaitForSignal not decrement timeoutMs?
self.PrepareAsyncWaitCore (i.TimeoutMs, s_InternalAsyncWaitSuccess, s_InternalAsyncWaitTimeout, i);
self.PrepareAsyncWaitCore (we.TimeoutMs, we);
// on success calls InternalAsyncWaitSuccess, on timeout calls InternalAsyncWaitTimeout
}

internal void PrepareAsyncWaitCore(int timeout_ms, Action<LowLevelLifoSemaphore, object?> onSuccess, Action<LowLevelLifoSemaphore, object?> onTimeout, object? state)
private void PrepareAsyncWaitCore(int timeout_ms, WaitEntry internalWaitEntry)
{
ThrowIfInvalidSemaphoreKind (LifoSemaphoreKind.AsyncJS);
WaitEntry entry = new (this, onSuccess, onTimeout, state);
GCHandle gchandle = GCHandle.Alloc (entry);
GCHandle gchandle = GCHandle.Alloc (internalWaitEntry);
unsafe {
delegate* unmanaged<IntPtr, IntPtr, void> successCallback = &SuccessCallback;
delegate* unmanaged<IntPtr, IntPtr, void> timeoutCallback = &TimeoutCallback;
PrepareAsyncWaitInternal (lifo_semaphore, timeout_ms, successCallback, timeoutCallback, GCHandle.ToIntPtr(gchandle));
}
}

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern unsafe void PrepareAsyncWaitInternal(IntPtr semaphore,
int timeoutMs,
/*delegate* unmanaged<IntPtr, IntPtr, void> successCallback*/ void* successCallback,
/*delegate* unmanaged<IntPtr, IntPtr, void> timeoutCallback*/ void* timeoutCallback,
IntPtr userData);

[UnmanagedCallersOnly]
private static void SuccessCallback(IntPtr lifoSemaphore, IntPtr userData)
{
GCHandle gchandle = GCHandle.FromIntPtr(userData);
WaitEntry entry = (WaitEntry)gchandle.Target!;
WaitEntry internalWaitEntry = (WaitEntry)gchandle.Target!;
gchandle.Free();
entry.OnSuccess(entry.Semaphore, entry.State);
InternalAsyncWaitSuccess(internalWaitEntry.Semaphore, internalWaitEntry);
}

[UnmanagedCallersOnly]
private static void TimeoutCallback(IntPtr lifoSemaphore, IntPtr userData)
{
GCHandle gchandle = GCHandle.FromIntPtr(userData);
WaitEntry entry = (WaitEntry)gchandle.Target!;
WaitEntry internalWaitEntry = (WaitEntry)gchandle.Target!;
gchandle.Free();
entry.OnTimeout(entry.Semaphore, entry.State);
InternalAsyncWaitTimeout(internalWaitEntry.Semaphore, internalWaitEntry);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed unsafe partial class LowLevelLifoSemaphore : IDisposable
// Keep in sync with lifo-semaphore.h
private enum LifoSemaphoreKind : int {
Normal = 1,
AsyncJS = 2,
AsyncWait = 2,
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static partial class WorkerThread
/// Semaphore for controlling how many threads are currently working.
/// </summary>
private static readonly LowLevelLifoSemaphore s_semaphore =
LowLevelLifoSemaphore.CreateAsyncJS(
LowLevelLifoSemaphore.CreateAsyncWaitSemaphore(
0,
MaxPossibleThreadCount,
AppContextConfigHelper.GetInt32Config(
Expand Down