VirtualBox

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

Last change on this file since 39503 was 39503, checked in by vboxsync, 13 years ago

RTReq: Bugfix and some more refactoring.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/* $Id: reqqueue.cpp 39503 2011-12-01 21:36:44Z vboxsync $ */
2/** @file
3 * IPRT - Request Queue.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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/string.h>
37#include <iprt/time.h>
38#include <iprt/semaphore.h>
39#include <iprt/thread.h>
40#include <iprt/log.h>
41#include <iprt/mem.h>
42
43#include "internal/req.h"
44#include "internal/magics.h"
45
46
47
48RTDECL(int) RTReqQueueCreate(RTREQQUEUE *phQueue)
49{
50 PRTREQQUEUEINT pQueue = (PRTREQQUEUEINT)RTMemAllocZ(sizeof(RTREQQUEUEINT));
51 if (!pQueue)
52 return VERR_NO_MEMORY;
53 int rc = RTSemEventCreate(&pQueue->EventSem);
54 if (RT_SUCCESS(rc))
55 {
56 pQueue->u32Magic = RTREQQUEUE_MAGIC;
57
58 *phQueue = pQueue;
59 return VINF_SUCCESS;
60 }
61
62 RTMemFree(pQueue);
63 return rc;
64}
65RT_EXPORT_SYMBOL(RTReqQueueCreate);
66
67
68RTDECL(int) RTReqQueueDestroy(RTREQQUEUE hQueue)
69{
70 /*
71 * Check input.
72 */
73 if (hQueue == NIL_RTREQQUEUE)
74 return VINF_SUCCESS;
75 PRTREQQUEUEINT pQueue = hQueue;
76 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
77 AssertReturn(ASMAtomicCmpXchgU32(&pQueue->u32Magic, RTREQQUEUE_MAGIC_DEAD, RTREQQUEUE_MAGIC), VERR_INVALID_HANDLE);
78
79 RTSemEventDestroy(pQueue->EventSem);
80 pQueue->EventSem = NIL_RTSEMEVENT;
81
82 for (unsigned i = 0; i < RT_ELEMENTS(pQueue->apReqFree); i++)
83 {
84 PRTREQ pReq = (PRTREQ)ASMAtomicXchgPtr((void **)&pQueue->apReqFree[i], NULL);
85 while (pReq)
86 {
87 PRTREQ pNext = pReq->pNext;
88
89 pReq->u32Magic = RTREQ_MAGIC_DEAD;
90 RTSemEventDestroy(pReq->EventSem);
91 pReq->EventSem = NIL_RTSEMEVENT;
92 RTMemFree(pReq);
93
94 pReq = pNext;
95 }
96 }
97
98 RTMemFree(pQueue);
99 return VINF_SUCCESS;
100}
101RT_EXPORT_SYMBOL(RTReqQueueDestroy);
102
103
104RTDECL(int) RTReqQueueProcess(RTREQQUEUE hQueue, RTMSINTERVAL cMillies)
105{
106 LogFlow(("RTReqProcess %x\n", hQueue));
107
108 /*
109 * Check input.
110 */
111 PRTREQQUEUEINT pQueue = hQueue;
112 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
113 AssertReturn(pQueue->u32Magic == RTREQQUEUE_MAGIC, VERR_INVALID_HANDLE);
114
115 /*
116 * Process loop.
117 *
118 * We do not repeat the outer loop if we've got an informational status code
119 * since that code needs processing by our caller.
120 */
121 int rc = VINF_SUCCESS;
122 while (rc <= VINF_SUCCESS)
123 {
124 /*
125 * Get pending requests.
126 */
127 PRTREQ pReqs = ASMAtomicXchgPtrT(&pQueue->pReqs, NULL, PRTREQ);
128 if (!pReqs)
129 {
130 ASMAtomicWriteBool(&pQueue->fBusy, false); /* this aint 100% perfect, but it's good enough for now... */
131 /** @todo We currently don't care if the entire time wasted here is larger than
132 * cMillies */
133 rc = RTSemEventWait(pQueue->EventSem, cMillies);
134 if (rc != VINF_SUCCESS)
135 break;
136 continue;
137 }
138 ASMAtomicWriteBool(&pQueue->fBusy, true);
139
140 /*
141 * Reverse the list to process it in FIFO order.
142 */
143 PRTREQ pReq = pReqs;
144 if (pReq->pNext)
145 Log2(("RTReqProcess: 2+ requests: %p %p %p\n", pReq, pReq->pNext, pReq->pNext->pNext));
146 pReqs = NULL;
147 while (pReq)
148 {
149 Assert(pReq->enmState == RTREQSTATE_QUEUED);
150 Assert(pReq->uOwner.hQueue == pQueue);
151 PRTREQ pCur = pReq;
152 pReq = pReq->pNext;
153 pCur->pNext = pReqs;
154 pReqs = pCur;
155 }
156
157
158 /*
159 * Process the requests.
160 */
161 while (pReqs)
162 {
163 /* Unchain the first request and advance the list. */
164 pReq = pReqs;
165 pReqs = pReqs->pNext;
166 pReq->pNext = NULL;
167
168 /* Process the request */
169 rc = rtReqProcessOne(pReq);
170 AssertRC(rc);
171 if (rc != VINF_SUCCESS)
172 break; /** @todo r=bird: we're dropping requests here! Add 2nd queue that can hold them. (will fix when writing a testcase) */
173 }
174 }
175
176 LogFlow(("RTReqProcess: returns %Rrc\n", rc));
177 return rc;
178}
179RT_EXPORT_SYMBOL(RTReqQueueProcess);
180
181
182RTDECL(int) RTReqQueueCall(RTREQQUEUE hQueue, PRTREQ *ppReq, RTMSINTERVAL cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
183{
184 va_list va;
185 va_start(va, cArgs);
186 int rc = RTReqQueueCallV(hQueue, ppReq, cMillies, RTREQFLAGS_IPRT_STATUS, pfnFunction, cArgs, va);
187 va_end(va);
188 return rc;
189}
190RT_EXPORT_SYMBOL(RTReqQueueCall);
191
192
193RTDECL(int) RTReqQueueCallVoid(RTREQQUEUE hQueue, PRTREQ *ppReq, RTMSINTERVAL cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
194{
195 va_list va;
196 va_start(va, cArgs);
197 int rc = RTReqQueueCallV(hQueue, ppReq, cMillies, RTREQFLAGS_VOID, pfnFunction, cArgs, va);
198 va_end(va);
199 return rc;
200}
201RT_EXPORT_SYMBOL(RTReqQueueCallVoid);
202
203
204RTDECL(int) RTReqQueueCallEx(RTREQQUEUE hQueue, PRTREQ *ppReq, RTMSINTERVAL cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, ...)
205{
206 va_list va;
207 va_start(va, cArgs);
208 int rc = RTReqQueueCallV(hQueue, ppReq, cMillies, fFlags, pfnFunction, cArgs, va);
209 va_end(va);
210 return rc;
211}
212RT_EXPORT_SYMBOL(RTReqQueueCallEx);
213
214
215RTDECL(int) RTReqQueueCallV(RTREQQUEUE hQueue, PRTREQ *ppReq, RTMSINTERVAL cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, va_list Args)
216{
217 LogFlow(("RTReqCallV: cMillies=%d fFlags=%#x pfnFunction=%p cArgs=%d\n", cMillies, fFlags, pfnFunction, cArgs));
218
219 /*
220 * Check input.
221 */
222 PRTREQQUEUEINT pQueue = hQueue;
223 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
224 AssertReturn(pQueue->u32Magic == RTREQQUEUE_MAGIC, VERR_INVALID_HANDLE);
225 AssertPtrReturn(pfnFunction, VERR_INVALID_POINTER);
226 AssertReturn(!(fFlags & ~(RTREQFLAGS_RETURN_MASK | RTREQFLAGS_NO_WAIT)), VERR_INVALID_PARAMETER);
227
228 if (!(fFlags & RTREQFLAGS_NO_WAIT) || ppReq)
229 {
230 AssertPtrReturn(ppReq, VERR_INVALID_POINTER);
231 *ppReq = NULL;
232 }
233
234 PRTREQ pReq = NULL;
235 AssertMsgReturn(cArgs * sizeof(uintptr_t) <= sizeof(pReq->u.Internal.aArgs), ("cArgs=%u\n", cArgs), VERR_TOO_MUCH_DATA);
236
237 /*
238 * Allocate request
239 */
240 int rc = RTReqQueueAlloc(pQueue, &pReq, RTREQTYPE_INTERNAL);
241 if (rc != VINF_SUCCESS)
242 return rc;
243
244 /*
245 * Initialize the request data.
246 */
247 pReq->fFlags = fFlags;
248 pReq->u.Internal.pfn = pfnFunction;
249 pReq->u.Internal.cArgs = cArgs;
250 for (unsigned iArg = 0; iArg < cArgs; iArg++)
251 pReq->u.Internal.aArgs[iArg] = va_arg(Args, uintptr_t);
252
253 /*
254 * Queue the request and return.
255 */
256 rc = RTReqSubmit(pReq, cMillies);
257 if ( rc != VINF_SUCCESS
258 && rc != VERR_TIMEOUT)
259 {
260 RTReqFree(pReq);
261 pReq = NULL;
262 }
263 if (!(fFlags & RTREQFLAGS_NO_WAIT))
264 {
265 *ppReq = pReq;
266 LogFlow(("RTReqCallV: returns %Rrc *ppReq=%p\n", rc, pReq));
267 }
268 else
269 LogFlow(("RTReqCallV: returns %Rrc\n", rc));
270 Assert(rc != VERR_INTERRUPTED);
271 return rc;
272}
273RT_EXPORT_SYMBOL(RTReqQueueCallV);
274
275
276RTDECL(bool) RTReqQueueIsBusy(RTREQQUEUE hQueue)
277{
278 PRTREQQUEUEINT pQueue = hQueue;
279 AssertPtrReturn(pQueue, false);
280
281 if (ASMAtomicReadBool(&pQueue->fBusy))
282 return true;
283 if (ASMAtomicReadPtrT(&pQueue->pReqs, PRTREQ) != NULL)
284 return true;
285 if (ASMAtomicReadBool(&pQueue->fBusy))
286 return true;
287 return false;
288}
289RT_EXPORT_SYMBOL(RTReqQueueIsBusy);
290
291
292/**
293 * Joins the list pList with whatever is linked up at *pHead.
294 */
295static void vmr3ReqJoinFreeSub(volatile PRTREQ *ppHead, PRTREQ pList)
296{
297 for (unsigned cIterations = 0;; cIterations++)
298 {
299 PRTREQ pHead = ASMAtomicXchgPtrT(ppHead, pList, PRTREQ);
300 if (!pHead)
301 return;
302 PRTREQ pTail = pHead;
303 while (pTail->pNext)
304 pTail = pTail->pNext;
305 pTail->pNext = pList;
306 if (ASMAtomicCmpXchgPtr(ppHead, pHead, pList))
307 return;
308 pTail->pNext = NULL;
309 if (ASMAtomicCmpXchgPtr(ppHead, pHead, NULL))
310 return;
311 pList = pHead;
312 Assert(cIterations != 32);
313 Assert(cIterations != 64);
314 }
315}
316
317
318/**
319 * Joins the list pList with whatever is linked up at *pHead.
320 */
321static void vmr3ReqJoinFree(PRTREQQUEUEINT pQueue, PRTREQ pList)
322{
323 /*
324 * Split the list if it's too long.
325 */
326 unsigned cReqs = 1;
327 PRTREQ pTail = pList;
328 while (pTail->pNext)
329 {
330 if (cReqs++ > 25)
331 {
332 const uint32_t i = pQueue->iReqFree;
333 vmr3ReqJoinFreeSub(&pQueue->apReqFree[(i + 2) % RT_ELEMENTS(pQueue->apReqFree)], pTail->pNext);
334
335 pTail->pNext = NULL;
336 vmr3ReqJoinFreeSub(&pQueue->apReqFree[(i + 2 + (i == pQueue->iReqFree)) % RT_ELEMENTS(pQueue->apReqFree)], pTail->pNext);
337 return;
338 }
339 pTail = pTail->pNext;
340 }
341 vmr3ReqJoinFreeSub(&pQueue->apReqFree[(pQueue->iReqFree + 2) % RT_ELEMENTS(pQueue->apReqFree)], pList);
342}
343
344
345RTDECL(int) RTReqQueueAlloc(RTREQQUEUE hQueue, PRTREQ *ppReq, RTREQTYPE enmType)
346{
347 /*
348 * Validate input.
349 */
350 PRTREQQUEUEINT pQueue = hQueue;
351 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
352 AssertReturn(pQueue->u32Magic == RTREQQUEUE_MAGIC, VERR_INVALID_HANDLE);
353 AssertMsgReturn(enmType > RTREQTYPE_INVALID && enmType < RTREQTYPE_MAX, ("%d\n", enmType), VERR_RT_REQUEST_INVALID_TYPE);
354
355 /*
356 * Try get a recycled packet.
357 * While this could all be solved with a single list with a lock, it's a sport
358 * of mine to avoid locks.
359 */
360 int cTries = RT_ELEMENTS(pQueue->apReqFree) * 2;
361 while (--cTries >= 0)
362 {
363 PRTREQ volatile *ppHead = &pQueue->apReqFree[ASMAtomicIncU32(&pQueue->iReqFree) % RT_ELEMENTS(pQueue->apReqFree)];
364#if 0 /* sad, but this won't work safely because the reading of pReq->pNext. */
365 PRTREQ pNext = NULL;
366 PRTREQ pReq = *ppHead;
367 if ( pReq
368 && !ASMAtomicCmpXchgPtr(ppHead, (pNext = pReq->pNext), pReq)
369 && (pReq = *ppHead)
370 && !ASMAtomicCmpXchgPtr(ppHead, (pNext = pReq->pNext), pReq))
371 pReq = NULL;
372 if (pReq)
373 {
374 Assert(pReq->pNext == pNext); NOREF(pReq);
375#else
376 PRTREQ pReq = ASMAtomicXchgPtrT(ppHead, NULL, PRTREQ);
377 if (pReq)
378 {
379 PRTREQ pNext = pReq->pNext;
380 if ( pNext
381 && !ASMAtomicCmpXchgPtr(ppHead, pNext, NULL))
382 {
383 vmr3ReqJoinFree(pQueue, pReq->pNext);
384 }
385#endif
386 ASMAtomicDecU32(&pQueue->cReqFree);
387
388 /*
389 * Make sure the event sem is not signaled.
390 */
391 if (!pReq->fEventSemClear)
392 {
393 int rc = RTSemEventWait(pReq->EventSem, 0);
394 if (rc != VINF_SUCCESS && rc != VERR_TIMEOUT)
395 {
396 /*
397 * This shall not happen, but if it does we'll just destroy
398 * the semaphore and create a new one.
399 */
400 AssertMsgFailed(("rc=%Rrc from RTSemEventWait(%#x).\n", rc, pReq->EventSem));
401 RTSemEventDestroy(pReq->EventSem);
402 rc = RTSemEventCreate(&pReq->EventSem);
403 AssertRC(rc);
404 if (rc != VINF_SUCCESS)
405 return rc;
406 }
407 pReq->fEventSemClear = true;
408 }
409 else
410 Assert(RTSemEventWait(pReq->EventSem, 0) == VERR_TIMEOUT);
411
412 /*
413 * Initialize the packet and return it.
414 */
415 Assert(pReq->u32Magic == RTREQ_MAGIC);
416 Assert(pReq->enmType == RTREQTYPE_INVALID);
417 Assert(pReq->enmState == RTREQSTATE_FREE);
418 Assert(!pReq->fPoolOrQueue);
419 Assert(pReq->uOwner.hQueue == pQueue);
420 ASMAtomicWriteNullPtr(&pReq->pNext);
421 pReq->iStatusX = VERR_RT_REQUEST_STATUS_STILL_PENDING;
422 pReq->enmState = RTREQSTATE_ALLOCATED;
423 pReq->fFlags = RTREQFLAGS_IPRT_STATUS;
424 pReq->enmType = enmType;
425
426 *ppReq = pReq;
427 LogFlow(("RTReqAlloc: returns VINF_SUCCESS *ppReq=%p recycled\n", pReq));
428 return VINF_SUCCESS;
429 }
430 }
431
432 /*
433 * Ok allocate one.
434 */
435 PRTREQ pReq = (PRTREQ)RTMemAllocZ(sizeof(*pReq));
436 if (!pReq)
437 return VERR_NO_MEMORY;
438
439 /*
440 * Create the semaphore.
441 */
442 int rc = RTSemEventCreate(&pReq->EventSem);
443 AssertRC(rc);
444 if (rc != VINF_SUCCESS)
445 {
446 RTMemFree(pReq);
447 return rc;
448 }
449
450 /*
451 * Initialize the packet and return it.
452 */
453 pReq->u32Magic = RTREQ_MAGIC;
454 pReq->fEventSemClear= true;
455 pReq->fPoolOrQueue = false;
456 pReq->iStatusX = VERR_RT_REQUEST_STATUS_STILL_PENDING;
457 pReq->enmState = RTREQSTATE_ALLOCATED;
458 pReq->pNext = NULL;
459 pReq->uOwner.hQueue = pQueue;
460 pReq->fFlags = RTREQFLAGS_IPRT_STATUS;
461 pReq->enmType = enmType;
462
463 *ppReq = pReq;
464 LogFlow(("RTReqAlloc: returns VINF_SUCCESS *ppReq=%p new\n", pReq));
465 return VINF_SUCCESS;
466}
467RT_EXPORT_SYMBOL(RTReqQueueAlloc);
468
Note: See TracBrowser for help on using the repository browser.

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