2013-01-24 23:09:44 +04:00
|
|
|
/**
|
|
|
|
* 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/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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods
|
|
|
|
*/
|
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
BOOL MessageQueue_Wait(wMessageQueue* queue)
|
2013-01-24 23:09:44 +04:00
|
|
|
{
|
2013-01-25 00:08:49 +04:00
|
|
|
BOOL status = FALSE;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
if (WaitForSingleObject(queue->event, INFINITE) == WAIT_OBJECT_0)
|
|
|
|
status = TRUE;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
return status;
|
2013-01-24 23:09:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(queue->mutex, INFINITE);
|
|
|
|
|
|
|
|
if (queue->size == queue->capacity)
|
|
|
|
{
|
|
|
|
int old_capacity;
|
|
|
|
int new_capacity;
|
|
|
|
|
|
|
|
old_capacity = queue->capacity;
|
|
|
|
new_capacity = queue->capacity * 2;
|
|
|
|
|
|
|
|
queue->capacity = new_capacity;
|
2013-01-25 00:08:49 +04:00
|
|
|
queue->array = (wMessage*) realloc(queue->array, sizeof(wMessage) * queue->capacity);
|
|
|
|
ZeroMemory(&(queue->array[old_capacity]), old_capacity * sizeof(wMessage));
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
if (queue->tail < (old_capacity - 1))
|
|
|
|
{
|
2013-01-25 00:08:49 +04:00
|
|
|
CopyMemory(&(queue->array[old_capacity]), queue->array, queue->tail * sizeof(wMessage));
|
2013-01-24 23:09:44 +04:00
|
|
|
queue->tail += old_capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
CopyMemory(&(queue->array[queue->tail]), message, sizeof(wMessage));
|
2013-01-24 23:09:44 +04:00
|
|
|
queue->tail = (queue->tail + 1) % queue->capacity;
|
|
|
|
queue->size++;
|
|
|
|
|
|
|
|
SetEvent(queue->event);
|
|
|
|
|
|
|
|
ReleaseMutex(queue->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue_Post(wMessageQueue* queue, void* context, UINT32 type, void* wParam, void* lParam)
|
|
|
|
{
|
2013-01-25 00:08:49 +04:00
|
|
|
wMessage message;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
message.context = context;
|
|
|
|
message.type = type;
|
|
|
|
message.wParam = wParam;
|
|
|
|
message.lParam = lParam;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
MessageQueue_Dispatch(queue, &message);
|
|
|
|
}
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
void MessageQueue_PostQuit(wMessageQueue* queue, int nExitCode)
|
|
|
|
{
|
|
|
|
MessageQueue_Post(queue, NULL, WMQ_QUIT, (void*) (size_t) nExitCode, NULL);
|
2013-01-24 23:09:44 +04:00
|
|
|
}
|
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
int MessageQueue_Get(wMessageQueue* queue, wMessage* message)
|
2013-01-24 23:09:44 +04:00
|
|
|
{
|
2013-01-25 00:08:49 +04:00
|
|
|
int status = -1;
|
|
|
|
|
|
|
|
if (!MessageQueue_Wait(queue))
|
|
|
|
return status;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
WaitForSingleObject(queue->mutex, INFINITE);
|
|
|
|
|
|
|
|
if (queue->size > 0)
|
|
|
|
{
|
2013-01-25 00:08:49 +04:00
|
|
|
CopyMemory(message, &(queue->array[queue->head]), sizeof(wMessage));
|
|
|
|
ZeroMemory(&(queue->array[queue->head]), sizeof(wMessage));
|
2013-01-24 23:09:44 +04:00
|
|
|
queue->head = (queue->head + 1) % queue->capacity;
|
|
|
|
queue->size--;
|
2013-01-25 00:08:49 +04:00
|
|
|
|
|
|
|
status = (message->type != WMQ_QUIT) ? 1 : 0;
|
2013-01-24 23:09:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (queue->size < 1)
|
|
|
|
ResetEvent(queue->event);
|
|
|
|
|
|
|
|
ReleaseMutex(queue->mutex);
|
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
return status;
|
2013-01-24 23:09:44 +04:00
|
|
|
}
|
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove)
|
2013-01-24 23:09:44 +04:00
|
|
|
{
|
2013-01-25 00:08:49 +04:00
|
|
|
int status = 0;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
WaitForSingleObject(queue->mutex, INFINITE);
|
|
|
|
|
|
|
|
if (queue->size > 0)
|
2013-01-25 00:08:49 +04:00
|
|
|
{
|
|
|
|
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--;
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
ReleaseMutex(queue->mutex);
|
|
|
|
|
2013-01-25 00:08:49 +04:00
|
|
|
return status;
|
2013-01-24 23:09:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2013-01-25 00:08:49 +04:00
|
|
|
queue->array = (wMessage*) malloc(sizeof(wMessage) * queue->capacity);
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
queue->mutex = CreateMutex(NULL, FALSE, NULL);
|
|
|
|
queue->event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue_Free(wMessageQueue* queue)
|
|
|
|
{
|
|
|
|
CloseHandle(queue->event);
|
|
|
|
CloseHandle(queue->mutex);
|
|
|
|
|
|
|
|
free(queue->array);
|
|
|
|
free(queue);
|
|
|
|
}
|