Skip to content

Conversation

jkoritzinsky
Copy link
Member

@jkoritzinsky jkoritzinsky commented Jul 17, 2025

Use the shared managed wait subsystem for CoreCLR's managed code instead of the Win32 PAL

Also, remove the named mutex support from the CoreCLR PAL as nothing uses it now.

TODO: Once #117688 is in, we can remove Mutex support from the Win32 PAL entirely

Unblocks #115685

Depends on #117635

Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

Comment on lines +14 to 18
// Declared nullable even though it is initialized in Create
// as Roslyn doesn't see that it's set in Create and Create is called from all constructors.
private WaitSubsystem.WaitableObject? _semaphore;

private void Create(int maximumSignalCount)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use [MemberNotNull(nameof(_semaphore))]?

// as we're running on the finalizer thread right now.
PREPARE_NONVIRTUAL_CALLSITE(METHOD__THREAD__ON_THREAD_EXITING);
DECLARE_ARGHOLDER_ARRAY(args, 1);
args[ARGNUM_0] = OBJECTREF_TO_ARGHOLDER(((Thread*)iter.GetElement())->GetExposedObject());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's keeping the Threads in the temporary list alive?

(The lifetime management of the native Threads is a mess, see e.g. #107473.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These managed thread objects can't be GCd until they are finalized, and (from what I can tell) they can't be put on the finalizer queue until after the native thread is marked as detached.

Since we're doing this processing on the finalizer thread after detaching the native threads but before processing any additional objects on the finalizer queue, they can't have been finalized yet. At worst they're currently on the finalizer queue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the native Thread objects, those are kept alive until after this function runs after the managed thread object has been finalized. As we can't run the managed thread object's finalizer while we're in this function as we're on the finalizer thread, we know that the native thread object hasn't been deleted yet.

@risc-vv
Copy link

risc-vv commented Aug 21, 2025

@dotnet/samsung Could you please take a look? These changes may be related to riscv64.

jkoritzinsky and others added 19 commits August 28, 2025 11:14
…as already hardened the cases that this makes weird, so we can reliably do it with CoreCLR
… wait" worker as the only handles it gets on Unix are PAL handles, not wait subsystem handles (and for Windows, we don't need to call back for handles that come through here)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Threading blocked Issue/PR is blocked on something - see comments runtime-coreclr specific to the CoreCLR runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants