1 /**
2  * WinPR: Windows Portable Runtime
3  * Message Queue
4  *
5  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <winpr/crt.h>
25 #include <winpr/sysinfo.h>
26 
27 #include <winpr/collections.h>
28 
29 /**
30  * Message Queue inspired from Windows:
31  * http://msdn.microsoft.com/en-us/library/ms632590/
32  */
33 
34 /**
35  * Properties
36  */
37 
38 /**
39  * Gets an event which is set when the queue is non-empty
40  */
41 
MessageQueue_Event(wMessageQueue * queue)42 HANDLE MessageQueue_Event(wMessageQueue* queue)
43 {
44 	return queue->event;
45 }
46 
47 /**
48  * Gets the queue size
49  */
50 
MessageQueue_Size(wMessageQueue * queue)51 int MessageQueue_Size(wMessageQueue* queue)
52 {
53 	return queue->size;
54 }
55 
56 /**
57  * Methods
58  */
59 
MessageQueue_Wait(wMessageQueue * queue)60 BOOL MessageQueue_Wait(wMessageQueue* queue)
61 {
62 	BOOL status = FALSE;
63 
64 	if (WaitForSingleObject(queue->event, INFINITE) == WAIT_OBJECT_0)
65 		status = TRUE;
66 
67 	return status;
68 }
69 
MessageQueue_Dispatch(wMessageQueue * queue,wMessage * message)70 BOOL MessageQueue_Dispatch(wMessageQueue* queue, wMessage* message)
71 {
72 	BOOL ret = FALSE;
73 	if (!queue || !message)
74 		return FALSE;
75 
76 	EnterCriticalSection(&queue->lock);
77 
78 	if (queue->size == queue->capacity)
79 	{
80 		int old_capacity;
81 		int new_capacity;
82 		wMessage* new_arr;
83 
84 		old_capacity = queue->capacity;
85 		new_capacity = queue->capacity * 2;
86 
87 		new_arr = (wMessage*)realloc(queue->array, sizeof(wMessage) * new_capacity);
88 		if (!new_arr)
89 			goto out;
90 		queue->array = new_arr;
91 		queue->capacity = new_capacity;
92 		ZeroMemory(&(queue->array[old_capacity]), (new_capacity - old_capacity) * sizeof(wMessage));
93 
94 		/* rearrange wrapped entries */
95 		if (queue->tail <= queue->head)
96 		{
97 			CopyMemory(&(queue->array[old_capacity]), queue->array, queue->tail * sizeof(wMessage));
98 			queue->tail += old_capacity;
99 		}
100 	}
101 
102 	CopyMemory(&(queue->array[queue->tail]), message, sizeof(wMessage));
103 
104 	message = &(queue->array[queue->tail]);
105 	message->time = GetTickCount64();
106 
107 	queue->tail = (queue->tail + 1) % queue->capacity;
108 	queue->size++;
109 
110 	if (queue->size > 0)
111 		SetEvent(queue->event);
112 
113 	ret = TRUE;
114 out:
115 	LeaveCriticalSection(&queue->lock);
116 	return ret;
117 }
118 
MessageQueue_Post(wMessageQueue * queue,void * context,UINT32 type,void * wParam,void * lParam)119 BOOL MessageQueue_Post(wMessageQueue* queue, void* context, UINT32 type, void* wParam, void* lParam)
120 {
121 	wMessage message;
122 
123 	message.context = context;
124 	message.id = type;
125 	message.wParam = wParam;
126 	message.lParam = lParam;
127 	message.Free = NULL;
128 
129 	return MessageQueue_Dispatch(queue, &message);
130 }
131 
MessageQueue_PostQuit(wMessageQueue * queue,int nExitCode)132 BOOL MessageQueue_PostQuit(wMessageQueue* queue, int nExitCode)
133 {
134 	return MessageQueue_Post(queue, NULL, WMQ_QUIT, (void*)(size_t)nExitCode, NULL);
135 }
136 
MessageQueue_Get(wMessageQueue * queue,wMessage * message)137 int MessageQueue_Get(wMessageQueue* queue, wMessage* message)
138 {
139 	int status = -1;
140 
141 	if (!MessageQueue_Wait(queue))
142 		return status;
143 
144 	EnterCriticalSection(&queue->lock);
145 
146 	if (queue->size > 0)
147 	{
148 		CopyMemory(message, &(queue->array[queue->head]), sizeof(wMessage));
149 		ZeroMemory(&(queue->array[queue->head]), sizeof(wMessage));
150 		queue->head = (queue->head + 1) % queue->capacity;
151 		queue->size--;
152 
153 		if (queue->size < 1)
154 			ResetEvent(queue->event);
155 
156 		status = (message->id != WMQ_QUIT) ? 1 : 0;
157 	}
158 
159 	LeaveCriticalSection(&queue->lock);
160 
161 	return status;
162 }
163 
MessageQueue_Peek(wMessageQueue * queue,wMessage * message,BOOL remove)164 int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove)
165 {
166 	int status = 0;
167 
168 	EnterCriticalSection(&queue->lock);
169 
170 	if (queue->size > 0)
171 	{
172 		CopyMemory(message, &(queue->array[queue->head]), sizeof(wMessage));
173 		status = 1;
174 
175 		if (remove)
176 		{
177 			ZeroMemory(&(queue->array[queue->head]), sizeof(wMessage));
178 			queue->head = (queue->head + 1) % queue->capacity;
179 			queue->size--;
180 
181 			if (queue->size < 1)
182 				ResetEvent(queue->event);
183 		}
184 	}
185 
186 	LeaveCriticalSection(&queue->lock);
187 
188 	return status;
189 }
190 
191 /**
192  * Construction, Destruction
193  */
194 
MessageQueue_New(const wObject * callback)195 wMessageQueue* MessageQueue_New(const wObject* callback)
196 {
197 	wMessageQueue* queue = NULL;
198 
199 	queue = (wMessageQueue*)calloc(1, sizeof(wMessageQueue));
200 	if (!queue)
201 		return NULL;
202 
203 	queue->capacity = 32;
204 	queue->array = (wMessage*)calloc(queue->capacity, sizeof(wMessage));
205 	if (!queue->array)
206 		goto error_array;
207 
208 	if (!InitializeCriticalSectionAndSpinCount(&queue->lock, 4000))
209 		goto error_spinlock;
210 
211 	queue->event = CreateEvent(NULL, TRUE, FALSE, NULL);
212 	if (!queue->event)
213 		goto error_event;
214 
215 	if (callback)
216 		queue->object = *callback;
217 
218 	return queue;
219 
220 error_event:
221 	DeleteCriticalSection(&queue->lock);
222 error_spinlock:
223 	free(queue->array);
224 error_array:
225 	free(queue);
226 	return NULL;
227 }
228 
MessageQueue_Free(wMessageQueue * queue)229 void MessageQueue_Free(wMessageQueue* queue)
230 {
231 	if (!queue)
232 		return;
233 
234 	MessageQueue_Clear(queue);
235 
236 	CloseHandle(queue->event);
237 	DeleteCriticalSection(&queue->lock);
238 
239 	free(queue->array);
240 	free(queue);
241 }
242 
MessageQueue_Clear(wMessageQueue * queue)243 int MessageQueue_Clear(wMessageQueue* queue)
244 {
245 	int status = 0;
246 
247 	EnterCriticalSection(&queue->lock);
248 
249 	while (queue->size > 0)
250 	{
251 		wMessage* msg = &(queue->array[queue->head]);
252 
253 		/* Free resources of message. */
254 		if (queue->object.fnObjectUninit)
255 			queue->object.fnObjectUninit(msg);
256 		if (queue->object.fnObjectFree)
257 			queue->object.fnObjectFree(msg);
258 
259 		ZeroMemory(msg, sizeof(wMessage));
260 
261 		queue->head = (queue->head + 1) % queue->capacity;
262 		queue->size--;
263 	}
264 	ResetEvent(queue->event);
265 
266 	LeaveCriticalSection(&queue->lock);
267 
268 	return status;
269 }
270