-
Notifications
You must be signed in to change notification settings - Fork 5k
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
base: main
Are you sure you want to change the base?
Conversation
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. |
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. |
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 |
Apparently named semaphore isn't supported in PAL implementation either: runtime/src/coreclr/pal/src/synchobj/semaphore.cpp Lines 179 to 184 in ab105b5
runtime/src/coreclr/pal/src/synchobj/semaphore.cpp Lines 468 to 478 in ab105b5
Am I missing anything? |
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. |
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. |
This reverts commit 5c31732.
src/coreclr/utilcode/utsem.cpp
Outdated
@@ -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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
CreateSemaphore
is only used in one place, replaced withpthread_rwlock_t
instead.Mutex
andEvent
are used in a bit more places, thus not included in the same PR.