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>
|
2013-02-20 07:36:04 +04:00
|
|
|
#include <winpr/sysinfo.h>
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2013-02-10 02:13:53 +04:00
|
|
|
/**
|
|
|
|
* Gets the queue size
|
|
|
|
*/
|
|
|
|
|
|
|
|
int MessageQueue_Size(wMessageQueue* queue)
|
|
|
|
{
|
|
|
|
return queue->size;
|
|
|
|
}
|
|
|
|
|
2013-01-24 23:09:44 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
BOOL MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message)
|
2013-01-24 23:09:44 +04:00
|
|
|
{
|
2015-05-23 23:47:18 +03:00
|
|
|
BOOL ret = FALSE;
|
2019-10-25 14:19:42 +03:00
|
|
|
if (!queue || !message)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
if (queue->size == queue->capacity)
|
|
|
|
{
|
|
|
|
int old_capacity;
|
|
|
|
int new_capacity;
|
2015-03-11 10:04:38 +03:00
|
|
|
wMessage* new_arr;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
old_capacity = queue->capacity;
|
|
|
|
new_capacity = queue->capacity * 2;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
new_arr = (wMessage*)realloc(queue->array, sizeof(wMessage) * new_capacity);
|
2015-03-11 10:04:38 +03:00
|
|
|
if (!new_arr)
|
2015-05-23 23:47:18 +03:00
|
|
|
goto out;
|
2015-03-11 09:59:56 +03:00
|
|
|
queue->array = new_arr;
|
2013-01-24 23:09:44 +04:00
|
|
|
queue->capacity = new_capacity;
|
2017-02-15 06:44:19 +03:00
|
|
|
ZeroMemory(&(queue->array[old_capacity]), (new_capacity - old_capacity) * sizeof(wMessage));
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2017-02-15 06:44:19 +03:00
|
|
|
/* rearrange wrapped entries */
|
|
|
|
if (queue->tail <= queue->head)
|
2013-01-24 23:09:44 +04:00
|
|
|
{
|
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
|
|
|
|
2013-02-20 07:36:04 +04:00
|
|
|
message = &(queue->array[queue->tail]);
|
2019-09-02 16:44:52 +03:00
|
|
|
message->time = GetTickCount64();
|
2013-02-20 07:36:04 +04:00
|
|
|
|
2020-06-08 21:17:35 +03:00
|
|
|
queue->tail = (queue->tail + 1) % queue->capacity;
|
|
|
|
queue->size++;
|
|
|
|
|
2013-02-12 05:38:19 +04:00
|
|
|
if (queue->size > 0)
|
2013-02-10 02:13:53 +04:00
|
|
|
SetEvent(queue->event);
|
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
ret = TRUE;
|
|
|
|
out:
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
2015-05-23 23:47:18 +03:00
|
|
|
return ret;
|
2013-01-24 23:09:44 +04:00
|
|
|
}
|
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
BOOL MessageQueue_Post(wMessageQueue* queue, void* context, UINT32 type, void* wParam, void* lParam)
|
2013-01-24 23:09:44 +04:00
|
|
|
{
|
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;
|
2013-02-04 23:56:54 +04:00
|
|
|
message.id = type;
|
2013-01-25 00:08:49 +04:00
|
|
|
message.wParam = wParam;
|
|
|
|
message.lParam = lParam;
|
2014-09-19 06:18:58 +04:00
|
|
|
message.Free = NULL;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
return MessageQueue_Dispatch(queue, &message);
|
2013-01-25 00:08:49 +04:00
|
|
|
}
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
BOOL MessageQueue_PostQuit(wMessageQueue* queue, int nExitCode)
|
2013-01-25 00:08:49 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return 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
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
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;
|
2013-02-12 05:38:19 +04:00
|
|
|
queue->size--;
|
2013-02-10 02:13:53 +04:00
|
|
|
|
2013-02-12 05:38:19 +04:00
|
|
|
if (queue->size < 1)
|
2013-02-10 02:13:53 +04:00
|
|
|
ResetEvent(queue->event);
|
|
|
|
|
2013-02-04 23:56:54 +04:00
|
|
|
status = (message->id != WMQ_QUIT) ? 1 : 0;
|
2013-01-24 23:09:44 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&queue->lock);
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
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;
|
2013-02-12 05:38:19 +04:00
|
|
|
queue->size--;
|
2013-02-10 02:13:53 +04:00
|
|
|
|
2013-02-12 05:38:19 +04:00
|
|
|
if (queue->size < 1)
|
2013-02-10 02:13:53 +04:00
|
|
|
ResetEvent(queue->event);
|
2013-01-25 00:08:49 +04:00
|
|
|
}
|
|
|
|
}
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&queue->lock);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construction, Destruction
|
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
wMessageQueue* MessageQueue_New(const wObject* callback)
|
2013-01-24 23:09:44 +04:00
|
|
|
{
|
|
|
|
wMessageQueue* queue = NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
queue = (wMessageQueue*)calloc(1, sizeof(wMessageQueue));
|
2015-05-23 23:47:18 +03:00
|
|
|
if (!queue)
|
|
|
|
return NULL;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
queue->capacity = 32;
|
2019-11-06 17:24:51 +03:00
|
|
|
queue->array = (wMessage*)calloc(queue->capacity, sizeof(wMessage));
|
2015-05-23 23:47:18 +03:00
|
|
|
if (!queue->array)
|
|
|
|
goto error_array;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&queue->lock, 4000))
|
|
|
|
goto error_spinlock;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
queue->event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!queue->event)
|
|
|
|
goto error_event;
|
2013-11-12 18:10:41 +04:00
|
|
|
|
2015-05-23 23:47:18 +03:00
|
|
|
if (callback)
|
|
|
|
queue->object = *callback;
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
return queue;
|
2015-05-23 23:47:18 +03:00
|
|
|
|
|
|
|
error_event:
|
|
|
|
DeleteCriticalSection(&queue->lock);
|
|
|
|
error_spinlock:
|
|
|
|
free(queue->array);
|
|
|
|
error_array:
|
|
|
|
free(queue);
|
|
|
|
return NULL;
|
2013-01-24 23:09:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue_Free(wMessageQueue* queue)
|
|
|
|
{
|
2015-07-30 17:02:36 +03:00
|
|
|
if (!queue)
|
|
|
|
return;
|
|
|
|
|
2018-02-09 12:29:18 +03:00
|
|
|
MessageQueue_Clear(queue);
|
|
|
|
|
2013-01-24 23:09:44 +04:00
|
|
|
CloseHandle(queue->event);
|
2013-08-02 14:07:05 +04:00
|
|
|
DeleteCriticalSection(&queue->lock);
|
2013-01-24 23:09:44 +04:00
|
|
|
|
|
|
|
free(queue->array);
|
|
|
|
free(queue);
|
|
|
|
}
|
2013-11-12 18:10:41 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
int MessageQueue_Clear(wMessageQueue* queue)
|
2013-11-12 18:10:41 +04:00
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
EnterCriticalSection(&queue->lock);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
while (queue->size > 0)
|
2013-11-12 18:10:41 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
wMessage* msg = &(queue->array[queue->head]);
|
2013-11-12 18:10:41 +04:00
|
|
|
|
|
|
|
/* Free resources of message. */
|
|
|
|
if (queue->object.fnObjectUninit)
|
|
|
|
queue->object.fnObjectUninit(msg);
|
|
|
|
if (queue->object.fnObjectFree)
|
|
|
|
queue->object.fnObjectFree(msg);
|
|
|
|
|
|
|
|
ZeroMemory(msg, sizeof(wMessage));
|
|
|
|
|
|
|
|
queue->head = (queue->head + 1) % queue->capacity;
|
|
|
|
queue->size--;
|
|
|
|
}
|
|
|
|
ResetEvent(queue->event);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&queue->lock);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|