FreeRDP/winpr/libwinpr/utils/collections/MessageQueue.c
Norbert Federa 3a58934eb2 libwinpr-utils: Use criticalsection with spincount
Use InitializeCriticalSectionAndSpinCount instead of IntializeCriticalSection.
Using spin counts for critical sections of short duration enables the calling
thread to avoid the wait operation in most situations which can dramatically
improve the overall performance on multiprocessor systems.

On Linux this change has no effect because the new winpr critical section
implementation does not use the SpinCount field under Linux because the NPTL
synchronization primitives are implemented using the extremely performant
futex system calls which have this magic already built in.

However, on Mac OS X this change improved the overall performance of the
multithreaded RemoteFX decoder by 25 percent.

I've used a SpinCount of 4000 which avoided 99 percent of the wait calls.
This value is also used by Microsoft's heap manager for its per-heap
critical sections.

Note: This change requires pull request #1397 to be merged.
2013-08-07 18:16:01 +02:00

212 lines
4.4 KiB
C

/**
* WinPR: Windows Portable Runtime
* Message Queue
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <winpr/crt.h>
#include <winpr/sysinfo.h>
#include <winpr/collections.h>
/**
* Message Queue inspired from Windows:
* http://msdn.microsoft.com/en-us/library/ms632590/
*/
/**
* Properties
*/
/**
* Gets an event which is set when the queue is non-empty
*/
HANDLE MessageQueue_Event(wMessageQueue* queue)
{
return queue->event;
}
/**
* Gets the queue size
*/
int MessageQueue_Size(wMessageQueue* queue)
{
return queue->size;
}
/**
* Methods
*/
BOOL MessageQueue_Wait(wMessageQueue* queue)
{
BOOL status = FALSE;
if (WaitForSingleObject(queue->event, INFINITE) == WAIT_OBJECT_0)
status = TRUE;
return status;
}
void MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message)
{
EnterCriticalSection(&queue->lock);
if (queue->size == queue->capacity)
{
int old_capacity;
int new_capacity;
old_capacity = queue->capacity;
new_capacity = queue->capacity * 2;
queue->capacity = new_capacity;
queue->array = (wMessage*) realloc(queue->array, sizeof(wMessage) * queue->capacity);
ZeroMemory(&(queue->array[old_capacity]), old_capacity * sizeof(wMessage));
if (queue->tail < old_capacity)
{
CopyMemory(&(queue->array[old_capacity]), queue->array, queue->tail * sizeof(wMessage));
queue->tail += old_capacity;
}
}
CopyMemory(&(queue->array[queue->tail]), message, sizeof(wMessage));
queue->tail = (queue->tail + 1) % queue->capacity;
queue->size++;
message = &(queue->array[queue->tail]);
message->time = (UINT64) GetTickCount();
if (queue->size > 0)
SetEvent(queue->event);
LeaveCriticalSection(&queue->lock);
}
void MessageQueue_Post(wMessageQueue* queue, void* context, UINT32 type, void* wParam, void* lParam)
{
wMessage message;
message.context = context;
message.id = type;
message.wParam = wParam;
message.lParam = lParam;
MessageQueue_Dispatch(queue, &message);
}
void MessageQueue_PostQuit(wMessageQueue* queue, int nExitCode)
{
MessageQueue_Post(queue, NULL, WMQ_QUIT, (void*) (size_t) nExitCode, NULL);
}
int MessageQueue_Get(wMessageQueue* queue, wMessage* message)
{
int status = -1;
if (!MessageQueue_Wait(queue))
return status;
EnterCriticalSection(&queue->lock);
if (queue->size > 0)
{
CopyMemory(message, &(queue->array[queue->head]), sizeof(wMessage));
ZeroMemory(&(queue->array[queue->head]), sizeof(wMessage));
queue->head = (queue->head + 1) % queue->capacity;
queue->size--;
if (queue->size < 1)
ResetEvent(queue->event);
status = (message->id != WMQ_QUIT) ? 1 : 0;
}
LeaveCriticalSection(&queue->lock);
return status;
}
int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove)
{
int status = 0;
EnterCriticalSection(&queue->lock);
if (queue->size > 0)
{
CopyMemory(message, &(queue->array[queue->head]), sizeof(wMessage));
status = 1;
if (remove)
{
ZeroMemory(&(queue->array[queue->head]), sizeof(wMessage));
queue->head = (queue->head + 1) % queue->capacity;
queue->size--;
if (queue->size < 1)
ResetEvent(queue->event);
}
}
LeaveCriticalSection(&queue->lock);
return status;
}
/**
* Construction, Destruction
*/
wMessageQueue* MessageQueue_New()
{
wMessageQueue* queue = NULL;
queue = (wMessageQueue*) malloc(sizeof(wMessageQueue));
if (queue)
{
queue->head = 0;
queue->tail = 0;
queue->size = 0;
queue->capacity = 32;
queue->array = (wMessage*) malloc(sizeof(wMessage) * queue->capacity);
ZeroMemory(queue->array, sizeof(wMessage) * queue->capacity);
InitializeCriticalSectionAndSpinCount(&queue->lock, 4000);
queue->event = CreateEvent(NULL, TRUE, FALSE, NULL);
}
return queue;
}
void MessageQueue_Free(wMessageQueue* queue)
{
CloseHandle(queue->event);
DeleteCriticalSection(&queue->lock);
free(queue->array);
free(queue);
}