Skip to content

Cleanup semaphore usage in utilcode #115685

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

huoyaoyuan
Copy link
Member

CreateSemaphore is only used in one place, replaced with pthread_rwlock_t instead.

Mutex and Event are used in a bit more places, thus not included in the same PR.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 17, 2025
@jkotas
Copy link
Member

jkotas commented May 17, 2025

I do not think that this is going to work. These Windows-simulating CoreCLR PAL APIs are used to implement managed System.Threading.Semaphore and friends on non-Windows. The main pre-requisite for cleaning up these Windows-simulating PAL APIs is to switch managed System.Threading.Semaphore and friends to managed WaitSubSystem. It requires factoring out support for named semaphores from the CoreCLR PAL. I know @jkoritzinsky has been thinking about what it would take.

@jkoritzinsky
Copy link
Member

Yeah this PR isn't going to work as-is because the Semaphore's underlying handle needs to be a SafeWaitHandle, which on non-Windows is a PAL wait handle. We can't have two separate implementations here. The way forward is to use the managed wait subsystem, but we need to determine how to handle features that aren't supported there (named semaphores, named mutexes, anything cross-process) before we can move to that.

The changes in utilcode to use pthreads can be done, but we can't remove the pal implementation yet.

@huoyaoyuan
Copy link
Member Author

huoyaoyuan commented May 18, 2025

These Windows-simulating CoreCLR PAL APIs are used to implement managed System.Threading.Semaphore and friends on non-Windows.

I do find such usage for Mutex, but not Semaphore.

I can see "can't find QCall" in the test failures now.

OK, I see the Interop.Semaphore.cs is included unconditionally in coreclr.

@huoyaoyuan
Copy link
Member Author

but we need to determine how to handle features that aren't supported there (named semaphores, named mutexes, anything cross-process) before we can move to that.

Apparently named semaphore isn't supported in PAL implementation either:

if (lpName != nullptr)
{
ASSERT("lpName: Cross-process named objects are not supported in PAL");
palError = ERROR_NOT_SUPPORTED;
goto InternalCreateSemaphoreExit;
}

/* validate parameters */
if (lpName == nullptr)
{
ERROR("lpName is NULL\n");
palError = ERROR_INVALID_PARAMETER;
}
else
{
ASSERT("lpName: Cross-process named objects are not supported in PAL");
palError = ERROR_NOT_SUPPORTED;
}

Am I missing anything?

@jkotas
Copy link
Member

jkotas commented May 18, 2025

It requires factoring out support for named semaphores from the CoreCLR PAL.

I meant to say named mutexes. Named mutexes are supported by CoreCLR PAL.

All PAL synchronization primitives (semaphores, mutexes, events) return opaque HANDLEs that can be used interchangeably and passed to PAL methods like WaitForMultipleObjects. It means it is not possible to switch these synchronization primitives to use the managed WaitSubSystem one at a time.

@huoyaoyuan
Copy link
Member Author

All PAL synchronization primitives (semaphores, mutexes, events) return opaque HANDLEs that can be used interchangeably and passed to PAL methods like WaitForMultipleObjects.

Yes, I realized this after seeing the QCalls.

Then I think it's better to do a minor refactor first and investigate other synchronization primitives.

@huoyaoyuan huoyaoyuan changed the title Cleanup Windows-simulating semaphore in PAL Cleanup semaphore usage in utilcode May 18, 2025
@@ -172,6 +183,10 @@ UTSemReadWrite::Init()

m_hWriteWaiterEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
IfNullRet(m_hWriteWaiterEvent);
#else // HOST_WINDOWS
pthread_rwlock_init(&m_rwLock, nullptr);
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to handle failures?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think so, but unsure about what to do on failure.

Copy link
Member

Choose a reason for hiding this comment

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

Same way as the current code - return HRESULT.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reading through the manpage, it's unclear to me that when a writer is holding the lock, whether a reader will be blocked or returned with EBUSY. Simply returning E_FAIL under any failure?

Volatile<ULONG> m_dwFlag; // internal state, see implementation
HANDLE m_hReadWaiterSemaphore; // semaphore for awakening read waiters
HANDLE m_hWriteWaiterEvent; // event for awakening write waiters
#else // HOST_WINDOWS
bool m_initialized;
pthread_rwlock_t m_rwLock;
Copy link
Member

@jkotas jkotas May 19, 2025

Choose a reason for hiding this comment

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

How does performance of pthread_rwlock_t compare to the current implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-PAL-coreclr community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants