Skip to content

Replace Win32 PAL implementation with minipal implementation #115858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

AaronRobinsonMSFT
Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT commented May 22, 2025

This PR removes the PAL Win32 emulation of CRITICAL_SECTION. The emulation logic is unnecessarily complicated and used both a pthread mutex and condition variable. For CoreCLR purposes a mutex is sufficient.

A new minipal API was introduced, minipal_critsect. This uses the Win32 CRITICAL_SECTION on the Windows platform and pthread_mutex_t on all other platforms. This implementation aligns with a private "critical section" defined for the GC and native AOT. All now use this new API.

The CRITICAL_SECTION associated APIs and tests in the CoreCLR PAL have been removed.

Copy link
Contributor

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

@huoyaoyuan
Copy link
Member

FWIW, I tested std::mutex and mtx_t on Windows. std::mutex is about the same speed of CRITICAL_SECTION, and mtx_t is 2x slower. We are unable to use std::mutex because it's not noexcept.

Remove any non-running tests requiring CRITICAL_SECTION
Remove unused PAL header referencing CRITICAL_SECTION (internal.h)
@AaronRobinsonMSFT
Copy link
Member Author

AaronRobinsonMSFT commented May 22, 2025

FWIW, I tested std::mutex and mtx_t on Windows. std::mutex is about the same speed of CRITICAL_SECTION, and mtx_t is 2x slower. We are unable to use std::mutex because it's not noexcept.

It isn't just because of noexcept. On Windows std::mutex will pull in the concrt, which is a huge dependency.

mtx_t

clang on macOS doesn't support C11 threads. It isn't an option in any case.

@am11
Copy link
Member

am11 commented May 22, 2025

If the implementation is shared between:

  • coreclr and nativeaot, we put it under src/coreclr/minipal.
  • coreclr/natvieaot and libs/corehost/mono, we put it under src/native/minipal.

This PR is mixing the existing pattern.

@jkotas
Copy link
Member

jkotas commented May 22, 2025

If the implementation is shared between:
coreclr and nativeaot, we put it under src/coreclr/minipal.
coreclr/natvieaot and libs/corehost/mono, we put it under src/native/minipal.

src/native/minipal have been used to abstract general basic services. Simple lock fits the bill. It is not necessary for everything to be switched to the minipal implementation right way. For example, the initial implementation of the timers was like that.

src/coreclr/minipal have been used for functionality that's very specific and not a general basic services, like the double mapper.

@am11
Copy link
Member

am11 commented May 22, 2025

It is not necessary for everything to be switched to the minipal implementation right way.

If there is a plan to share this impl. with other subsets, then it makes sense. Otherwise, choosing among src/coreclr/runtime, src/coreclr/minipal and src/native/minipal to share code between nativeaot and coreclr is getting vague and not how they were initially defined.

@@ -490,7 +487,7 @@ LONG WINAPI RhpVectoredExceptionHandler(PEXCEPTION_POINTERS pExPtrs)

// Do not use ASSERT_UNCONDITIONALLY here. It will crash because of it consumes too much stack.
PalPrintFatalError("\nProcess is terminating due to StackOverflowException.\n");
RaiseFailFastException(pExPtrs->ExceptionRecord, pExPtrs->ContextRecord, 0);
PalRaiseFailFastException(pExPtrs->ExceptionRecord, pExPtrs->ContextRecord, 0);
Copy link
Member

Choose a reason for hiding this comment

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

This is Windows-specific code calling Windows-specific API. There is no point going through a PAL layer in cases like these.

Copy link
Member Author

@AaronRobinsonMSFT AaronRobinsonMSFT May 22, 2025

Choose a reason for hiding this comment

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

I see. The problem with this model is there is an odd consistency in files. It isn't obvious/clear on when we can avoid the PAL. If this file was clearly in a "windows" directory it would be obvious. In this case though we have 100s of lines of code that are defined/included in complex ways that make it tricky to know. If we instead just implement a PAL consistently we can use the simple rule of "only use PAL APIs". I'll change it, but I don't think this is a helpful way of writing code in a file that isn't defined as being platform specific.

See the other "Pal" functions in the same block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants