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.
This commit is contained in:
Norbert Federa 2013-08-02 12:07:05 +02:00
parent 8b621518b6
commit 81ef251fc8
13 changed files with 153 additions and 143 deletions

View File

@ -58,7 +58,7 @@ struct _wQueue
int tail; int tail;
int size; int size;
void** array; void** array;
HANDLE mutex; CRITICAL_SECTION lock;
HANDLE event; HANDLE event;
wObject object; wObject object;
@ -67,8 +67,8 @@ typedef struct _wQueue wQueue;
WINPR_API int Queue_Count(wQueue* queue); WINPR_API int Queue_Count(wQueue* queue);
WINPR_API BOOL Queue_Lock(wQueue* queue); WINPR_API void Queue_Lock(wQueue* queue);
WINPR_API BOOL Queue_Unlock(wQueue* queue); WINPR_API void Queue_Unlock(wQueue* queue);
WINPR_API HANDLE Queue_Event(wQueue* queue); WINPR_API HANDLE Queue_Event(wQueue* queue);
@ -93,7 +93,7 @@ struct _wStack
int size; int size;
int capacity; int capacity;
void** array; void** array;
HANDLE mutex; CRITICAL_SECTION lock;
BOOL synchronized; BOOL synchronized;
wObject object; wObject object;
}; };
@ -125,7 +125,7 @@ struct _wArrayList
int size; int size;
void** array; void** array;
HANDLE mutex; CRITICAL_SECTION lock;
wObject object; wObject object;
}; };
@ -137,8 +137,8 @@ WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList);
WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList); WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList);
WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList); WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList);
WINPR_API BOOL ArrayList_Lock(wArrayList* arrayList); WINPR_API void ArrayList_Lock(wArrayList* arrayList);
WINPR_API BOOL ArrayList_Unlock(wArrayList* arrayList); WINPR_API void ArrayList_Unlock(wArrayList* arrayList);
WINPR_API void* ArrayList_GetItem(wArrayList* arrayList, int index); WINPR_API void* ArrayList_GetItem(wArrayList* arrayList, int index);
WINPR_API void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj); WINPR_API void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj);
@ -165,7 +165,7 @@ WINPR_API void ArrayList_Free(wArrayList* arrayList);
struct _wDictionary struct _wDictionary
{ {
BOOL synchronized; BOOL synchronized;
HANDLE mutex; CRITICAL_SECTION lock;
}; };
typedef struct _wDictionary wDictionary; typedef struct _wDictionary wDictionary;
@ -184,7 +184,7 @@ struct _wListDictionaryItem
struct _wListDictionary struct _wListDictionary
{ {
BOOL synchronized; BOOL synchronized;
HANDLE mutex; CRITICAL_SECTION lock;
wListDictionaryItem* head; wListDictionaryItem* head;
}; };
@ -227,7 +227,7 @@ typedef int (*REFERENCE_FREE)(void* context, void* ptr);
struct _wReferenceTable struct _wReferenceTable
{ {
UINT32 size; UINT32 size;
HANDLE mutex; CRITICAL_SECTION lock;
void* context; void* context;
BOOL synchronized; BOOL synchronized;
wReference* array; wReference* array;
@ -246,7 +246,7 @@ WINPR_API void ReferenceTable_Free(wReferenceTable* referenceTable);
struct _wCountdownEvent struct _wCountdownEvent
{ {
DWORD count; DWORD count;
HANDLE mutex; CRITICAL_SECTION lock;
HANDLE event; HANDLE event;
DWORD initialCount; DWORD initialCount;
}; };
@ -271,7 +271,7 @@ struct _wBufferPool
int size; int size;
int capacity; int capacity;
void** array; void** array;
HANDLE mutex; CRITICAL_SECTION lock;
int fixedSize; int fixedSize;
DWORD alignment; DWORD alignment;
BOOL synchronized; BOOL synchronized;
@ -292,7 +292,7 @@ struct _wObjectPool
int size; int size;
int capacity; int capacity;
void** array; void** array;
HANDLE mutex; CRITICAL_SECTION lock;
wObject object; wObject object;
BOOL synchronized; BOOL synchronized;
}; };
@ -330,7 +330,7 @@ struct _wMessageQueue
int size; int size;
int capacity; int capacity;
wMessage* array; wMessage* array;
HANDLE mutex; CRITICAL_SECTION lock;
HANDLE event; HANDLE event;
}; };
typedef struct _wMessageQueue wMessageQueue; typedef struct _wMessageQueue wMessageQueue;
@ -427,7 +427,7 @@ typedef struct _wEventType wEventType;
struct _wPubSub struct _wPubSub
{ {
HANDLE mutex; CRITICAL_SECTION lock;
BOOL synchronized; BOOL synchronized;
int size; int size;
@ -436,8 +436,8 @@ struct _wPubSub
}; };
typedef struct _wPubSub wPubSub; typedef struct _wPubSub wPubSub;
WINPR_API BOOL PubSub_Lock(wPubSub* pubSub); WINPR_API void PubSub_Lock(wPubSub* pubSub);
WINPR_API BOOL PubSub_Unlock(wPubSub* pubSub); WINPR_API void PubSub_Unlock(wPubSub* pubSub);
WINPR_API wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count); WINPR_API wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count);
WINPR_API void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, int count); WINPR_API void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, int count);

View File

@ -24,6 +24,7 @@
#include <winpr/winpr.h> #include <winpr/winpr.h>
#include <winpr/wtypes.h> #include <winpr/wtypes.h>
#include <winpr/endian.h> #include <winpr/endian.h>
#include <winpr/synch.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -236,7 +237,7 @@ struct _wStreamPool
int uCapacity; int uCapacity;
wStream** uArray; wStream** uArray;
HANDLE mutex; CRITICAL_SECTION lock;
BOOL synchronized; BOOL synchronized;
size_t defaultSize; size_t defaultSize;
}; };

View File

@ -31,6 +31,12 @@
#ifndef _WIN32 #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) VOID InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
{ {
if (lpCriticalSection) if (lpCriticalSection)
@ -44,60 +50,65 @@ VOID InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
lpCriticalSection->OwningThread = NULL; lpCriticalSection->OwningThread = NULL;
lpCriticalSection->LockSemaphore = NULL; lpCriticalSection->LockSemaphore = NULL;
lpCriticalSection->LockSemaphore = (winpr_sem_t*) malloc(sizeof(winpr_sem_t)); lpCriticalSection->LockSemaphore = malloc(sizeof(pthread_mutex_t));
#if defined __APPLE__ pthread_mutex_init(lpCriticalSection->LockSemaphore, NULL);
semaphore_create(mach_task_self(), lpCriticalSection->LockSemaphore, SYNC_POLICY_FIFO, 1);
#else
sem_init(lpCriticalSection->LockSemaphore, 0, 1);
#endif
} }
} }
BOOL InitializeCriticalSectionEx(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount, DWORD Flags) 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) BOOL InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount)
{ {
InitializeCriticalSection(lpCriticalSection);
SetCriticalSectionSpinCount(lpCriticalSection, dwSpinCount);
return TRUE; return TRUE;
} }
DWORD SetCriticalSectionSpinCount(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount) DWORD SetCriticalSectionSpinCount(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount)
{ {
return 0; DWORD dwPreviousSpinCount = lpCriticalSection->SpinCount;
lpCriticalSection->SpinCount = dwSpinCount;
return dwPreviousSpinCount;
} }
VOID EnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection) VOID EnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
{ {
#if defined __APPLE__ /**
semaphore_wait(*((winpr_sem_t*) lpCriticalSection->LockSemaphore)); * Linux NPTL thread synchronization primitives are implemented using
#else * the futex system calls ... no need for performing a trylock loop.
sem_wait((winpr_sem_t*) lpCriticalSection->LockSemaphore); */
#if !defined(__linux__)
ULONG spin = lpCriticalSection->SpinCount;
while (spin--)
{
if (pthread_mutex_trylock((pthread_mutex_t*)lpCriticalSection->LockSemaphore) == 0)
return;
pthread_yield();
}
#endif #endif
pthread_mutex_lock((pthread_mutex_t*)lpCriticalSection->LockSemaphore);
} }
BOOL TryEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection) BOOL TryEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
{ {
return TRUE; return (pthread_mutex_trylock((pthread_mutex_t*)lpCriticalSection->LockSemaphore) == 0 ? TRUE : FALSE);
} }
VOID LeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection) VOID LeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
{ {
#if defined __APPLE__ pthread_mutex_unlock((pthread_mutex_t*)lpCriticalSection->LockSemaphore);
semaphore_signal(*((winpr_sem_t*) lpCriticalSection->LockSemaphore));
#else
sem_post((winpr_sem_t*) lpCriticalSection->LockSemaphore);
#endif
} }
VOID DeleteCriticalSection(LPCRITICAL_SECTION lpCriticalSection) VOID DeleteCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
{ {
#if defined __APPLE__ pthread_mutex_destroy((pthread_mutex_t*)lpCriticalSection->LockSemaphore);
semaphore_destroy(mach_task_self(), *((winpr_sem_t*) lpCriticalSection->LockSemaphore)); free(lpCriticalSection->LockSemaphore);
#else
sem_destroy((winpr_sem_t*) lpCriticalSection->LockSemaphore);
#endif
} }
#endif #endif

View File

@ -83,18 +83,18 @@ BOOL ArrayList_IsSynchronized(wArrayList* arrayList)
* Lock access to the 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 * 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; int index;
if (arrayList->synchronized) if (arrayList->synchronized)
WaitForSingleObject(arrayList->mutex, INFINITE); EnterCriticalSection(&arrayList->lock);
for (index = 0; index < arrayList->size; index++) for (index = 0; index < arrayList->size; index++)
{ {
@ -177,7 +177,7 @@ void ArrayList_Clear(wArrayList* arrayList)
arrayList->size = 0; arrayList->size = 0;
if (arrayList->synchronized) 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) BOOL ArrayList_Contains(wArrayList* arrayList, void* obj)
{ {
if (arrayList->synchronized) if (arrayList->synchronized)
WaitForSingleObject(arrayList->mutex, INFINITE); EnterCriticalSection(&arrayList->lock);
if (arrayList->synchronized) if (arrayList->synchronized)
ReleaseMutex(arrayList->mutex); LeaveCriticalSection(&arrayList->lock);
return FALSE; return FALSE;
} }
@ -204,7 +204,7 @@ int ArrayList_Add(wArrayList* arrayList, void* obj)
int index; int index;
if (arrayList->synchronized) if (arrayList->synchronized)
WaitForSingleObject(arrayList->mutex, INFINITE); EnterCriticalSection(&arrayList->lock);
if (arrayList->size + 1 > arrayList->capacity) if (arrayList->size + 1 > arrayList->capacity)
{ {
@ -216,7 +216,7 @@ int ArrayList_Add(wArrayList* arrayList, void* obj)
index = arrayList->size; index = arrayList->size;
if (arrayList->synchronized) if (arrayList->synchronized)
ReleaseMutex(arrayList->mutex); LeaveCriticalSection(&arrayList->lock);
return index; return index;
} }
@ -228,7 +228,7 @@ int ArrayList_Add(wArrayList* arrayList, void* obj)
void ArrayList_Insert(wArrayList* arrayList, int index, void* obj) void ArrayList_Insert(wArrayList* arrayList, int index, void* obj)
{ {
if (arrayList->synchronized) if (arrayList->synchronized)
WaitForSingleObject(arrayList->mutex, INFINITE); EnterCriticalSection(&arrayList->lock);
if ((index >= 0) && (index < arrayList->size)) if ((index >= 0) && (index < arrayList->size))
{ {
@ -237,7 +237,7 @@ void ArrayList_Insert(wArrayList* arrayList, int index, void* obj)
} }
if (arrayList->synchronized) if (arrayList->synchronized)
ReleaseMutex(arrayList->mutex); LeaveCriticalSection(&arrayList->lock);
} }
/** /**
@ -250,7 +250,7 @@ void ArrayList_Remove(wArrayList* arrayList, void* obj)
BOOL found = FALSE; BOOL found = FALSE;
if (arrayList->synchronized) if (arrayList->synchronized)
WaitForSingleObject(arrayList->mutex, INFINITE); EnterCriticalSection(&arrayList->lock);
for (index = 0; index < arrayList->size; index++) for (index = 0; index < arrayList->size; index++)
{ {
@ -265,7 +265,7 @@ void ArrayList_Remove(wArrayList* arrayList, void* obj)
ArrayList_Shift(arrayList, index, -1); ArrayList_Shift(arrayList, index, -1);
if (arrayList->synchronized) 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) void ArrayList_RemoveAt(wArrayList* arrayList, int index)
{ {
if (arrayList->synchronized) if (arrayList->synchronized)
WaitForSingleObject(arrayList->mutex, INFINITE); EnterCriticalSection(&arrayList->lock);
if ((index >= 0) && (index < arrayList->size)) if ((index >= 0) && (index < arrayList->size))
{ {
@ -283,7 +283,7 @@ void ArrayList_RemoveAt(wArrayList* arrayList, int index)
} }
if (arrayList->synchronized) 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; BOOL found = FALSE;
if (arrayList->synchronized) if (arrayList->synchronized)
WaitForSingleObject(arrayList->mutex, INFINITE); EnterCriticalSection(&arrayList->lock);
if (startIndex < 0) if (startIndex < 0)
startIndex = 0; startIndex = 0;
@ -323,7 +323,7 @@ int ArrayList_IndexOf(wArrayList* arrayList, void* obj, int startIndex, int coun
index = -1; index = -1;
if (arrayList->synchronized) if (arrayList->synchronized)
ReleaseMutex(arrayList->mutex); LeaveCriticalSection(&arrayList->lock);
return index; return index;
} }
@ -344,7 +344,7 @@ int ArrayList_LastIndexOf(wArrayList* arrayList, void* obj, int startIndex, int
BOOL found = FALSE; BOOL found = FALSE;
if (arrayList->synchronized) if (arrayList->synchronized)
WaitForSingleObject(arrayList->mutex, INFINITE); EnterCriticalSection(&arrayList->lock);
if (startIndex < 0) if (startIndex < 0)
startIndex = 0; startIndex = 0;
@ -365,7 +365,7 @@ int ArrayList_LastIndexOf(wArrayList* arrayList, void* obj, int startIndex, int
index = -1; index = -1;
if (arrayList->synchronized) if (arrayList->synchronized)
ReleaseMutex(arrayList->mutex); LeaveCriticalSection(&arrayList->lock);
return index; return index;
} }
@ -390,7 +390,7 @@ wArrayList* ArrayList_New(BOOL synchronized)
arrayList->array = (void**) malloc(sizeof(void*) * arrayList->capacity); arrayList->array = (void**) malloc(sizeof(void*) * arrayList->capacity);
arrayList->mutex = CreateMutex(NULL, FALSE, NULL); InitializeCriticalSection(&arrayList->lock);
ZeroMemory(&arrayList->object, sizeof(wObject)); ZeroMemory(&arrayList->object, sizeof(wObject));
} }
@ -402,7 +402,7 @@ void ArrayList_Free(wArrayList* arrayList)
{ {
ArrayList_Clear(arrayList); ArrayList_Clear(arrayList);
CloseHandle(arrayList->mutex); DeleteCriticalSection(&arrayList->lock);
free(arrayList->array); free(arrayList->array);
free(arrayList); free(arrayList);
} }

View File

@ -43,7 +43,7 @@ void* BufferPool_Take(wBufferPool* pool, int bufferSize)
void* buffer = NULL; void* buffer = NULL;
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
if (pool->fixedSize) if (pool->fixedSize)
{ {
@ -64,7 +64,7 @@ void* BufferPool_Take(wBufferPool* pool, int bufferSize)
} }
if (pool->synchronized) if (pool->synchronized)
ReleaseMutex(pool->mutex); LeaveCriticalSection(&pool->lock);
return buffer; return buffer;
} }
@ -76,7 +76,7 @@ void* BufferPool_Take(wBufferPool* pool, int bufferSize)
void BufferPool_Return(wBufferPool* pool, void* buffer) void BufferPool_Return(wBufferPool* pool, void* buffer)
{ {
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
if ((pool->size + 1) >= pool->capacity) if ((pool->size + 1) >= pool->capacity)
{ {
@ -87,7 +87,7 @@ void BufferPool_Return(wBufferPool* pool, void* buffer)
pool->array[(pool->size)++] = buffer; pool->array[(pool->size)++] = buffer;
if (pool->synchronized) 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) void BufferPool_Clear(wBufferPool* pool)
{ {
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
while (pool->size > 0) while (pool->size > 0)
{ {
@ -110,7 +110,7 @@ void BufferPool_Clear(wBufferPool* pool)
} }
if (pool->synchronized) 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; pool->synchronized = synchronized;
if (pool->synchronized) if (pool->synchronized)
pool->mutex = CreateMutex(NULL, FALSE, NULL); InitializeCriticalSection(&pool->lock);
if (!pool->fixedSize) if (!pool->fixedSize)
{ {
@ -156,7 +156,7 @@ void BufferPool_Free(wBufferPool* pool)
BufferPool_Clear(pool); BufferPool_Clear(pool);
if (pool->synchronized) if (pool->synchronized)
CloseHandle(pool->mutex); DeleteCriticalSection(&pool->lock);
free(pool->array); free(pool->array);

View File

@ -89,14 +89,14 @@ HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown)
void CountdownEvent_AddCount(wCountdownEvent* countdown, DWORD signalCount) void CountdownEvent_AddCount(wCountdownEvent* countdown, DWORD signalCount)
{ {
WaitForSingleObject(countdown->mutex, INFINITE); EnterCriticalSection(&countdown->lock);
countdown->count += signalCount; countdown->count += signalCount;
if (countdown->count > 0) if (countdown->count > 0)
ResetEvent(countdown->event); ResetEvent(countdown->event);
ReleaseMutex(countdown->mutex); LeaveCriticalSection(&countdown->lock);
} }
/** /**
@ -111,7 +111,7 @@ BOOL CountdownEvent_Signal(wCountdownEvent* countdown, DWORD signalCount)
status = newStatus = oldStatus = FALSE; status = newStatus = oldStatus = FALSE;
WaitForSingleObject(countdown->mutex, INFINITE); EnterCriticalSection(&countdown->lock);
if (WaitForSingleObject(countdown->event, 0) == WAIT_OBJECT_0) if (WaitForSingleObject(countdown->event, 0) == WAIT_OBJECT_0)
oldStatus = TRUE; oldStatus = TRUE;
@ -130,7 +130,7 @@ BOOL CountdownEvent_Signal(wCountdownEvent* countdown, DWORD signalCount)
status = TRUE; status = TRUE;
} }
ReleaseMutex(countdown->mutex); LeaveCriticalSection(&countdown->lock);
return status; return status;
} }
@ -158,7 +158,7 @@ wCountdownEvent* CountdownEvent_New(DWORD initialCount)
{ {
countdown->count = initialCount; countdown->count = initialCount;
countdown->initialCount = initialCount; countdown->initialCount = initialCount;
countdown->mutex = CreateMutex(NULL, FALSE, NULL); InitializeCriticalSection(&countdown->lock);
countdown->event = CreateEvent(NULL, TRUE, FALSE, NULL); countdown->event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (countdown->count == 0) if (countdown->count == 0)
@ -170,7 +170,7 @@ wCountdownEvent* CountdownEvent_New(DWORD initialCount)
void CountdownEvent_Free(wCountdownEvent* countdown) void CountdownEvent_Free(wCountdownEvent* countdown)
{ {
CloseHandle(countdown->mutex); DeleteCriticalSection(&countdown->lock);
CloseHandle(countdown->event); CloseHandle(countdown->event);
free(countdown); free(countdown);

View File

@ -69,7 +69,7 @@ BOOL MessageQueue_Wait(wMessageQueue* queue)
void MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message) void MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message)
{ {
WaitForSingleObject(queue->mutex, INFINITE); EnterCriticalSection(&queue->lock);
if (queue->size == queue->capacity) if (queue->size == queue->capacity)
{ {
@ -100,7 +100,7 @@ void MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message)
if (queue->size > 0) if (queue->size > 0)
SetEvent(queue->event); SetEvent(queue->event);
ReleaseMutex(queue->mutex); LeaveCriticalSection(&queue->lock);
} }
void MessageQueue_Post(wMessageQueue* queue, void* context, UINT32 type, void* wParam, void* lParam) 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)) if (!MessageQueue_Wait(queue))
return status; return status;
WaitForSingleObject(queue->mutex, INFINITE); EnterCriticalSection(&queue->lock);
if (queue->size > 0) if (queue->size > 0)
{ {
@ -142,7 +142,7 @@ int MessageQueue_Get(wMessageQueue* queue, wMessage* message)
status = (message->id != WMQ_QUIT) ? 1 : 0; status = (message->id != WMQ_QUIT) ? 1 : 0;
} }
ReleaseMutex(queue->mutex); LeaveCriticalSection(&queue->lock);
return status; return status;
} }
@ -151,7 +151,7 @@ int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove)
{ {
int status = 0; int status = 0;
WaitForSingleObject(queue->mutex, INFINITE); EnterCriticalSection(&queue->lock);
if (queue->size > 0) 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; return status;
} }
@ -194,7 +194,7 @@ wMessageQueue* MessageQueue_New()
queue->array = (wMessage*) malloc(sizeof(wMessage) * queue->capacity); queue->array = (wMessage*) malloc(sizeof(wMessage) * queue->capacity);
ZeroMemory(queue->array, 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); queue->event = CreateEvent(NULL, TRUE, FALSE, NULL);
} }
@ -204,7 +204,7 @@ wMessageQueue* MessageQueue_New()
void MessageQueue_Free(wMessageQueue* queue) void MessageQueue_Free(wMessageQueue* queue)
{ {
CloseHandle(queue->event); CloseHandle(queue->event);
CloseHandle(queue->mutex); DeleteCriticalSection(&queue->lock);
free(queue->array); free(queue->array);
free(queue); free(queue);

View File

@ -43,7 +43,7 @@ void* ObjectPool_Take(wObjectPool* pool)
void* obj = NULL; void* obj = NULL;
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
if (pool->size > 0) if (pool->size > 0)
obj = pool->array[--(pool->size)]; obj = pool->array[--(pool->size)];
@ -55,7 +55,7 @@ void* ObjectPool_Take(wObjectPool* pool)
} }
if (pool->synchronized) if (pool->synchronized)
ReleaseMutex(pool->mutex); LeaveCriticalSection(&pool->lock);
return obj; return obj;
} }
@ -67,7 +67,7 @@ void* ObjectPool_Take(wObjectPool* pool)
void ObjectPool_Return(wObjectPool* pool, void* obj) void ObjectPool_Return(wObjectPool* pool, void* obj)
{ {
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
if ((pool->size + 1) >= pool->capacity) if ((pool->size + 1) >= pool->capacity)
{ {
@ -78,7 +78,7 @@ void ObjectPool_Return(wObjectPool* pool, void* obj)
pool->array[(pool->size)++] = obj; pool->array[(pool->size)++] = obj;
if (pool->synchronized) 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) void ObjectPool_Clear(wObjectPool* pool)
{ {
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
while (pool->size > 0) while (pool->size > 0)
{ {
@ -99,7 +99,7 @@ void ObjectPool_Clear(wObjectPool* pool)
} }
if (pool->synchronized) if (pool->synchronized)
ReleaseMutex(pool->mutex); LeaveCriticalSection(&pool->lock);
} }
/** /**
@ -119,7 +119,7 @@ wObjectPool* ObjectPool_New(BOOL synchronized)
pool->synchronized = synchronized; pool->synchronized = synchronized;
if (pool->synchronized) if (pool->synchronized)
pool->mutex = CreateMutex(NULL, FALSE, NULL); InitializeCriticalSection(&pool->lock);
pool->size = 0; pool->size = 0;
pool->capacity = 32; pool->capacity = 32;
@ -136,7 +136,7 @@ void ObjectPool_Free(wObjectPool* pool)
ObjectPool_Clear(pool); ObjectPool_Clear(pool);
if (pool->synchronized) if (pool->synchronized)
CloseHandle(pool->mutex); DeleteCriticalSection(&pool->lock);
free(pool->array); free(pool->array);

View File

@ -46,14 +46,14 @@ wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count)
* Methods * 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) wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName)
@ -202,7 +202,7 @@ wPubSub* PubSub_New(BOOL synchronized)
pubSub->synchronized = synchronized; pubSub->synchronized = synchronized;
if (pubSub->synchronized) if (pubSub->synchronized)
pubSub->mutex = CreateMutex(NULL, FALSE, NULL); InitializeCriticalSection(&pubSub->lock);
pubSub->count = 0; pubSub->count = 0;
pubSub->size = 64; pubSub->size = 64;
@ -219,7 +219,7 @@ void PubSub_Free(wPubSub* pubSub)
if (pubSub) if (pubSub)
{ {
if (pubSub->synchronized) if (pubSub->synchronized)
CloseHandle(pubSub->mutex); DeleteCriticalSection(&pubSub->lock);
if (pubSub->events) if (pubSub->events)
free(pubSub->events); free(pubSub->events);

View File

@ -47,18 +47,18 @@ int Queue_Count(wQueue* queue)
* Lock access to the ArrayList * 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 * 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; int index;
if (queue->synchronized) if (queue->synchronized)
WaitForSingleObject(queue->mutex, INFINITE); EnterCriticalSection(&queue->lock);
for (index = queue->head; index != queue->tail; index = (index + 1) % queue->capacity) 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; queue->head = queue->tail = 0;
if (queue->synchronized) if (queue->synchronized)
ReleaseMutex(queue->mutex); LeaveCriticalSection(&queue->lock);
} }
/** /**
@ -110,7 +110,7 @@ BOOL Queue_Contains(wQueue* queue, void* obj)
BOOL found = FALSE; BOOL found = FALSE;
if (queue->synchronized) if (queue->synchronized)
WaitForSingleObject(queue->mutex, INFINITE); EnterCriticalSection(&queue->lock);
for (index = 0; index < queue->tail; index++) for (index = 0; index < queue->tail; index++)
{ {
@ -122,7 +122,7 @@ BOOL Queue_Contains(wQueue* queue, void* obj)
} }
if (queue->synchronized) if (queue->synchronized)
ReleaseMutex(queue->mutex); LeaveCriticalSection(&queue->lock);
return found; return found;
} }
@ -134,7 +134,7 @@ BOOL Queue_Contains(wQueue* queue, void* obj)
void Queue_Enqueue(wQueue* queue, void* obj) void Queue_Enqueue(wQueue* queue, void* obj)
{ {
if (queue->synchronized) if (queue->synchronized)
WaitForSingleObject(queue->mutex, INFINITE); EnterCriticalSection(&queue->lock);
if (queue->size == queue->capacity) if (queue->size == queue->capacity)
{ {
@ -162,7 +162,7 @@ void Queue_Enqueue(wQueue* queue, void* obj)
SetEvent(queue->event); SetEvent(queue->event);
if (queue->synchronized) if (queue->synchronized)
ReleaseMutex(queue->mutex); LeaveCriticalSection(&queue->lock);
} }
/** /**
@ -174,7 +174,7 @@ void* Queue_Dequeue(wQueue* queue)
void* obj = NULL; void* obj = NULL;
if (queue->synchronized) if (queue->synchronized)
WaitForSingleObject(queue->mutex, INFINITE); EnterCriticalSection(&queue->lock);
if (queue->size > 0) if (queue->size > 0)
{ {
@ -188,7 +188,7 @@ void* Queue_Dequeue(wQueue* queue)
ResetEvent(queue->event); ResetEvent(queue->event);
if (queue->synchronized) if (queue->synchronized)
ReleaseMutex(queue->mutex); LeaveCriticalSection(&queue->lock);
return obj; return obj;
} }
@ -202,13 +202,13 @@ void* Queue_Peek(wQueue* queue)
void* obj = NULL; void* obj = NULL;
if (queue->synchronized) if (queue->synchronized)
WaitForSingleObject(queue->mutex, INFINITE); EnterCriticalSection(&queue->lock);
if (queue->size > 0) if (queue->size > 0)
obj = queue->array[queue->head]; obj = queue->array[queue->head];
if (queue->synchronized) if (queue->synchronized)
ReleaseMutex(queue->mutex); LeaveCriticalSection(&queue->lock);
return obj; return obj;
} }
@ -243,7 +243,7 @@ wQueue* Queue_New(BOOL synchronized, int capacity, int growthFactor)
queue->array = (void**) malloc(sizeof(void*) * queue->capacity); queue->array = (void**) malloc(sizeof(void*) * queue->capacity);
ZeroMemory(queue->array, 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); queue->event = CreateEvent(NULL, TRUE, FALSE, NULL);
ZeroMemory(&queue->object, sizeof(wObject)); ZeroMemory(&queue->object, sizeof(wObject));
@ -257,7 +257,7 @@ void Queue_Free(wQueue* queue)
Queue_Clear(queue); Queue_Clear(queue);
CloseHandle(queue->event); CloseHandle(queue->event);
CloseHandle(queue->mutex); DeleteCriticalSection(&queue->lock);
free(queue->array); free(queue->array);
free(queue); free(queue);
} }

View File

@ -88,7 +88,7 @@ UINT32 ReferenceTable_Add(wReferenceTable* referenceTable, void* ptr)
wReference* reference = NULL; wReference* reference = NULL;
if (referenceTable->synchronized) if (referenceTable->synchronized)
WaitForSingleObject(referenceTable->mutex, INFINITE); EnterCriticalSection(&referenceTable->lock);
reference = ReferenceTable_FindEntry(referenceTable, ptr); reference = ReferenceTable_FindEntry(referenceTable, ptr);
@ -102,7 +102,7 @@ UINT32 ReferenceTable_Add(wReferenceTable* referenceTable, void* ptr)
count = ++(reference->Count); count = ++(reference->Count);
if (referenceTable->synchronized) if (referenceTable->synchronized)
ReleaseMutex(referenceTable->mutex); LeaveCriticalSection(&referenceTable->lock);
return count; return count;
} }
@ -113,7 +113,7 @@ UINT32 ReferenceTable_Release(wReferenceTable* referenceTable, void* ptr)
wReference* reference = NULL; wReference* reference = NULL;
if (referenceTable->synchronized) if (referenceTable->synchronized)
WaitForSingleObject(referenceTable->mutex, INFINITE); EnterCriticalSection(&referenceTable->lock);
reference = ReferenceTable_FindEntry(referenceTable, ptr); reference = ReferenceTable_FindEntry(referenceTable, ptr);
@ -133,7 +133,7 @@ UINT32 ReferenceTable_Release(wReferenceTable* referenceTable, void* ptr)
} }
if (referenceTable->synchronized) if (referenceTable->synchronized)
ReleaseMutex(referenceTable->mutex); LeaveCriticalSection(&referenceTable->lock);
return count; return count;
} }
@ -154,7 +154,7 @@ wReferenceTable* ReferenceTable_New(BOOL synchronized, void* context, REFERENCE_
ZeroMemory(referenceTable->array, sizeof(wReference) * referenceTable->size); ZeroMemory(referenceTable->array, sizeof(wReference) * referenceTable->size);
referenceTable->synchronized = synchronized; referenceTable->synchronized = synchronized;
referenceTable->mutex = CreateMutex(NULL, FALSE, NULL); InitializeCriticalSection(&referenceTable->lock);
} }
return referenceTable; return referenceTable;
@ -164,7 +164,7 @@ void ReferenceTable_Free(wReferenceTable* referenceTable)
{ {
if (referenceTable) if (referenceTable)
{ {
CloseHandle(referenceTable->mutex); DeleteCriticalSection(&referenceTable->lock);
free(referenceTable->array); free(referenceTable->array);
free(referenceTable); free(referenceTable);
} }

View File

@ -84,7 +84,7 @@ BOOL Stack_Contains(wStack* stack, void* obj)
void Stack_Push(wStack* stack, void* obj) void Stack_Push(wStack* stack, void* obj)
{ {
if (stack->synchronized) if (stack->synchronized)
WaitForSingleObject(stack->mutex, INFINITE); EnterCriticalSection(&stack->lock);
if ((stack->size + 1) >= stack->capacity) if ((stack->size + 1) >= stack->capacity)
{ {
@ -95,7 +95,7 @@ void Stack_Push(wStack* stack, void* obj)
stack->array[(stack->size)++] = obj; stack->array[(stack->size)++] = obj;
if (stack->synchronized) if (stack->synchronized)
ReleaseMutex(stack->mutex); LeaveCriticalSection(&stack->lock);
} }
/** /**
@ -107,13 +107,13 @@ void* Stack_Pop(wStack* stack)
void* obj = NULL; void* obj = NULL;
if (stack->synchronized) if (stack->synchronized)
WaitForSingleObject(stack->mutex, INFINITE); EnterCriticalSection(&stack->lock);
if (stack->size > 0) if (stack->size > 0)
obj = stack->array[--(stack->size)]; obj = stack->array[--(stack->size)];
if (stack->synchronized) if (stack->synchronized)
ReleaseMutex(stack->mutex); LeaveCriticalSection(&stack->lock);
return obj; return obj;
} }
@ -127,13 +127,13 @@ void* Stack_Peek(wStack* stack)
void* obj = NULL; void* obj = NULL;
if (stack->synchronized) if (stack->synchronized)
WaitForSingleObject(stack->mutex, INFINITE); EnterCriticalSection(&stack->lock);
if (stack->size > 0) if (stack->size > 0)
obj = stack->array[stack->size]; obj = stack->array[stack->size];
if (stack->synchronized) if (stack->synchronized)
ReleaseMutex(stack->mutex); LeaveCriticalSection(&stack->lock);
return obj; return obj;
} }
@ -153,7 +153,7 @@ wStack* Stack_New(BOOL synchronized)
stack->synchronized = synchronized; stack->synchronized = synchronized;
if (stack->synchronized) if (stack->synchronized)
stack->mutex = CreateMutex(NULL, FALSE, NULL); InitializeCriticalSection(&stack->lock);
stack->size = 0; stack->size = 0;
stack->capacity = 32; stack->capacity = 32;
@ -168,7 +168,7 @@ void Stack_Free(wStack* stack)
if (stack) if (stack)
{ {
if (stack->synchronized) if (stack->synchronized)
CloseHandle(stack->mutex); DeleteCriticalSection(&stack->lock);
free(stack->array); free(stack->array);

View File

@ -118,7 +118,7 @@ wStream* StreamPool_Take(wStreamPool* pool, size_t size)
BOOL found = FALSE; BOOL found = FALSE;
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
if (size == 0) if (size == 0)
size = pool->defaultSize; size = pool->defaultSize;
@ -153,7 +153,7 @@ wStream* StreamPool_Take(wStreamPool* pool, size_t size)
StreamPool_AddUsed(pool, s); StreamPool_AddUsed(pool, s);
if (pool->synchronized) if (pool->synchronized)
ReleaseMutex(pool->mutex); LeaveCriticalSection(&pool->lock);
return s; return s;
} }
@ -165,7 +165,7 @@ wStream* StreamPool_Take(wStreamPool* pool, size_t size)
void StreamPool_Return(wStreamPool* pool, wStream* s) void StreamPool_Return(wStreamPool* pool, wStream* s)
{ {
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
if ((pool->aSize + 1) >= pool->aCapacity) if ((pool->aSize + 1) >= pool->aCapacity)
{ {
@ -177,7 +177,7 @@ void StreamPool_Return(wStreamPool* pool, wStream* s)
StreamPool_RemoveUsed(pool, s); StreamPool_RemoveUsed(pool, s);
if (pool->synchronized) 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) 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) 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; wStream* s = NULL;
BOOL found = FALSE; BOOL found = FALSE;
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
for (index = 0; index < pool->uSize; index++) 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; return (found) ? s : NULL;
} }
@ -294,7 +294,7 @@ void StreamPool_Release(wStreamPool* pool, BYTE* ptr)
void StreamPool_Clear(wStreamPool* pool) void StreamPool_Clear(wStreamPool* pool)
{ {
if (pool->synchronized) if (pool->synchronized)
WaitForSingleObject(pool->mutex, INFINITE); EnterCriticalSection(&pool->lock);
while (pool->aSize > 0) while (pool->aSize > 0)
{ {
@ -303,7 +303,7 @@ void StreamPool_Clear(wStreamPool* pool)
} }
if (pool->synchronized) 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->synchronized = synchronized;
pool->defaultSize = defaultSize; pool->defaultSize = defaultSize;
if (pool->synchronized) InitializeCriticalSection(&pool->lock);
pool->mutex = CreateMutex(NULL, FALSE, NULL);
pool->aSize = 0; pool->aSize = 0;
pool->aCapacity = 32; pool->aCapacity = 32;
@ -344,8 +343,7 @@ void StreamPool_Free(wStreamPool* pool)
{ {
StreamPool_Clear(pool); StreamPool_Clear(pool);
if (pool->synchronized) DeleteCriticalSection(&pool->lock);
CloseHandle(pool->mutex);
free(pool->aArray); free(pool->aArray);
free(pool->uArray); free(pool->uArray);