VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/reqqueue.cpp@ 77752

Last change on this file since 77752 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1/* $Id: reqqueue.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Request Queue.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/req.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/asm.h>
36#include <iprt/err.h>
37#include <iprt/string.h>
38#include <iprt/time.h>
39#include <iprt/semaphore.h>
40#include <iprt/thread.h>
41#include <iprt/log.h>
42#include <iprt/mem.h>
43
44#include "internal/req.h"
45#include "internal/magics.h"
46
47
48
49RTDECL(int) RTReqQueueCreate(RTREQQUEUE *phQueue)
50{
51 PRTREQQUEUEINT pQueue = (PRTREQQUEUEINT)RTMemAllocZ(sizeof(RTREQQUEUEINT));
52 if (!pQueue)
53 return VERR_NO_MEMORY;
54 int rc = RTSemEventCreate(&pQueue->EventSem);
55 if (RT_SUCCESS(rc))
56 {
57 pQueue->u32Magic = RTREQQUEUE_MAGIC;
58
59 *phQueue = pQueue;
60 return VINF_SUCCESS;
61 }
62
63 RTMemFree(pQueue);
64 return rc;
65}
66RT_EXPORT_SYMBOL(RTReqQueueCreate);
67
68
69RTDECL(int) RTReqQueueDestroy(RTREQQUEUE hQueue)
70{
71 /*
72 * Check input.
73 */
74 if (hQueue == NIL_RTREQQUEUE)
75 return VINF_SUCCESS;
76 PRTREQQUEUEINT pQueue = hQueue;
77 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
78 AssertReturn(ASMAtomicCmpXchgU32(&pQueue->u32Magic, RTREQQUEUE_MAGIC_DEAD, RTREQQUEUE_MAGIC), VERR_INVALID_HANDLE);
79
80 RTSemEventDestroy(pQueue->EventSem);
81 pQueue->EventSem = NIL_RTSEMEVENT;
82
83 for (unsigned i = 0; i < RT_ELEMENTS(pQueue->apReqFree); i++)
84 {
85 PRTREQ pReq = (PRTREQ)ASMAtomicXchgPtr((void **)&pQueue->apReqFree[i], NULL);
86 while (pReq)
87 {
88 PRTREQ pNext = pReq->pNext;
89 rtReqFreeIt(pReq);
90 pReq = pNext;
91 }
92 }
93
94 RTMemFree(pQueue);
95 return VINF_SUCCESS;
96}
97RT_EXPORT_SYMBOL(RTReqQueueDestroy);
98
99
100RTDECL(int) RTReqQueueProcess(RTREQQUEUE hQueue, RTMSINTERVAL cMillies)
101{
102 LogFlow(("RTReqQueueProcess %x\n", hQueue));
103
104 /*
105 * Check input.
106 */
107 PRTREQQUEUEINT pQueue = hQueue;
108 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
109 AssertReturn(pQueue->u32Magic == RTREQQUEUE_MAGIC, VERR_INVALID_HANDLE);
110
111 /*
112 * Process loop. Stop (break) after the first non-VINF_SUCCESS status code.
113 */
114 int rc = VINF_SUCCESS;
115 for (;;)
116 {
117 /*
118 * Get pending requests.
119 */
120 PRTREQ pReqs = ASMAtomicXchgPtrT(&pQueue->pAlreadyPendingReqs, NULL, PRTREQ);
121 if (RT_LIKELY(!pReqs))
122 {
123 pReqs = ASMAtomicXchgPtrT(&pQueue->pReqs, NULL, PRTREQ);
124 if (!pReqs)
125 {
126 /* We do not adjust cMillies (documented behavior). */
127 ASMAtomicWriteBool(&pQueue->fBusy, false); /* this aint 100% perfect, but it's good enough for now... */
128 rc = RTSemEventWait(pQueue->EventSem, cMillies);
129 if (rc != VINF_SUCCESS)
130 break;
131 continue;
132 }
133
134 ASMAtomicWriteBool(&pQueue->fBusy, true);
135
136 /*
137 * Reverse the list to process it in FIFO order.
138 */
139 PRTREQ pReq = pReqs;
140 if (pReq->pNext)
141 Log2(("RTReqQueueProcess: 2+ requests: %p %p %p\n", pReq, pReq->pNext, pReq->pNext->pNext));
142 pReqs = NULL;
143 while (pReq)
144 {
145 Assert(pReq->enmState == RTREQSTATE_QUEUED);
146 Assert(pReq->uOwner.hQueue == pQueue);
147 PRTREQ pCur = pReq;
148 pReq = pReq->pNext;
149 pCur->pNext = pReqs;
150 pReqs = pCur;
151 }
152
153 }
154 else
155 ASMAtomicWriteBool(&pQueue->fBusy, true);
156
157 /*
158 * Process the requests.
159 */
160 while (pReqs)
161 {
162 /* Unchain the first request and advance the list. */
163 PRTREQ pReq = pReqs;
164 pReqs = pReqs->pNext;
165 pReq->pNext = NULL;
166
167 /* Process the request. */
168 rc = rtReqProcessOne(pReq);
169 AssertRC(rc);
170 if (rc != VINF_SUCCESS)
171 {
172 /* Propagate the return code to caller. If more requests pending, queue them for later. */
173 if (pReqs)
174 {
175 pReqs = ASMAtomicXchgPtrT(&pQueue->pAlreadyPendingReqs, pReqs, PRTREQ);
176 Assert(!pReqs);
177 }
178 break;
179 }
180 }
181 if (rc != VINF_SUCCESS)
182 break;
183 }
184
185 LogFlow(("RTReqQueueProcess: returns %Rrc\n", rc));
186 return rc;
187}
188RT_EXPORT_SYMBOL(RTReqQueueProcess);
189
190
191RTDECL(int) RTReqQueueCall(RTREQQUEUE hQueue, PRTREQ *ppReq, RTMSINTERVAL cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
192{
193 va_list va;
194 va_start(va, cArgs);
195 int rc = RTReqQueueCallV(hQueue, ppReq, cMillies, RTREQFLAGS_IPRT_STATUS, pfnFunction, cArgs, va);
196 va_end(va);
197 return rc;
198}
199RT_EXPORT_SYMBOL(RTReqQueueCall);
200
201
202RTDECL(int) RTReqQueueCallVoid(RTREQQUEUE hQueue, PRTREQ *ppReq, RTMSINTERVAL cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
203{
204 va_list va;
205 va_start(va, cArgs);
206 int rc = RTReqQueueCallV(hQueue, ppReq, cMillies, RTREQFLAGS_VOID, pfnFunction, cArgs, va);
207 va_end(va);
208 return rc;
209}
210RT_EXPORT_SYMBOL(RTReqQueueCallVoid);
211
212
213RTDECL(int) RTReqQueueCallEx(RTREQQUEUE hQueue, PRTREQ *ppReq, RTMSINTERVAL cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, ...)
214{
215 va_list va;
216 va_start(va, cArgs);
217 int rc = RTReqQueueCallV(hQueue, ppReq, cMillies, fFlags, pfnFunction, cArgs, va);
218 va_end(va);
219 return rc;
220}
221RT_EXPORT_SYMBOL(RTReqQueueCallEx);
222
223
224RTDECL(int) RTReqQueueCallV(RTREQQUEUE hQueue, PRTREQ *ppReq, RTMSINTERVAL cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, va_list Args)
225{
226 LogFlow(("RTReqQueueCallV: cMillies=%d fFlags=%#x pfnFunction=%p cArgs=%d\n", cMillies, fFlags, pfnFunction, cArgs));
227
228 /*
229 * Check input.
230 */
231 PRTREQQUEUEINT pQueue = hQueue;
232 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
233 AssertReturn(pQueue->u32Magic == RTREQQUEUE_MAGIC, VERR_INVALID_HANDLE);
234 AssertPtrReturn(pfnFunction, VERR_INVALID_POINTER);
235 AssertReturn(!(fFlags & ~(RTREQFLAGS_RETURN_MASK | RTREQFLAGS_NO_WAIT)), VERR_INVALID_PARAMETER);
236
237 if (!(fFlags & RTREQFLAGS_NO_WAIT) || ppReq)
238 {
239 AssertPtrReturn(ppReq, VERR_INVALID_POINTER);
240 *ppReq = NULL;
241 }
242
243 PRTREQ pReq = NULL;
244 AssertMsgReturn(cArgs * sizeof(uintptr_t) <= sizeof(pReq->u.Internal.aArgs), ("cArgs=%u\n", cArgs), VERR_TOO_MUCH_DATA);
245
246 /*
247 * Allocate request
248 */
249 int rc = RTReqQueueAlloc(pQueue, RTREQTYPE_INTERNAL, &pReq);
250 if (rc != VINF_SUCCESS)
251 return rc;
252
253 /*
254 * Initialize the request data.
255 */
256 pReq->fFlags = fFlags;
257 pReq->u.Internal.pfn = pfnFunction;
258 pReq->u.Internal.cArgs = cArgs;
259 for (unsigned iArg = 0; iArg < cArgs; iArg++)
260 pReq->u.Internal.aArgs[iArg] = va_arg(Args, uintptr_t);
261
262 /*
263 * Queue the request and return.
264 */
265 rc = RTReqSubmit(pReq, cMillies);
266 if ( rc != VINF_SUCCESS
267 && rc != VERR_TIMEOUT)
268 {
269 RTReqRelease(pReq);
270 pReq = NULL;
271 }
272 if (!(fFlags & RTREQFLAGS_NO_WAIT))
273 {
274 *ppReq = pReq;
275 LogFlow(("RTReqQueueCallV: returns %Rrc *ppReq=%p\n", rc, pReq));
276 }
277 else
278 LogFlow(("RTReqQueueCallV: returns %Rrc\n", rc));
279 Assert(rc != VERR_INTERRUPTED);
280 return rc;
281}
282RT_EXPORT_SYMBOL(RTReqQueueCallV);
283
284
285RTDECL(bool) RTReqQueueIsBusy(RTREQQUEUE hQueue)
286{
287 PRTREQQUEUEINT pQueue = hQueue;
288 AssertPtrReturn(pQueue, false);
289
290 if (ASMAtomicReadBool(&pQueue->fBusy))
291 return true;
292 if (ASMAtomicReadPtrT(&pQueue->pReqs, PRTREQ) != NULL)
293 return true;
294 if (ASMAtomicReadBool(&pQueue->fBusy))
295 return true;
296 return false;
297}
298RT_EXPORT_SYMBOL(RTReqQueueIsBusy);
299
300
301/**
302 * Joins the list pList with whatever is linked up at *pHead.
303 */
304static void vmr3ReqJoinFreeSub(volatile PRTREQ *ppHead, PRTREQ pList)
305{
306 for (unsigned cIterations = 0;; cIterations++)
307 {
308 PRTREQ pHead = ASMAtomicXchgPtrT(ppHead, pList, PRTREQ);
309 if (!pHead)
310 return;
311 PRTREQ pTail = pHead;
312 while (pTail->pNext)
313 pTail = pTail->pNext;
314 pTail->pNext = pList;
315 if (ASMAtomicCmpXchgPtr(ppHead, pHead, pList))
316 return;
317 pTail->pNext = NULL;
318 if (ASMAtomicCmpXchgPtr(ppHead, pHead, NULL))
319 return;
320 pList = pHead;
321 Assert(cIterations != 32);
322 Assert(cIterations != 64);
323 }
324}
325
326
327/**
328 * Joins the list pList with whatever is linked up at *pHead.
329 */
330static void vmr3ReqJoinFree(PRTREQQUEUEINT pQueue, PRTREQ pList)
331{
332 /*
333 * Split the list if it's too long.
334 */
335 unsigned cReqs = 1;
336 PRTREQ pTail = pList;
337 while (pTail->pNext)
338 {
339 if (cReqs++ > 25)
340 {
341 const uint32_t i = pQueue->iReqFree;
342 vmr3ReqJoinFreeSub(&pQueue->apReqFree[(i + 2) % RT_ELEMENTS(pQueue->apReqFree)], pTail->pNext);
343
344 pTail->pNext = NULL;
345 vmr3ReqJoinFreeSub(&pQueue->apReqFree[(i + 2 + (i == pQueue->iReqFree)) % RT_ELEMENTS(pQueue->apReqFree)], pTail->pNext);
346 return;
347 }
348 pTail = pTail->pNext;
349 }
350 vmr3ReqJoinFreeSub(&pQueue->apReqFree[(pQueue->iReqFree + 2) % RT_ELEMENTS(pQueue->apReqFree)], pList);
351}
352
353
354RTDECL(int) RTReqQueueAlloc(RTREQQUEUE hQueue, RTREQTYPE enmType, PRTREQ *phReq)
355{
356 /*
357 * Validate input.
358 */
359 PRTREQQUEUEINT pQueue = hQueue;
360 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
361 AssertReturn(pQueue->u32Magic == RTREQQUEUE_MAGIC, VERR_INVALID_HANDLE);
362 AssertMsgReturn(enmType > RTREQTYPE_INVALID && enmType < RTREQTYPE_MAX, ("%d\n", enmType), VERR_RT_REQUEST_INVALID_TYPE);
363
364 /*
365 * Try get a recycled packet.
366 *
367 * While this could all be solved with a single list with a lock, it's a sport
368 * of mine to avoid locks.
369 */
370 int cTries = RT_ELEMENTS(pQueue->apReqFree) * 2;
371 while (--cTries >= 0)
372 {
373 PRTREQ volatile *ppHead = &pQueue->apReqFree[ASMAtomicIncU32(&pQueue->iReqFree) % RT_ELEMENTS(pQueue->apReqFree)];
374 PRTREQ pReq = ASMAtomicXchgPtrT(ppHead, NULL, PRTREQ);
375 if (pReq)
376 {
377 PRTREQ pNext = pReq->pNext;
378 if ( pNext
379 && !ASMAtomicCmpXchgPtr(ppHead, pNext, NULL))
380 vmr3ReqJoinFree(pQueue, pReq->pNext);
381 ASMAtomicDecU32(&pQueue->cReqFree);
382
383 Assert(pReq->uOwner.hQueue == pQueue);
384 Assert(!pReq->fPoolOrQueue);
385
386 int rc = rtReqReInit(pReq, enmType);
387 if (RT_SUCCESS(rc))
388 {
389 *phReq = pReq;
390 LogFlow(("RTReqQueueAlloc: returns VINF_SUCCESS *phReq=%p recycled\n", pReq));
391 return VINF_SUCCESS;
392 }
393 }
394 }
395
396 /*
397 * Ok, allocate a new one.
398 */
399 int rc = rtReqAlloc(enmType, false /*fPoolOrQueue*/, pQueue, phReq);
400 LogFlow(("RTReqQueueAlloc: returns %Rrc *phReq=%p\n", rc, *phReq));
401 return rc;
402}
403RT_EXPORT_SYMBOL(RTReqQueueAlloc);
404
405
406/**
407 * Recycles a requst.
408 *
409 * @returns true if recycled, false if it should be freed.
410 * @param pQueue The queue.
411 * @param pReq The request.
412 */
413DECLHIDDEN(bool) rtReqQueueRecycle(PRTREQQUEUEINT pQueue, PRTREQINT pReq)
414{
415 if ( !pQueue
416 || pQueue->cReqFree >= 128)
417 return false;
418
419 ASMAtomicIncU32(&pQueue->cReqFree);
420 PRTREQ volatile *ppHead = &pQueue->apReqFree[ASMAtomicIncU32(&pQueue->iReqFree) % RT_ELEMENTS(pQueue->apReqFree)];
421 PRTREQ pNext;
422 do
423 {
424 pNext = *ppHead;
425 ASMAtomicWritePtr(&pReq->pNext, pNext);
426 } while (!ASMAtomicCmpXchgPtr(ppHead, pReq, pNext));
427
428 return true;
429}
430
431
432/**
433 * Submits a request to the queue.
434 *
435 * @param pQueue The queue.
436 * @param pReq The request.
437 */
438DECLHIDDEN(void) rtReqQueueSubmit(PRTREQQUEUEINT pQueue, PRTREQINT pReq)
439{
440 PRTREQ pNext;
441 do
442 {
443 pNext = pQueue->pReqs;
444 pReq->pNext = pNext;
445 ASMAtomicWriteBool(&pQueue->fBusy, true);
446 } while (!ASMAtomicCmpXchgPtr(&pQueue->pReqs, pReq, pNext));
447
448 /*
449 * Notify queue thread.
450 */
451 RTSemEventSignal(pQueue->EventSem);
452}
453
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette