|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Diagnostics.Tracing; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +namespace System.Threading; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Keep a pthread alive in its WebWorker after its pthread start function returns. |
| 12 | +/// </summary> |
| 13 | +internal static class WebWorkerEventLoop |
| 14 | +{ |
| 15 | + // FIXME: these keepalive calls could be qcalls with a SuppressGCTransitionAttribute |
| 16 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 17 | + private static extern void KeepalivePushInternal(); |
| 18 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 19 | + private static extern void KeepalivePopInternal(); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// A keepalive token prevents a thread from shutting down even if it returns to the JS event |
| 23 | + /// loop. A thread may want a keepalive token if it needs to allow JS code to run to settle JS |
| 24 | + /// promises or execute JS timeout callbacks. |
| 25 | + /// </summary> |
| 26 | + internal sealed class KeepaliveToken |
| 27 | + { |
| 28 | + public bool Valid {get; private set; } |
| 29 | + |
| 30 | + private KeepaliveToken() { Valid = true; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Decrement the Emscripten keepalive count. A thread with a zero keepalive count will |
| 34 | + /// terminate when it returns from its start function or from an async invocation from the |
| 35 | + /// JS event loop. |
| 36 | + /// </summary> |
| 37 | + internal void Pop() { |
| 38 | + if (!Valid) |
| 39 | + throw new InvalidOperationException(); |
| 40 | + Valid = false; |
| 41 | + KeepalivePopInternal(); |
| 42 | + } |
| 43 | + |
| 44 | + internal static KeepaliveToken Create() |
| 45 | + { |
| 46 | + KeepalivePushInternal(); |
| 47 | + return new KeepaliveToken(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Increment the Emscripten keepalive count. A thread with a positive keepalive can return from its |
| 53 | + /// thread start function or a JS event loop invocation and continue running in the JS event |
| 54 | + /// loop. |
| 55 | + /// </summary> |
| 56 | + internal static KeepaliveToken KeepalivePush() => KeepaliveToken.Create(); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Start a thread that may be kept alive on its webworker after the start function returns, |
| 60 | + /// if the emscripten keepalive count is positive. Once the thread returns to the JS event |
| 61 | + /// loop it will be able to settle JS promises as well as run any queued managed async |
| 62 | + /// callbacks. |
| 63 | + /// </summary> |
| 64 | + internal static void StartExitable(Thread thread, bool captureContext) |
| 65 | + { |
| 66 | + // don't support captureContext == true, for now, since it's |
| 67 | + // not needed by PortableThreadPool.WorkerThread |
| 68 | + if (captureContext) |
| 69 | + throw new InvalidOperationException(); |
| 70 | + // hack: threadpool threads are exitable, and nothing else is. |
| 71 | + // see create_thread() in mono/metadata/threads.c |
| 72 | + if (!thread.IsThreadPoolThread) |
| 73 | + throw new InvalidOperationException(); |
| 74 | + thread.UnsafeStart(); |
| 75 | + } |
| 76 | + |
| 77 | + /// returns true if the current thread has unsettled JS Interop promises |
| 78 | + private static bool HasUnsettledInteropPromises => HasUnsettledInteropPromisesNative(); |
| 79 | + |
| 80 | + // FIXME: this could be a qcall with a SuppressGCTransitionAttribute |
| 81 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 82 | + private static extern bool HasUnsettledInteropPromisesNative(); |
| 83 | + |
| 84 | + /// <summary>returns true if the current WebWorker has JavaScript objects that depend on the |
| 85 | + /// current managed thread.</summary> |
| 86 | + /// |
| 87 | + /// <remarks>If this returns false, the runtime is allowed to allow the current managed thread |
| 88 | + /// to exit and for the WebWorker to be recycled by Emscripten for another managed |
| 89 | + /// thread.</remarks> |
| 90 | + internal static bool HasJavaScriptInteropDependents |
| 91 | + { |
| 92 | + // |
| 93 | + // FIXME: |
| 94 | + // https://github.com/dotnet/runtime/issues/85052 - unsettled promises are not the only relevant |
| 95 | + // reasons for keeping a worker thread alive. We will need to add other conditions here. |
| 96 | + get => HasUnsettledInteropPromises; |
| 97 | + } |
| 98 | +} |
0 commit comments