Coverage Report

Created: 2026-11-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/utils/collections/MessageQueue.c
Line
Count
Source
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
#include <winpr/config.h>
21
22
#include <winpr/crt.h>
23
#include <winpr/sysinfo.h>
24
#include <winpr/assert.h>
25
26
#include <winpr/collections.h>
27
28
struct s_wMessageQueue
29
{
30
  size_t head;
31
  size_t tail;
32
  size_t size;
33
  size_t capacity;
34
  BOOL closed;
35
  wMessage* array;
36
  CRITICAL_SECTION lock;
37
  HANDLE event;
38
39
  wObject object;
40
};
41
42
/**
43
 * Message Queue inspired from Windows:
44
 * http://msdn.microsoft.com/en-us/library/ms632590/
45
 */
46
47
/**
48
 * Properties
49
 */
50
51
wObject* MessageQueue_Object(wMessageQueue* queue)
52
19.8k
{
53
19.8k
  WINPR_ASSERT(queue);
54
19.8k
  return &queue->object;
55
19.8k
}
56
57
/**
58
 * Gets an event which is set when the queue is non-empty
59
 */
60
61
HANDLE MessageQueue_Event(wMessageQueue* queue)
62
158
{
63
158
  WINPR_ASSERT(queue);
64
158
  return queue->event;
65
158
}
66
67
/**
68
 * Gets the queue size
69
 */
70
71
size_t MessageQueue_Size(wMessageQueue* queue)
72
0
{
73
0
  WINPR_ASSERT(queue);
74
0
  EnterCriticalSection(&queue->lock);
75
0
  const size_t ret = queue->size;
76
0
  LeaveCriticalSection(&queue->lock);
77
0
  return ret;
78
0
}
79
80
size_t MessageQueue_Capacity(wMessageQueue* queue)
81
0
{
82
0
  WINPR_ASSERT(queue);
83
0
  EnterCriticalSection(&queue->lock);
84
0
  const size_t ret = queue->capacity;
85
0
  LeaveCriticalSection(&queue->lock);
86
0
  return ret;
87
0
}
88
89
/**
90
 * Methods
91
 */
92
93
BOOL MessageQueue_Wait(wMessageQueue* queue)
94
0
{
95
0
  BOOL status = FALSE;
96
97
0
  WINPR_ASSERT(queue);
98
0
  if (WaitForSingleObject(queue->event, INFINITE) == WAIT_OBJECT_0)
99
0
    status = TRUE;
100
101
0
  return status;
102
0
}
103
104
static BOOL MessageQueue_EnsureCapacity(wMessageQueue* queue, size_t count)
105
71.6k
{
106
71.6k
  BOOL res = TRUE;
107
71.6k
  const size_t increment = 128;
108
71.6k
  WINPR_ASSERT(queue);
109
110
71.6k
  const size_t required = queue->size + count;
111
  // check for overflow
112
71.6k
  if ((required < queue->size) || (required < count) ||
113
71.6k
      (required > (SIZE_MAX - increment) / sizeof(wMessage)))
114
0
    return FALSE;
115
116
71.6k
  if (required > queue->capacity)
117
69.3k
  {
118
69.3k
    const size_t old_capacity = queue->capacity;
119
69.3k
    const size_t new_capacity = required + increment;
120
121
69.3k
    wMessage* new_arr = (wMessage*)realloc(queue->array, sizeof(wMessage) * new_capacity);
122
69.3k
    if (!new_arr)
123
0
      return FALSE;
124
69.3k
    queue->array = new_arr;
125
69.3k
    queue->capacity = new_capacity;
126
69.3k
    ZeroMemory(&(queue->array[old_capacity]), (new_capacity - old_capacity) * sizeof(wMessage));
127
128
    /* rearrange wrapped entries:
129
     * fill up the newly available space and move tail
130
     * back by the amount of elements that have been moved to the newly
131
     * allocated space.
132
     */
133
69.3k
    if (queue->tail <= queue->head)
134
69.3k
    {
135
69.3k
      size_t tocopy = queue->tail;
136
69.3k
      size_t slots = new_capacity - old_capacity;
137
69.3k
      const size_t batch = (tocopy < slots) ? tocopy : slots;
138
69.3k
      CopyMemory(&(queue->array[old_capacity]), queue->array, batch * sizeof(wMessage));
139
140
      /* Tail is decremented. if the whole thing is appended
141
       * just move the existing tail by old_capacity */
142
69.3k
      if (tocopy < slots)
143
69.3k
      {
144
69.3k
        ZeroMemory(queue->array, batch * sizeof(wMessage));
145
69.3k
        queue->tail += old_capacity;
146
69.3k
      }
147
0
      else
148
0
      {
149
0
        const size_t remain = queue->tail - batch;
150
0
        const size_t movesize = remain * sizeof(wMessage);
151
0
        res = memmove_s(queue->array, queue->tail * sizeof(wMessage), &queue->array[batch],
152
0
                        movesize) >= 0;
153
154
0
        const size_t zerooffset = remain;
155
0
        const size_t zerosize = (queue->tail - remain) * sizeof(wMessage);
156
0
        ZeroMemory(&queue->array[zerooffset], zerosize);
157
0
        queue->tail -= batch;
158
0
      }
159
69.3k
    }
160
69.3k
  }
161
162
71.6k
  return res;
163
71.6k
}
164
165
BOOL MessageQueue_Dispatch(wMessageQueue* queue, const wMessage* message)
166
2.27k
{
167
2.27k
  wMessage* dst = nullptr;
168
2.27k
  BOOL ret = FALSE;
169
2.27k
  WINPR_ASSERT(queue);
170
171
2.27k
  if (!message)
172
0
    return FALSE;
173
174
2.27k
  WINPR_ASSERT(queue);
175
2.27k
  EnterCriticalSection(&queue->lock);
176
177
2.27k
  if (queue->closed)
178
0
    goto out;
179
180
2.27k
  if (!MessageQueue_EnsureCapacity(queue, 1))
181
0
    goto out;
182
183
2.27k
  dst = &(queue->array[queue->tail]);
184
2.27k
  *dst = *message;
185
2.27k
  dst->time = GetTickCount64();
186
187
2.27k
  queue->tail = (queue->tail + 1) % queue->capacity;
188
2.27k
  queue->size++;
189
190
2.27k
  if (queue->size > 0)
191
2.27k
    (void)SetEvent(queue->event);
192
193
2.27k
  if (message->id == WMQ_QUIT)
194
2.27k
    queue->closed = TRUE;
195
196
2.27k
  ret = TRUE;
197
2.27k
out:
198
2.27k
  LeaveCriticalSection(&queue->lock);
199
2.27k
  return ret;
200
2.27k
}
201
202
BOOL MessageQueue_Post(wMessageQueue* queue, void* context, UINT32 type, void* wParam, void* lParam)
203
2.27k
{
204
2.27k
  wMessage message = WINPR_C_ARRAY_INIT;
205
206
2.27k
  message.context = context;
207
2.27k
  message.id = type;
208
2.27k
  message.wParam = wParam;
209
2.27k
  message.lParam = lParam;
210
2.27k
  message.Free = nullptr;
211
212
2.27k
  return MessageQueue_Dispatch(queue, &message);
213
2.27k
}
214
215
BOOL MessageQueue_PostQuit(wMessageQueue* queue, int nExitCode)
216
2.27k
{
217
2.27k
  return MessageQueue_Post(queue, nullptr, WMQ_QUIT, (void*)(size_t)nExitCode, nullptr);
218
2.27k
}
219
220
int MessageQueue_Get(wMessageQueue* queue, wMessage* message)
221
0
{
222
0
  int status = -1;
223
224
0
  if (!MessageQueue_Wait(queue))
225
0
    return status;
226
227
0
  EnterCriticalSection(&queue->lock);
228
229
0
  if (queue->size > 0)
230
0
  {
231
0
    CopyMemory(message, &(queue->array[queue->head]), sizeof(wMessage));
232
0
    ZeroMemory(&(queue->array[queue->head]), sizeof(wMessage));
233
0
    queue->head = (queue->head + 1) % queue->capacity;
234
0
    queue->size--;
235
236
0
    if (queue->size < 1)
237
0
      (void)ResetEvent(queue->event);
238
239
0
    status = (message->id != WMQ_QUIT) ? 1 : 0;
240
0
  }
241
242
0
  LeaveCriticalSection(&queue->lock);
243
244
0
  return status;
245
0
}
246
247
int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove)
248
158
{
249
158
  int status = 0;
250
251
158
  WINPR_ASSERT(queue);
252
158
  EnterCriticalSection(&queue->lock);
253
254
158
  if (queue->size > 0)
255
79
  {
256
79
    CopyMemory(message, &(queue->array[queue->head]), sizeof(wMessage));
257
79
    status = 1;
258
259
79
    if (remove)
260
79
    {
261
79
      ZeroMemory(&(queue->array[queue->head]), sizeof(wMessage));
262
79
      queue->head = (queue->head + 1) % queue->capacity;
263
79
      queue->size--;
264
265
79
      if (queue->size < 1)
266
79
      {
267
79
        if (!ResetEvent(queue->event))
268
0
          status = -1;
269
79
      }
270
79
    }
271
79
  }
272
273
158
  LeaveCriticalSection(&queue->lock);
274
275
158
  return status;
276
158
}
277
278
/**
279
 * Construction, Destruction
280
 */
