-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from 5 commits
6b88092
5c31732
57b7fa3
c1fc77d
f894dc6
a9232ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7144,8 +7144,10 @@ CMiniMdRW::ValidateVirtualSortAfterAddRecord( | |
| void | ||
| CMiniMdRW::Debug_CheckIsLockedForWrite() | ||
| { | ||
| #ifdef HOST_WINDOWS | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not an appropriate limitation. There should be nothing Windows specific about this code path. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't find a way to test whether a |
||
| // If this assert fires, then we are trying to modify MetaData that is not locked for write | ||
| _ASSERTE((dbg_m_pLock == NULL) || dbg_m_pLock->Debug_IsLockedForWrite()); | ||
| #endif // HOST_WINDOWS | ||
| } | ||
|
|
||
| #endif //_DEBUG | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,9 +120,14 @@ UTSemReadWrite::UTSemReadWrite() | |
| } | ||
| #endif //SELF_NO_HOST | ||
|
|
||
| #ifdef HOST_WINDOWS | ||
| m_dwFlag = 0; | ||
| m_hReadWaiterSemaphore = NULL; | ||
| m_hWriteWaiterEvent = NULL; | ||
| #else // HOST_WINDOWS | ||
| m_initialized = false; | ||
| memset(&m_rwLock, 0, sizeof(pthread_rwlock_t)); | ||
| #endif // HOST_WINDOWS | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -139,14 +144,19 @@ UTSemReadWrite::~UTSemReadWrite() | |
| GC_NOTRIGGER; | ||
| } | ||
| CONTRACTL_END; | ||
|
|
||
|
|
||
| #ifdef HOST_WINDOWS | ||
| _ASSERTE_MSG((m_dwFlag == (ULONG)0), "Destroying a UTSemReadWrite while in use"); | ||
|
|
||
| if (m_hReadWaiterSemaphore != NULL) | ||
| CloseHandle(m_hReadWaiterSemaphore); | ||
|
|
||
| if (m_hWriteWaiterEvent != NULL) | ||
| CloseHandle(m_hWriteWaiterEvent); | ||
| #else // HOST_WINDOWS | ||
| if (m_initialized) | ||
| pthread_rwlock_destroy(&m_rwLock); | ||
| #endif // HOST_WINDOWS | ||
| } | ||
|
|
||
| //======================================================================================= | ||
|
|
@@ -163,7 +173,8 @@ UTSemReadWrite::Init() | |
| } | ||
| CONTRACTL_END; | ||
|
|
||
|
|
||
|
|
||
| #ifdef HOST_WINDOWS | ||
| _ASSERTE(m_hReadWaiterSemaphore == NULL); | ||
| _ASSERTE(m_hWriteWaiterEvent == NULL); | ||
|
|
||
|
|
@@ -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); | ||
|
||
| m_initialized = true; | ||
| #endif // HOST_WINDOWS | ||
|
|
||
| return S_OK; | ||
| } // UTSemReadWrite::Init | ||
|
|
@@ -195,6 +210,7 @@ HRESULT UTSemReadWrite::LockRead() | |
| // holding this lock. | ||
| IncCantStopCount(); | ||
|
|
||
| #ifdef HOST_WINDOWS | ||
| // First do some spinning - copied from file:..\VM\crst.cpp#CrstBase::SpinEnter | ||
| for (DWORD iter = 0; iter < g_SpinConstants.dwRepetitions; iter++) | ||
| { | ||
|
|
@@ -261,6 +277,10 @@ HRESULT UTSemReadWrite::LockRead() | |
| ReadLockAcquired: | ||
| _ASSERTE ((m_dwFlag & READERS_MASK) != 0 && "reader count is zero after acquiring read lock"); | ||
| _ASSERTE ((m_dwFlag & WRITERS_MASK) == 0 && "writer count is nonzero after acquiring write lock"); | ||
| #else // HOST_WINDOWS | ||
| pthread_rwlock_rdlock(&m_rwLock); | ||
| #endif // HOST_WINDOWS | ||
|
|
||
| EE_LOCK_TAKEN(this); | ||
|
|
||
| return S_OK; | ||
|
|
@@ -286,7 +306,8 @@ HRESULT UTSemReadWrite::LockWrite() | |
| // Inform CLR that the debugger shouldn't suspend this thread while | ||
| // holding this lock. | ||
| IncCantStopCount(); | ||
|
|
||
|
|
||
| #ifdef HOST_WINDOWS | ||
| // First do some spinning - copied from file:..\VM\crst.cpp#CrstBase::SpinEnter | ||
| for (DWORD iter = 0; iter < g_SpinConstants.dwRepetitions; iter++) | ||
| { | ||
|
|
@@ -350,6 +371,10 @@ HRESULT UTSemReadWrite::LockWrite() | |
| WriteLockAcquired: | ||
| _ASSERTE ((m_dwFlag & READERS_MASK) == 0 && "reader count is nonzero after acquiring write lock"); | ||
| _ASSERTE ((m_dwFlag & WRITERS_MASK) == WRITERS_INCR && "writer count is not 1 after acquiring write lock"); | ||
| #else // HOST_WINDOWS | ||
| pthread_rwlock_wrlock(&m_rwLock); | ||
| #endif // HOST_WINDOWS | ||
|
|
||
| EE_LOCK_TAKEN(this); | ||
|
|
||
| return S_OK; | ||
|
|
@@ -371,6 +396,7 @@ void UTSemReadWrite::UnlockRead() | |
| } | ||
| CONTRACTL_END; | ||
|
|
||
| #ifdef HOST_WINDOWS | ||
| ULONG dwFlag; | ||
|
|
||
|
|
||
|
|
@@ -416,6 +442,9 @@ void UTSemReadWrite::UnlockRead() | |
| } | ||
| } | ||
| } | ||
| #else // HOST_WINDOWS | ||
| pthread_rwlock_unlock(&m_rwLock); | ||
| #endif // HOST_WINDOWS | ||
|
|
||
| DecCantStopCount(); | ||
| EE_LOCK_RELEASED(this); | ||
|
|
@@ -435,7 +464,8 @@ void UTSemReadWrite::UnlockWrite() | |
| GC_NOTRIGGER; | ||
| } | ||
| CONTRACTL_END; | ||
|
|
||
|
|
||
| #ifdef HOST_WINDOWS | ||
| ULONG dwFlag; | ||
| ULONG count; | ||
|
|
||
|
|
@@ -480,12 +510,15 @@ void UTSemReadWrite::UnlockWrite() | |
| } | ||
| } | ||
| } | ||
| #else // HOST_WINDOWS | ||
| pthread_rwlock_unlock(&m_rwLock); | ||
| #endif // HOST_WINDOWS | ||
|
|
||
| DecCantStopCount(); | ||
| EE_LOCK_RELEASED(this); | ||
| } // UTSemReadWrite::UnlockWrite | ||
|
|
||
| #ifdef _DEBUG | ||
| #if defined(_DEBUG) && defined(HOST_WINDOWS) | ||
|
|
||
| //======================================================================================= | ||
| BOOL | ||
|
|
@@ -501,5 +534,5 @@ UTSemReadWrite::Debug_IsLockedForWrite() | |
| return ((m_dwFlag & WRITERS_MASK) != 0); | ||
| } | ||
|
|
||
| #endif //_DEBUG | ||
| #endif // defined(_DEBUG) && defined(HOST_WINDOWS) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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_tcompare to the current implementation?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.
Plugged
UTSemReadWriteinto corerun and some basic tests. The time cost for 1 million loops of single-thread lock-unlock under WSL:PAL implementation: Read lock: 12350 us Write lock: 12379 us
pthreads implementation: Read lock: 11678 us Write lock: 15976 us
For reference, under Windows: Read lock: 13450 us Write lock: 12793 us