From f25f2e60559631b6e7e6bf65be18e12f9fe2fd88 Mon Sep 17 00:00:00 2001 From: Norbert Federa Date: Fri, 2 Aug 2013 12:07:05 +0200 Subject: [PATCH] winpr: improve and fix locking for data structures - Improved/completed(almost) winpr's critical section implementation - Replaced WaitForSingleObject locking with critical sections Note: WaitForSingleObject should _never_ be used for granular low-contention locks as it _always_ enters the kernel. Just replacing WaitForSingleObject locking in Bufferpool with EnterCriticalSection boosts the multithreaded rfx decoder performance by almost 400% on win32. (pull #1388 - cherry picked from commit 81ef251fc8f345c544d459e6a47a8479ff550e8a) --- winpr/include/winpr/collections.h | 34 +++++------ winpr/include/winpr/stream.h | 3 +- winpr/libwinpr/synch/critical.c | 57 +++++++++++-------- winpr/libwinpr/utils/collections/ArrayList.c | 44 +++++++------- winpr/libwinpr/utils/collections/BufferPool.c | 16 +++--- .../utils/collections/CountdownEvent.c | 12 ++-- .../libwinpr/utils/collections/MessageQueue.c | 16 +++--- winpr/libwinpr/utils/collections/ObjectPool.c | 16 +++--- winpr/libwinpr/utils/collections/PubSub.c | 12 ++-- winpr/libwinpr/utils/collections/Queue.c | 32 +++++------ winpr/libwinpr/utils/collections/Reference.c | 12 ++-- winpr/libwinpr/utils/collections/Stack.c | 16 +++--- winpr/libwinpr/utils/collections/StreamPool.c | 26 ++++----- 13 files changed, 153 insertions(+), 143 deletions(-) diff --git a/winpr/include/winpr/collections.h b/winpr/include/winpr/collections.h index 4ef2e3b19..e6fc2bcf5 100644 --- a/winpr/include/winpr/collections.h +++ b/winpr/include/winpr/collections.h @@ -58,7 +58,7 @@ struct _wQueue int tail; int size; void** array; - HANDLE mutex; + CRITICAL_SECTION lock; HANDLE event; wObject object; @@ -67,8 +67,8 @@ typedef struct _wQueue wQueue; WINPR_API int Queue_Count(wQueue* queue); -WINPR_API BOOL Queue_Lock(wQueue* queue); -WINPR_API BOOL Queue_Unlock(wQueue* queue); +WINPR_API void Queue_Lock(wQueue* queue); +WINPR_API void Queue_Unlock(wQueue* queue); WINPR_API HANDLE Queue_Event(wQueue* queue); @@ -93,7 +93,7 @@ struct _wStack int size; int capacity; void** array; - HANDLE mutex; + CRITICAL_SECTION lock; BOOL synchronized; wObject object; }; @@ -125,7 +125,7 @@ struct _wArrayList int size; void** array; - HANDLE mutex; + CRITICAL_SECTION lock; wObject object; }; @@ -137,8 +137,8 @@ WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList); WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList); WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList); -WINPR_API BOOL ArrayList_Lock(wArrayList* arrayList); -WINPR_API BOOL ArrayList_Unlock(wArrayList* arrayList); +WINPR_API void ArrayList_Lock(wArrayList* arrayList); +WINPR_API void ArrayList_Unlock(wArrayList* arrayList); WINPR_API void* ArrayList_GetItem(wArrayList* arrayList, int index); WINPR_API void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj); @@ -165,7 +165,7 @@ WINPR_API void ArrayList_Free(wArrayList* arrayList); struct _wDictionary { BOOL synchronized; - HANDLE mutex; + CRITICAL_SECTION lock; }; typedef struct _wDictionary wDictionary; @@ -184,7 +184,7 @@ struct _wListDictionaryItem struct _wListDictionary { BOOL synchronized; - HANDLE mutex; + CRITICAL_SECTION lock; wListDictionaryItem* head; }; @@ -227,7 +227,7 @@ typedef int (*REFERENCE_FREE)(void* context, void* ptr); struct _wReferenceTable { UINT32 size; - HANDLE mutex; + CRITICAL_SECTION lock; void* context; BOOL synchronized; wReference* array; @@ -246,7 +246,7 @@ WINPR_API void ReferenceTable_Free(wReferenceTable* referenceTable); struct _wCountdownEvent { DWORD count; - HANDLE mutex; + CRITICAL_SECTION lock; HANDLE event; DWORD initialCount; }; @@ -271,7 +271,7 @@ struct _wBufferPool int size; int capacity; void** array; - HANDLE mutex; + CRITICAL_SECTION lock; int fixedSize; DWORD alignment; BOOL synchronized; @@ -292,7 +292,7 @@ struct _wObjectPool int size; int capacity; void** array; - HANDLE mutex; + CRITICAL_SECTION lock; wObject object; BOOL synchronized; }; @@ -330,7 +330,7 @@ struct _wMessageQueue int size; int capacity; wMessage* array; - HANDLE mutex; + CRITICAL_SECTION lock; HANDLE event; }; typedef struct _wMessageQueue wMessageQueue; @@ -427,7 +427,7 @@ typedef struct _wEventType wEventType; struct _wPubSub { - HANDLE mutex; + CRITICAL_SECTION lock; BOOL synchronized; int size; @@ -436,8 +436,8 @@ struct _wPubSub }; typedef struct _wPubSub wPubSub; -WINPR_API BOOL PubSub_Lock(wPubSub* pubSub); -WINPR_API BOOL PubSub_Unlock(wPubSub* pubSub); +WINPR_API void PubSub_Lock(wPubSub* pubSub); +WINPR_API void PubSub_Unlock(wPubSub* pubSub); WINPR_API wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count); WINPR_API void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, int count); diff --git a/winpr/include/winpr/stream.h b/winpr/include/winpr/stream.h index e9805714b..0ad45602e 100644 --- a/winpr/include/winpr/stream.h +++ b/winpr/include/winpr/stream.h @@ -24,6 +24,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -236,7 +237,7 @@ struct _wStreamPool int uCapacity; wStream** uArray; - HANDLE mutex; + CRITICAL_SECTION lock; BOOL synchronized; size_t defaultSize; }; diff --git a/winpr/libwinpr/synch/critical.c b/winpr/libwinpr/synch/critical.c index 560fc512a..3466a6672 100644 --- a/winpr/libwinpr/synch/critical.c +++ b/winpr/libwinpr/synch/critical.c @@ -31,6 +31,12 @@ #ifndef _WIN32 +/** + * TODO: EnterCriticalSection allows recursive calls from the same thread. + * Implement this using pthreads (see PTHREAD_MUTEX_RECURSIVE_NP) + * http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608%28v=vs.85%29.aspx + */ + VOID InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection) { if (lpCriticalSection) @@ -44,60 +50,65 @@ VOID InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection) lpCriticalSection->OwningThread = NULL; lpCriticalSection->LockSemaphore = NULL; - lpCriticalSection->LockSemaphore = (winpr_sem_t*) malloc(sizeof(winpr_sem_t)); -#if defined __APPLE__ - semaphore_create(mach_task_self(), lpCriticalSection->LockSemaphore, SYNC_POLICY_FIFO, 1); -#else - sem_init(lpCriticalSection->LockSemaphore, 0, 1); -#endif + lpCriticalSection->LockSemaphore = malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(lpCriticalSection->LockSemaphore, NULL); } } BOOL InitializeCriticalSectionEx(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount, DWORD Flags) { - return TRUE; + if (Flags != 0) { + fprintf(stderr, "warning: InitializeCriticalSectionEx Flags unimplemented\n"); + } + return InitializeCriticalSectionAndSpinCount(lpCriticalSection, dwSpinCount); } BOOL InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount) { + InitializeCriticalSection(lpCriticalSection); + SetCriticalSectionSpinCount(lpCriticalSection, dwSpinCount); return TRUE; } DWORD SetCriticalSectionSpinCount(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount) { - return 0; + DWORD dwPreviousSpinCount = lpCriticalSection->SpinCount; + lpCriticalSection->SpinCount = dwSpinCount; + return dwPreviousSpinCount; } VOID EnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection) { -#if defined __APPLE__ - semaphore_wait(*((winpr_sem_t*) lpCriticalSection->LockSemaphore)); -#else - sem_wait((winpr_sem_t*) lpCriticalSection->LockSemaphore); + /** + * Linux NPTL thread synchronization primitives are implemented using + * the futex system calls ... no need for performing a trylock loop. + */ +#if !defined(__linux__) + ULONG spin = lpCriticalSection->SpinCount; + while (spin--) + { + if (pthread_mutex_trylock((pthread_mutex_t*)lpCriticalSection->LockSemaphore) == 0) + return; + pthread_yield(); + } #endif + pthread_mutex_lock((pthread_mutex_t*)lpCriticalSection->LockSemaphore); } BOOL TryEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection) { - return TRUE; + return (pthread_mutex_trylock((pthread_mutex_t*)lpCriticalSection->LockSemaphore) == 0 ? TRUE : FALSE); } VOID LeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection) { -#if defined __APPLE__ - semaphore_signal(*((winpr_sem_t*) lpCriticalSection->LockSemaphore)); -#else - sem_post((winpr_sem_t*) lpCriticalSection->LockSemaphore); -#endif + pthread_mutex_unlock((pthread_mutex_t*)lpCriticalSection->LockSemaphore); } VOID DeleteCriticalSection(LPCRITICAL_SECTION lpCriticalSection) { -#if defined __APPLE__ - semaphore_destroy(mach_task_self(), *((winpr_sem_t*) lpCriticalSection->LockSemaphore)); -#else - sem_destroy((winpr_sem_t*) lpCriticalSection->LockSemaphore); -#endif + pthread_mutex_destroy((pthread_mutex_t*)lpCriticalSection->LockSemaphore); + free(lpCriticalSection->LockSemaphore); } #endif diff --git a/winpr/libwinpr/utils/collections/ArrayList.c b/winpr/libwinpr/utils/collections/ArrayList.c index 157b365b6..40fec723d 100644 --- a/winpr/libwinpr/utils/collections/ArrayList.c +++ b/winpr/libwinpr/utils/collections/ArrayList.c @@ -83,18 +83,18 @@ BOOL ArrayList_IsSynchronized(wArrayList* arrayList) * Lock access to the ArrayList */ -BOOL ArrayList_Lock(wArrayList* arrayList) +void ArrayList_Lock(wArrayList* arrayList) { - return (WaitForSingleObject(arrayList->mutex, INFINITE) == WAIT_OBJECT_0) ? TRUE : FALSE; + EnterCriticalSection(&arrayList->lock); } /** * Unlock access to the ArrayList */ -BOOL ArrayList_Unlock(wArrayList* arrayList) +void ArrayList_Unlock(wArrayList* arrayList) { - return ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); } /** @@ -164,7 +164,7 @@ void ArrayList_Clear(wArrayList* arrayList) int index; if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); + EnterCriticalSection(&arrayList->lock); for (index = 0; index < arrayList->size; index++) { @@ -177,7 +177,7 @@ void ArrayList_Clear(wArrayList* arrayList) arrayList->size = 0; if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); } /** @@ -187,10 +187,10 @@ void ArrayList_Clear(wArrayList* arrayList) BOOL ArrayList_Contains(wArrayList* arrayList, void* obj) { if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); + EnterCriticalSection(&arrayList->lock); if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); return FALSE; } @@ -204,7 +204,7 @@ int ArrayList_Add(wArrayList* arrayList, void* obj) int index; if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); + EnterCriticalSection(&arrayList->lock); if (arrayList->size + 1 > arrayList->capacity) { @@ -216,7 +216,7 @@ int ArrayList_Add(wArrayList* arrayList, void* obj) index = arrayList->size; if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); return index; } @@ -228,7 +228,7 @@ int ArrayList_Add(wArrayList* arrayList, void* obj) void ArrayList_Insert(wArrayList* arrayList, int index, void* obj) { if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); + EnterCriticalSection(&arrayList->lock); if ((index >= 0) && (index < arrayList->size)) { @@ -237,7 +237,7 @@ void ArrayList_Insert(wArrayList* arrayList, int index, void* obj) } if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); } /** @@ -250,7 +250,7 @@ void ArrayList_Remove(wArrayList* arrayList, void* obj) BOOL found = FALSE; if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); + EnterCriticalSection(&arrayList->lock); for (index = 0; index < arrayList->size; index++) { @@ -265,7 +265,7 @@ void ArrayList_Remove(wArrayList* arrayList, void* obj) ArrayList_Shift(arrayList, index, -1); if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); } /** @@ -275,7 +275,7 @@ void ArrayList_Remove(wArrayList* arrayList, void* obj) void ArrayList_RemoveAt(wArrayList* arrayList, int index) { if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); + EnterCriticalSection(&arrayList->lock); if ((index >= 0) && (index < arrayList->size)) { @@ -283,7 +283,7 @@ void ArrayList_RemoveAt(wArrayList* arrayList, int index) } if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); } /** @@ -302,7 +302,7 @@ int ArrayList_IndexOf(wArrayList* arrayList, void* obj, int startIndex, int coun BOOL found = FALSE; if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); + EnterCriticalSection(&arrayList->lock); if (startIndex < 0) startIndex = 0; @@ -323,7 +323,7 @@ int ArrayList_IndexOf(wArrayList* arrayList, void* obj, int startIndex, int coun index = -1; if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); return index; } @@ -344,7 +344,7 @@ int ArrayList_LastIndexOf(wArrayList* arrayList, void* obj, int startIndex, int BOOL found = FALSE; if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); + EnterCriticalSection(&arrayList->lock); if (startIndex < 0) startIndex = 0; @@ -365,7 +365,7 @@ int ArrayList_LastIndexOf(wArrayList* arrayList, void* obj, int startIndex, int index = -1; if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); + LeaveCriticalSection(&arrayList->lock); return index; } @@ -390,7 +390,7 @@ wArrayList* ArrayList_New(BOOL synchronized) arrayList->array = (void**) malloc(sizeof(void*) * arrayList->capacity); - arrayList->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&arrayList->lock); ZeroMemory(&arrayList->object, sizeof(wObject)); } @@ -402,7 +402,7 @@ void ArrayList_Free(wArrayList* arrayList) { ArrayList_Clear(arrayList); - CloseHandle(arrayList->mutex); + DeleteCriticalSection(&arrayList->lock); free(arrayList->array); free(arrayList); } diff --git a/winpr/libwinpr/utils/collections/BufferPool.c b/winpr/libwinpr/utils/collections/BufferPool.c index d10ba1360..3a8c317e9 100644 --- a/winpr/libwinpr/utils/collections/BufferPool.c +++ b/winpr/libwinpr/utils/collections/BufferPool.c @@ -43,7 +43,7 @@ void* BufferPool_Take(wBufferPool* pool, int bufferSize) void* buffer = NULL; if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); if (pool->fixedSize) { @@ -64,7 +64,7 @@ void* BufferPool_Take(wBufferPool* pool, int bufferSize) } if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); return buffer; } @@ -76,7 +76,7 @@ void* BufferPool_Take(wBufferPool* pool, int bufferSize) void BufferPool_Return(wBufferPool* pool, void* buffer) { if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); if ((pool->size + 1) >= pool->capacity) { @@ -87,7 +87,7 @@ void BufferPool_Return(wBufferPool* pool, void* buffer) pool->array[(pool->size)++] = buffer; if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); } /** @@ -97,7 +97,7 @@ void BufferPool_Return(wBufferPool* pool, void* buffer) void BufferPool_Clear(wBufferPool* pool) { if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); while (pool->size > 0) { @@ -110,7 +110,7 @@ void BufferPool_Clear(wBufferPool* pool) } if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); } /** @@ -134,7 +134,7 @@ wBufferPool* BufferPool_New(BOOL synchronized, int fixedSize, DWORD alignment) pool->synchronized = synchronized; if (pool->synchronized) - pool->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&pool->lock); if (!pool->fixedSize) { @@ -156,7 +156,7 @@ void BufferPool_Free(wBufferPool* pool) BufferPool_Clear(pool); if (pool->synchronized) - CloseHandle(pool->mutex); + DeleteCriticalSection(&pool->lock); free(pool->array); diff --git a/winpr/libwinpr/utils/collections/CountdownEvent.c b/winpr/libwinpr/utils/collections/CountdownEvent.c index 787c7e31b..0f11bb2ec 100644 --- a/winpr/libwinpr/utils/collections/CountdownEvent.c +++ b/winpr/libwinpr/utils/collections/CountdownEvent.c @@ -89,14 +89,14 @@ HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown) void CountdownEvent_AddCount(wCountdownEvent* countdown, DWORD signalCount) { - WaitForSingleObject(countdown->mutex, INFINITE); + EnterCriticalSection(&countdown->lock); countdown->count += signalCount; if (countdown->count > 0) ResetEvent(countdown->event); - ReleaseMutex(countdown->mutex); + LeaveCriticalSection(&countdown->lock); } /** @@ -111,7 +111,7 @@ BOOL CountdownEvent_Signal(wCountdownEvent* countdown, DWORD signalCount) status = newStatus = oldStatus = FALSE; - WaitForSingleObject(countdown->mutex, INFINITE); + EnterCriticalSection(&countdown->lock); if (WaitForSingleObject(countdown->event, 0) == WAIT_OBJECT_0) oldStatus = TRUE; @@ -130,7 +130,7 @@ BOOL CountdownEvent_Signal(wCountdownEvent* countdown, DWORD signalCount) status = TRUE; } - ReleaseMutex(countdown->mutex); + LeaveCriticalSection(&countdown->lock); return status; } @@ -158,7 +158,7 @@ wCountdownEvent* CountdownEvent_New(DWORD initialCount) { countdown->count = initialCount; countdown->initialCount = initialCount; - countdown->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&countdown->lock); countdown->event = CreateEvent(NULL, TRUE, FALSE, NULL); if (countdown->count == 0) @@ -170,7 +170,7 @@ wCountdownEvent* CountdownEvent_New(DWORD initialCount) void CountdownEvent_Free(wCountdownEvent* countdown) { - CloseHandle(countdown->mutex); + DeleteCriticalSection(&countdown->lock); CloseHandle(countdown->event); free(countdown); diff --git a/winpr/libwinpr/utils/collections/MessageQueue.c b/winpr/libwinpr/utils/collections/MessageQueue.c index d13916922..871316ab0 100644 --- a/winpr/libwinpr/utils/collections/MessageQueue.c +++ b/winpr/libwinpr/utils/collections/MessageQueue.c @@ -69,7 +69,7 @@ BOOL MessageQueue_Wait(wMessageQueue* queue) void MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message) { - WaitForSingleObject(queue->mutex, INFINITE); + EnterCriticalSection(&queue->lock); if (queue->size == queue->capacity) { @@ -100,7 +100,7 @@ void MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message) if (queue->size > 0) SetEvent(queue->event); - ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); } void MessageQueue_Post(wMessageQueue* queue, void* context, UINT32 type, void* wParam, void* lParam) @@ -127,7 +127,7 @@ int MessageQueue_Get(wMessageQueue* queue, wMessage* message) if (!MessageQueue_Wait(queue)) return status; - WaitForSingleObject(queue->mutex, INFINITE); + EnterCriticalSection(&queue->lock); if (queue->size > 0) { @@ -142,7 +142,7 @@ int MessageQueue_Get(wMessageQueue* queue, wMessage* message) status = (message->id != WMQ_QUIT) ? 1 : 0; } - ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); return status; } @@ -151,7 +151,7 @@ int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove) { int status = 0; - WaitForSingleObject(queue->mutex, INFINITE); + EnterCriticalSection(&queue->lock); if (queue->size > 0) { @@ -169,7 +169,7 @@ int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove) } } - ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); return status; } @@ -194,7 +194,7 @@ wMessageQueue* MessageQueue_New() queue->array = (wMessage*) malloc(sizeof(wMessage) * queue->capacity); ZeroMemory(queue->array, sizeof(wMessage) * queue->capacity); - queue->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&queue->lock); queue->event = CreateEvent(NULL, TRUE, FALSE, NULL); } @@ -204,7 +204,7 @@ wMessageQueue* MessageQueue_New() void MessageQueue_Free(wMessageQueue* queue) { CloseHandle(queue->event); - CloseHandle(queue->mutex); + DeleteCriticalSection(&queue->lock); free(queue->array); free(queue); diff --git a/winpr/libwinpr/utils/collections/ObjectPool.c b/winpr/libwinpr/utils/collections/ObjectPool.c index 472eba1d2..488559085 100644 --- a/winpr/libwinpr/utils/collections/ObjectPool.c +++ b/winpr/libwinpr/utils/collections/ObjectPool.c @@ -43,7 +43,7 @@ void* ObjectPool_Take(wObjectPool* pool) void* obj = NULL; if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); if (pool->size > 0) obj = pool->array[--(pool->size)]; @@ -55,7 +55,7 @@ void* ObjectPool_Take(wObjectPool* pool) } if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); return obj; } @@ -67,7 +67,7 @@ void* ObjectPool_Take(wObjectPool* pool) void ObjectPool_Return(wObjectPool* pool, void* obj) { if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); if ((pool->size + 1) >= pool->capacity) { @@ -78,7 +78,7 @@ void ObjectPool_Return(wObjectPool* pool, void* obj) pool->array[(pool->size)++] = obj; if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); } /** @@ -88,7 +88,7 @@ void ObjectPool_Return(wObjectPool* pool, void* obj) void ObjectPool_Clear(wObjectPool* pool) { if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); while (pool->size > 0) { @@ -99,7 +99,7 @@ void ObjectPool_Clear(wObjectPool* pool) } if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); } /** @@ -119,7 +119,7 @@ wObjectPool* ObjectPool_New(BOOL synchronized) pool->synchronized = synchronized; if (pool->synchronized) - pool->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&pool->lock); pool->size = 0; pool->capacity = 32; @@ -136,7 +136,7 @@ void ObjectPool_Free(wObjectPool* pool) ObjectPool_Clear(pool); if (pool->synchronized) - CloseHandle(pool->mutex); + DeleteCriticalSection(&pool->lock); free(pool->array); diff --git a/winpr/libwinpr/utils/collections/PubSub.c b/winpr/libwinpr/utils/collections/PubSub.c index 1236d6858..26c89968a 100644 --- a/winpr/libwinpr/utils/collections/PubSub.c +++ b/winpr/libwinpr/utils/collections/PubSub.c @@ -46,14 +46,14 @@ wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count) * Methods */ -BOOL PubSub_Lock(wPubSub* pubSub) +void PubSub_Lock(wPubSub* pubSub) { - return (WaitForSingleObject(pubSub->mutex, INFINITE) == WAIT_OBJECT_0) ? TRUE : FALSE; + EnterCriticalSection(&pubSub->lock); } -BOOL PubSub_Unlock(wPubSub* pubSub) +void PubSub_Unlock(wPubSub* pubSub) { - return ReleaseMutex(pubSub->mutex); + LeaveCriticalSection(&pubSub->lock); } wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName) @@ -202,7 +202,7 @@ wPubSub* PubSub_New(BOOL synchronized) pubSub->synchronized = synchronized; if (pubSub->synchronized) - pubSub->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&pubSub->lock); pubSub->count = 0; pubSub->size = 64; @@ -219,7 +219,7 @@ void PubSub_Free(wPubSub* pubSub) if (pubSub) { if (pubSub->synchronized) - CloseHandle(pubSub->mutex); + DeleteCriticalSection(&pubSub->lock); if (pubSub->events) free(pubSub->events); diff --git a/winpr/libwinpr/utils/collections/Queue.c b/winpr/libwinpr/utils/collections/Queue.c index fa33df76a..95210649d 100644 --- a/winpr/libwinpr/utils/collections/Queue.c +++ b/winpr/libwinpr/utils/collections/Queue.c @@ -47,18 +47,18 @@ int Queue_Count(wQueue* queue) * Lock access to the ArrayList */ -BOOL Queue_Lock(wQueue* queue) +void Queue_Lock(wQueue* queue) { - return (WaitForSingleObject(queue->mutex, INFINITE) == WAIT_OBJECT_0) ? TRUE : FALSE; + EnterCriticalSection(&queue->lock); } /** * Unlock access to the ArrayList */ -BOOL Queue_Unlock(wQueue* queue) +void Queue_Unlock(wQueue* queue) { - return ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); } /** @@ -83,7 +83,7 @@ void Queue_Clear(wQueue* queue) int index; if (queue->synchronized) - WaitForSingleObject(queue->mutex, INFINITE); + EnterCriticalSection(&queue->lock); for (index = queue->head; index != queue->tail; index = (index + 1) % queue->capacity) { @@ -97,7 +97,7 @@ void Queue_Clear(wQueue* queue) queue->head = queue->tail = 0; if (queue->synchronized) - ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); } /** @@ -110,7 +110,7 @@ BOOL Queue_Contains(wQueue* queue, void* obj) BOOL found = FALSE; if (queue->synchronized) - WaitForSingleObject(queue->mutex, INFINITE); + EnterCriticalSection(&queue->lock); for (index = 0; index < queue->tail; index++) { @@ -122,7 +122,7 @@ BOOL Queue_Contains(wQueue* queue, void* obj) } if (queue->synchronized) - ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); return found; } @@ -134,7 +134,7 @@ BOOL Queue_Contains(wQueue* queue, void* obj) void Queue_Enqueue(wQueue* queue, void* obj) { if (queue->synchronized) - WaitForSingleObject(queue->mutex, INFINITE); + EnterCriticalSection(&queue->lock); if (queue->size == queue->capacity) { @@ -162,7 +162,7 @@ void Queue_Enqueue(wQueue* queue, void* obj) SetEvent(queue->event); if (queue->synchronized) - ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); } /** @@ -174,7 +174,7 @@ void* Queue_Dequeue(wQueue* queue) void* obj = NULL; if (queue->synchronized) - WaitForSingleObject(queue->mutex, INFINITE); + EnterCriticalSection(&queue->lock); if (queue->size > 0) { @@ -188,7 +188,7 @@ void* Queue_Dequeue(wQueue* queue) ResetEvent(queue->event); if (queue->synchronized) - ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); return obj; } @@ -202,13 +202,13 @@ void* Queue_Peek(wQueue* queue) void* obj = NULL; if (queue->synchronized) - WaitForSingleObject(queue->mutex, INFINITE); + EnterCriticalSection(&queue->lock); if (queue->size > 0) obj = queue->array[queue->head]; if (queue->synchronized) - ReleaseMutex(queue->mutex); + LeaveCriticalSection(&queue->lock); return obj; } @@ -243,7 +243,7 @@ wQueue* Queue_New(BOOL synchronized, int capacity, int growthFactor) queue->array = (void**) malloc(sizeof(void*) * queue->capacity); ZeroMemory(queue->array, sizeof(void*) * queue->capacity); - queue->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&queue->lock); queue->event = CreateEvent(NULL, TRUE, FALSE, NULL); ZeroMemory(&queue->object, sizeof(wObject)); @@ -257,7 +257,7 @@ void Queue_Free(wQueue* queue) Queue_Clear(queue); CloseHandle(queue->event); - CloseHandle(queue->mutex); + DeleteCriticalSection(&queue->lock); free(queue->array); free(queue); } diff --git a/winpr/libwinpr/utils/collections/Reference.c b/winpr/libwinpr/utils/collections/Reference.c index 7df1c84b2..6c32ffeac 100644 --- a/winpr/libwinpr/utils/collections/Reference.c +++ b/winpr/libwinpr/utils/collections/Reference.c @@ -88,7 +88,7 @@ UINT32 ReferenceTable_Add(wReferenceTable* referenceTable, void* ptr) wReference* reference = NULL; if (referenceTable->synchronized) - WaitForSingleObject(referenceTable->mutex, INFINITE); + EnterCriticalSection(&referenceTable->lock); reference = ReferenceTable_FindEntry(referenceTable, ptr); @@ -102,7 +102,7 @@ UINT32 ReferenceTable_Add(wReferenceTable* referenceTable, void* ptr) count = ++(reference->Count); if (referenceTable->synchronized) - ReleaseMutex(referenceTable->mutex); + LeaveCriticalSection(&referenceTable->lock); return count; } @@ -113,7 +113,7 @@ UINT32 ReferenceTable_Release(wReferenceTable* referenceTable, void* ptr) wReference* reference = NULL; if (referenceTable->synchronized) - WaitForSingleObject(referenceTable->mutex, INFINITE); + EnterCriticalSection(&referenceTable->lock); reference = ReferenceTable_FindEntry(referenceTable, ptr); @@ -133,7 +133,7 @@ UINT32 ReferenceTable_Release(wReferenceTable* referenceTable, void* ptr) } if (referenceTable->synchronized) - ReleaseMutex(referenceTable->mutex); + LeaveCriticalSection(&referenceTable->lock); return count; } @@ -154,7 +154,7 @@ wReferenceTable* ReferenceTable_New(BOOL synchronized, void* context, REFERENCE_ ZeroMemory(referenceTable->array, sizeof(wReference) * referenceTable->size); referenceTable->synchronized = synchronized; - referenceTable->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&referenceTable->lock); } return referenceTable; @@ -164,7 +164,7 @@ void ReferenceTable_Free(wReferenceTable* referenceTable) { if (referenceTable) { - CloseHandle(referenceTable->mutex); + DeleteCriticalSection(&referenceTable->lock); free(referenceTable->array); free(referenceTable); } diff --git a/winpr/libwinpr/utils/collections/Stack.c b/winpr/libwinpr/utils/collections/Stack.c index 383fe0de1..a0d063eee 100644 --- a/winpr/libwinpr/utils/collections/Stack.c +++ b/winpr/libwinpr/utils/collections/Stack.c @@ -84,7 +84,7 @@ BOOL Stack_Contains(wStack* stack, void* obj) void Stack_Push(wStack* stack, void* obj) { if (stack->synchronized) - WaitForSingleObject(stack->mutex, INFINITE); + EnterCriticalSection(&stack->lock); if ((stack->size + 1) >= stack->capacity) { @@ -95,7 +95,7 @@ void Stack_Push(wStack* stack, void* obj) stack->array[(stack->size)++] = obj; if (stack->synchronized) - ReleaseMutex(stack->mutex); + LeaveCriticalSection(&stack->lock); } /** @@ -107,13 +107,13 @@ void* Stack_Pop(wStack* stack) void* obj = NULL; if (stack->synchronized) - WaitForSingleObject(stack->mutex, INFINITE); + EnterCriticalSection(&stack->lock); if (stack->size > 0) obj = stack->array[--(stack->size)]; if (stack->synchronized) - ReleaseMutex(stack->mutex); + LeaveCriticalSection(&stack->lock); return obj; } @@ -127,13 +127,13 @@ void* Stack_Peek(wStack* stack) void* obj = NULL; if (stack->synchronized) - WaitForSingleObject(stack->mutex, INFINITE); + EnterCriticalSection(&stack->lock); if (stack->size > 0) obj = stack->array[stack->size]; if (stack->synchronized) - ReleaseMutex(stack->mutex); + LeaveCriticalSection(&stack->lock); return obj; } @@ -153,7 +153,7 @@ wStack* Stack_New(BOOL synchronized) stack->synchronized = synchronized; if (stack->synchronized) - stack->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&stack->lock); stack->size = 0; stack->capacity = 32; @@ -168,7 +168,7 @@ void Stack_Free(wStack* stack) if (stack) { if (stack->synchronized) - CloseHandle(stack->mutex); + DeleteCriticalSection(&stack->lock); free(stack->array); diff --git a/winpr/libwinpr/utils/collections/StreamPool.c b/winpr/libwinpr/utils/collections/StreamPool.c index 1605c5332..418681c61 100644 --- a/winpr/libwinpr/utils/collections/StreamPool.c +++ b/winpr/libwinpr/utils/collections/StreamPool.c @@ -118,7 +118,7 @@ wStream* StreamPool_Take(wStreamPool* pool, size_t size) BOOL found = FALSE; if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); if (size == 0) size = pool->defaultSize; @@ -153,7 +153,7 @@ wStream* StreamPool_Take(wStreamPool* pool, size_t size) StreamPool_AddUsed(pool, s); if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); return s; } @@ -165,7 +165,7 @@ wStream* StreamPool_Take(wStreamPool* pool, size_t size) void StreamPool_Return(wStreamPool* pool, wStream* s) { if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); if ((pool->aSize + 1) >= pool->aCapacity) { @@ -177,7 +177,7 @@ void StreamPool_Return(wStreamPool* pool, wStream* s) StreamPool_RemoveUsed(pool, s); if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); } /** @@ -186,7 +186,7 @@ void StreamPool_Return(wStreamPool* pool, wStream* s) void StreamPool_Lock(wStreamPool* pool) { - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); } /** @@ -195,7 +195,7 @@ void StreamPool_Lock(wStreamPool* pool) void StreamPool_Unlock(wStreamPool* pool) { - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); } /** @@ -241,7 +241,7 @@ wStream* StreamPool_Find(wStreamPool* pool, BYTE* ptr) wStream* s = NULL; BOOL found = FALSE; - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); for (index = 0; index < pool->uSize; index++) { @@ -254,7 +254,7 @@ wStream* StreamPool_Find(wStreamPool* pool, BYTE* ptr) } } - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); return (found) ? s : NULL; } @@ -294,7 +294,7 @@ void StreamPool_Release(wStreamPool* pool, BYTE* ptr) void StreamPool_Clear(wStreamPool* pool) { if (pool->synchronized) - WaitForSingleObject(pool->mutex, INFINITE); + EnterCriticalSection(&pool->lock); while (pool->aSize > 0) { @@ -303,7 +303,7 @@ void StreamPool_Clear(wStreamPool* pool) } if (pool->synchronized) - ReleaseMutex(pool->mutex); + LeaveCriticalSection(&pool->lock); } /** @@ -323,8 +323,7 @@ wStreamPool* StreamPool_New(BOOL synchronized, size_t defaultSize) pool->synchronized = synchronized; pool->defaultSize = defaultSize; - if (pool->synchronized) - pool->mutex = CreateMutex(NULL, FALSE, NULL); + InitializeCriticalSection(&pool->lock); pool->aSize = 0; pool->aCapacity = 32; @@ -344,8 +343,7 @@ void StreamPool_Free(wStreamPool* pool) { StreamPool_Clear(pool); - if (pool->synchronized) - CloseHandle(pool->mutex); + DeleteCriticalSection(&pool->lock); free(pool->aArray); free(pool->uArray);