281
282
wMessageQueue* MessageQueue_New(const wObject* callback)
283
69.3k
{
284
69.3k
  wMessageQueue* queue = nullptr;
285
286
69.3k
  queue = (wMessageQueue*)calloc(1, sizeof(wMessageQueue));
287
69.3k
  if (!queue)
288
0
    return nullptr;
289
290
69.3k
  if (!InitializeCriticalSectionAndSpinCount(&queue->lock, 4000))
291
0
    goto fail;
292
293
69.3k
  if (!MessageQueue_EnsureCapacity(queue, 32))
294
0
    goto fail;
295
296
69.3k
  queue->event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
297
69.3k
  if (!queue->event)
298
0
    goto fail;
299
300
69.3k
  if (callback)
301
49.5k
    queue->object = *callback;
302
303
69.3k
  return queue;
304
305
0
fail:
306
0
  WINPR_PRAGMA_DIAG_PUSH
307
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
308
0
  MessageQueue_Free(queue);
309
0
  WINPR_PRAGMA_DIAG_POP
310
0
  return nullptr;
311
69.3k
}
312
313
void MessageQueue_Free(wMessageQueue* queue)
314
69.2k
{
315
69.2k
  if (!queue)
316
0
    return;
317
318
69.2k
  if (queue->event)
319
69.2k
    MessageQueue_Clear(queue);
320
321
69.2k
  (void)CloseHandle(queue->event);
322
69.2k
  DeleteCriticalSection(&queue->lock);
323
324
69.2k
  free(queue->array);
325
69.2k
  free(queue);
326
69.2k
}
327
328
int MessageQueue_Clear(wMessageQueue* queue)
329
71.4k
{
330
71.4k
  int status = 0;
331
332
71.4k
  WINPR_ASSERT(queue);
333
71.4k
  WINPR_ASSERT(queue->event);
334
335
71.4k
  EnterCriticalSection(&queue->lock);
336
337
73.6k
  while (queue->size > 0)
338
2.19k
  {
339
2.19k
    wMessage* msg = &(queue->array[queue->head]);
340
341
    /* Free resources of message. */
342
2.19k
    if (queue->object.fnObjectUninit)
343
0
      queue->object.fnObjectUninit(msg);
344
2.19k
    if (queue->object.fnObjectFree)
345
2.19k
      queue->object.fnObjectFree(msg);
346
347
2.19k
    ZeroMemory(msg, sizeof(wMessage));
348
349
2.19k
    queue->head = (queue->head + 1) % queue->capacity;
350
2.19k
    queue->size--;
351
2.19k
  }
352
71.4k
  if (!ResetEvent(queue->event))
353
0
    status = -1;
354
71.4k
  queue->closed = FALSE;
355
356
71.4k
  LeaveCriticalSection(&queue->lock);
357
358
71.4k
  return status;
359
71.4k
}