VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMR3Task.cpp@ 81525

Last change on this file since 81525 was 81488, checked in by vboxsync, 5 years ago

%06x

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.6 KB
Line 
1/* $Id: PDMR3Task.cpp 81488 2019-10-23 10:41:29Z vboxsync $ */
2/** @file
3 * PDM Task - Asynchronous user mode tasks.
4 */
5
6/*
7 * Copyright (C) 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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_PDM_TASK
23#include "PDMInternal.h"
24#include <VBox/vmm/pdmtask.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/vm.h>
27#include <VBox/err.h>
28
29#include <VBox/log.h>
30#include <VBox/sup.h>
31#include <iprt/asm.h>
32#include <iprt/assert.h>
33#include <iprt/semaphore.h>
34#include <iprt/thread.h>
35
36
37/**
38 * @callback_method_impl{FNDBGFINFOARGVINT}
39 */
40static DECLCALLBACK(void) pdmR3TaskInfo(PVM pVM, PCDBGFINFOHLP pHlp, int cArgs, char **papszArgs)
41{
42 RT_NOREF(cArgs, papszArgs); /* for now. */
43
44 uint32_t cSetsDisplayed = 0;
45 for (size_t i = 0; i < RT_ELEMENTS(pVM->pdm.s.apTaskSets); i++)
46 {
47 PPDMTASKSET pTaskSet = pVM->pdm.s.apTaskSets[i];
48 if ( pTaskSet
49 && ( pTaskSet->cAllocated > 0
50 || ASMAtomicReadU64(&pTaskSet->fTriggered)))
51 {
52 if (cSetsDisplayed > 0)
53 pHlp->pfnPrintf(pHlp, "\n");
54 pHlp->pfnPrintf(pHlp,
55 "Task set #%u - handle base %u, pending %#RX64%s%s, running %d, %u of %u allocated:\n"
56 /* 123: triggered internal 0123456789abcdef 0123456789abcdef 0x0000 SomeFunctionName */
57 " Hnd: State Type pfnCallback pvUser Flags Name\n",
58 i, pTaskSet->uHandleBase, ASMAtomicReadU64(&pTaskSet->fTriggered),
59 pTaskSet->fRZEnabled ? " RZ-enabled" : "", pTaskSet->hThread != NIL_RTTHREAD ? "" : " no-thread",
60 (int)ASMAtomicReadU32(&pTaskSet->idxRunning), pTaskSet->cAllocated, RT_ELEMENTS(pTaskSet->aTasks));
61 for (unsigned j = 0; j < RT_ELEMENTS(pTaskSet->aTasks); j++)
62 {
63 PPDMTASK pTask = &pTaskSet->aTasks[j];
64 if (pTask->pvOwner)
65 {
66 const char *pszType;
67 switch (pTask->enmType)
68 {
69 case PDMTASKTYPE_DEV: pszType = " device "; break;
70 case PDMTASKTYPE_DRV: pszType = " driver "; break;
71 case PDMTASKTYPE_USB: pszType = " usbdev "; break;
72 case PDMTASKTYPE_INTERNAL: pszType = "internal"; break;
73 default: pszType = "unknown "; break;
74 }
75 pHlp->pfnPrintf(pHlp, " %3u: %s %s %p %p %#06x %s\n", pTaskSet->uHandleBase + j,
76 ASMBitTest(&pTaskSet->fTriggered, j) ? "triggered"
77 : ASMAtomicReadU32(&pTaskSet->idxRunning) == j ? " running " : " idle ",
78 pszType, pTask->pfnCallback, pTask->pvUser, pTask->fFlags, pTask->pszName);
79 }
80 }
81
82 cSetsDisplayed++;
83 }
84 }
85}
86
87
88/**
89 * Initializes the ring-0 capable tasks during VM construction.
90 *
91 * @returns VBox status code.
92 * @param pVM The cross context VM structure.
93 */
94int pdmR3TaskInit(PVM pVM)
95{
96 for (size_t i = 0; i < RT_ELEMENTS(pVM->pdm.s.aTaskSets); i++)
97 {
98 PPDMTASKSET pTaskSet = &pVM->pdm.s.aTaskSets[i];
99
100 pTaskSet->u32Magic = PDMTASKSET_MAGIC;
101 pTaskSet->fRZEnabled = true;
102 //pTaskSet->cAllocated = 0;
103 pTaskSet->uHandleBase = (uint16_t)(i * RT_ELEMENTS(pTaskSet->aTasks));
104 pTaskSet->hThread = NIL_RTTHREAD;
105 int rc = SUPSemEventCreate(pVM->pSession, &pTaskSet->hEventR0);
106 AssertRCReturn(rc, rc);
107 pTaskSet->hEventR3 = NIL_RTSEMEVENT;
108 //pTaskSet->fTriggered = 0;
109 pTaskSet->idxRunning = UINT8_MAX;
110 //pTaskSet->fShutdown = false;
111 pTaskSet->pVM = pVM;
112
113 pVM->pdm.s.apTaskSets[i] = pTaskSet;
114 }
115
116 int rc = DBGFR3InfoRegisterInternalArgv(pVM, "tasks", "PDM tasks", pdmR3TaskInfo, 0 /*fFlags*/);
117 AssertRC(rc);
118
119 return VINF_SUCCESS;
120}
121
122
123/**
124 * Terminates task threads when the VM is destroyed.
125 *
126 * @param pVM The cross context VM structure.
127 */
128void pdmR3TaskTerm(PVM pVM)
129{
130 /*
131 * Signal all the threads first.
132 */
133 for (size_t i = 0; i < RT_ELEMENTS(pVM->pdm.s.apTaskSets); i++)
134 {
135 PPDMTASKSET pTaskSet = pVM->pdm.s.apTaskSets[i];
136 if (pTaskSet)
137 {
138 /*
139 * Set the shutdown indicator and signal the thread.
140 */
141 ASMAtomicWriteBool(&pTaskSet->fShutdown, true);
142
143 if (pTaskSet->hEventR0 != NIL_SUPSEMEVENT)
144 {
145 int rc = SUPSemEventSignal(pVM->pSession, pTaskSet->hEventR0);
146 AssertRC(rc);
147 }
148
149 if (pTaskSet->hEventR3 != NIL_RTSEMEVENT)
150 {
151 int rc = RTSemEventSignal(pTaskSet->hEventR3);
152 AssertRC(rc);
153 }
154 }
155 }
156
157 /*
158 * Wait for them to terminate and clean up semaphores.
159 */
160 for (size_t i = 0; i < RT_ELEMENTS(pVM->pdm.s.apTaskSets); i++)
161 {
162 PPDMTASKSET pTaskSet = pVM->pdm.s.apTaskSets[i];
163 if (pTaskSet)
164 {
165 /*
166 * Wait for the thread to terminate.
167 */
168 if (pTaskSet->hThread != NIL_RTTHREAD)
169 {
170 int rc = RTThreadWait(pTaskSet->hThread, RT_MS_30SEC, NULL);
171 AssertLogRelMsg(RT_SUCCESS(rc), ("pTaskSet %u: thread wait failed: %Rrc\n", i, rc));
172 if (RT_SUCCESS(rc))
173 pTaskSet->hThread = NIL_RTTHREAD;
174 }
175
176 /*
177 * Destroy the semaphore.
178 */
179 if (pTaskSet->hEventR0 != NIL_SUPSEMEVENT)
180 {
181 int rc = SUPSemEventClose(pVM->pSession, pTaskSet->hEventR0);
182 AssertRC(rc);
183 pTaskSet->hEventR0 = NIL_SUPSEMEVENT;
184 }
185
186 if (pTaskSet->hEventR3 != NIL_RTSEMEVENT)
187 {
188 int rc = RTSemEventDestroy(pTaskSet->hEventR3);
189 AssertRC(rc);
190 pTaskSet->hEventR3 = NIL_RTSEMEVENT;
191 }
192 }
193 }
194}
195
196
197/**
198 * @callback_method_impl{FNRTTHREAD,
199 * PDM Asynchronous Task Executor Thread}
200 */
201static DECLCALLBACK(int) pdmR3TaskThread(RTTHREAD ThreadSelf, void *pvUser)
202{
203 PPDMTASKSET const pTaskSet = (PPDMTASKSET)pvUser;
204 AssertPtr(pTaskSet);
205 Assert(pTaskSet->u32Magic == PDMTASKSET_MAGIC);
206 RT_NOREF(ThreadSelf);
207
208 /*
209 * Process stuff until we're told to terminate.
210 */
211 while (!ASMAtomicReadBool(&pTaskSet->fShutdown))
212 {
213 /*
214 * Process pending tasks.
215 *
216 * The outer loop runs till there are no more pending tasks.
217 *
218 * The inner loop takes one snapshot of fTriggered and processes all
219 * pending bits in the snaphot. This ensure fairness.
220 */
221 for (;;)
222 {
223 uint64_t fTriggered = ASMAtomicReadU64(&pTaskSet->fTriggered);
224 unsigned iTask = ASMBitFirstSetU64(fTriggered);
225 if (iTask == 0)
226 break;
227 do
228 {
229 iTask--;
230 AssertBreak(iTask < RT_ELEMENTS(pTaskSet->aTasks));
231
232 if (ASMAtomicBitTestAndClear(&pTaskSet->fTriggered, iTask))
233 {
234 PPDMTASK pTask = &pTaskSet->aTasks[iTask];
235
236 /* Copy out the data we need here to try avoid destruction race trouble. */
237 PDMTASKTYPE const enmType = pTask->enmType;
238 PFNRT const pfnCallback = pTask->pfnCallback;
239 void * const pvOwner = pTask->pvOwner;
240 void * const pvTaskUser = pTask->pvUser;
241
242 ASMAtomicWriteU32(&pTaskSet->idxRunning, iTask);
243
244 if ( pvOwner
245 && pfnCallback
246 && pvOwner == pTask->pvOwner
247 && pfnCallback == pTask->pfnCallback
248 && pvTaskUser == pTask->pvUser
249 && enmType == pTask->enmType)
250 {
251 pTask->cRuns += 1;
252 switch (pTask->enmType)
253 {
254 case PDMTASKTYPE_DEV:
255 Log2(("pdmR3TaskThread: Runs dev task %s (%#x)\n", pTask->pszName, iTask + pTaskSet->uHandleBase));
256 ((PFNPDMTASKDEV)(pfnCallback))((PPDMDEVINS)pvOwner, pvTaskUser);
257 break;
258 case PDMTASKTYPE_DRV:
259 Log2(("pdmR3TaskThread: Runs drv task %s (%#x)\n", pTask->pszName, iTask + pTaskSet->uHandleBase));
260 ((PFNPDMTASKDRV)(pfnCallback))((PPDMDRVINS)pvOwner, pvTaskUser);
261 break;
262 case PDMTASKTYPE_USB:
263 Log2(("pdmR3TaskThread: Runs USB task %s (%#x)\n", pTask->pszName, iTask + pTaskSet->uHandleBase));
264 ((PFNPDMTASKUSB)(pfnCallback))((PPDMUSBINS)pvOwner, pvTaskUser);
265 break;
266 case PDMTASKTYPE_INTERNAL:
267 Log2(("pdmR3TaskThread: Runs int task %s (%#x)\n", pTask->pszName, iTask + pTaskSet->uHandleBase));
268 ((PFNPDMTASKINT)(pfnCallback))((PVM)pvOwner, pvTaskUser);
269 break;
270 default:
271 AssertFailed();
272 }
273 }
274 else /* Note! There might be a race here during destruction. */
275 AssertMsgFailed(("%d %p %p %p\n", enmType, pvOwner, pfnCallback, pvTaskUser));
276
277 ASMAtomicWriteU32(&pTaskSet->idxRunning, UINT32_MAX);
278 }
279
280 /* Next pending task. */
281 fTriggered &= ~RT_BIT_64(iTask);
282 iTask = ASMBitFirstSetU64(fTriggered);
283 } while (iTask != 0);
284 }
285
286 /*
287 * Wait.
288 */
289 if (pTaskSet->fRZEnabled)
290 SUPSemEventWaitNoResume(pTaskSet->pVM->pSession, pTaskSet->hEventR0, RT_MS_15SEC);
291 else
292 RTSemEventWaitNoResume(pTaskSet->hEventR3, RT_MS_15SEC);
293 }
294
295 return VINF_SUCCESS;
296}
297
298
299/**
300 * Worker for PDMR3TaskCreate().
301 */
302DECLINLINE(PPDMTASK) pdmR3TaskAllocInSet(PPDMTASKSET pTaskSet)
303{
304 if (pTaskSet->cAllocated < RT_ELEMENTS(pTaskSet->aTasks))
305 {
306 for (size_t j = 0; j < RT_ELEMENTS(pTaskSet->aTasks); j++)
307 if (pTaskSet->aTasks[j].pvOwner == NULL)
308 return &pTaskSet->aTasks[j];
309 AssertFailed();
310 }
311 return NULL;
312}
313
314/**
315 * Creates a task.
316 *
317 * @returns VBox status code.
318 * @param pVM The cross context VM structure.
319 * @param fFlags PDMTASK_F_XXX.
320 * @param pszName The task name (function name ++).
321 * @param enmType The task owner type.
322 * @param pvOwner The task owner pointer.
323 * @param pfnCallback The task callback.
324 * @param pvUser The user argument for the callback.
325 * @param phTask Where to return the task handle.
326 * @thread EMT(0)
327 */
328VMMR3_INT_DECL(int) PDMR3TaskCreate(PVM pVM, uint32_t fFlags, const char *pszName, PDMTASKTYPE enmType, void *pvOwner,
329 PFNRT pfnCallback, void *pvUser, PDMTASKHANDLE *phTask)
330{
331 /*
332 * Validate input.
333 */
334 AssertReturn(!(fFlags & ~PDMTASK_F_VALID_MASK), VERR_INVALID_FLAGS);
335 AssertPtrReturn(pvOwner, VERR_INVALID_POINTER);
336 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
337 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
338 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
339 VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT); /* implicit serialization by requiring EMT(0) */
340 switch (enmType)
341 {
342 case PDMTASKTYPE_DEV:
343 case PDMTASKTYPE_DRV:
344 case PDMTASKTYPE_USB:
345 break;
346 case PDMTASKTYPE_INTERNAL:
347 AssertReturn(pvOwner == (void *)pVM, VERR_INVALID_PARAMETER);
348 break;
349 default:
350 AssertFailedReturn(VERR_INVALID_PARAMETER);
351 }
352
353 /*
354 * If the callback must be ring-0 triggerable, we are restricted to the
355 * task sets living the VM structure. Otherwise, pick from the dynamically
356 * allocated sets living on ring-3 heap.
357 */
358 PPDMTASKSET pTaskSet = NULL;
359 PPDMTASK pTask = NULL;
360 if (fFlags & PDMTASK_F_RZ)
361 {
362 for (size_t i = 0; i < RT_ELEMENTS(pVM->pdm.s.aTaskSets); i++)
363 {
364 pTaskSet = &pVM->pdm.s.aTaskSets[i];
365 pTask = pdmR3TaskAllocInSet(pTaskSet);
366 if (pTask)
367 break;
368 }
369 }
370 else
371 {
372 for (size_t i = RT_ELEMENTS(pVM->pdm.s.aTaskSets); i < RT_ELEMENTS(pVM->pdm.s.apTaskSets); i++)
373 {
374 pTaskSet = pVM->pdm.s.apTaskSets[i];
375 if (pTaskSet)
376 {
377 pTask = pdmR3TaskAllocInSet(pTaskSet);
378 if (pTask)
379 break;
380 }
381 else
382 {
383 /*
384 * Try allocate a new set.
385 */
386 LogFlow(("PDMR3TaskCreate: Allocating new task set (%#u)...\n", i));
387 pTaskSet = (PPDMTASKSET)MMR3HeapAllocZ(pVM, MM_TAG_PDM, sizeof(*pTaskSet));
388 AssertReturn(pTaskSet, VERR_NO_MEMORY);
389
390 pTaskSet->u32Magic = PDMTASKSET_MAGIC;
391 //pTaskSet->fRZEnabled = false;
392 //pTaskSet->cAllocated = 0;
393 pTaskSet->uHandleBase = (uint16_t)(i * RT_ELEMENTS(pTaskSet->aTasks));
394 pTaskSet->hThread = NIL_RTTHREAD;
395 pTaskSet->hEventR0 = NIL_SUPSEMEVENT;
396 int rc = RTSemEventCreate(&pTaskSet->hEventR3);
397 AssertRCReturnStmt(rc, MMR3HeapFree(pTaskSet), rc);
398 //pTaskSet->fTriggered = 0;
399 pTaskSet->idxRunning = UINT8_MAX;
400 //pTaskSet->fShutdown = false;
401 pTaskSet->pVM = pVM;
402
403 pVM->pdm.s.apTaskSets[i] = pTaskSet;
404 pTask = &pTaskSet->aTasks[0];
405 break;
406 }
407 }
408 }
409 AssertLogRelReturn(pTask, VERR_OUT_OF_RESOURCES);
410
411 /*
412 * Do we need to start a worker thread? Do this first as it can fail.
413 */
414 if (pTaskSet->hThread == NIL_RTTHREAD)
415 {
416 int rc = RTThreadCreateF(&pTaskSet->hThread, pdmR3TaskThread, pTaskSet, 0 /*cbStack*/, RTTHREADTYPE_IO,
417 RTTHREADFLAGS_WAITABLE, "TaskSet%u", pTaskSet->uHandleBase / RT_ELEMENTS(pTaskSet->aTasks));
418 AssertLogRelRCReturn(rc, rc);
419 }
420
421 /*
422 * Complete the allocation.
423 */
424 pTask->enmType = enmType;
425 pTask->fFlags = fFlags;
426 pTask->pvUser = pvUser;
427 pTask->pfnCallback = pfnCallback;
428 pTask->pszName = pszName;
429 ASMAtomicWritePtr(&pTask->pvOwner, pvOwner);
430 pTaskSet->cAllocated += 1;
431
432 uint32_t const hTask = pTaskSet->uHandleBase + (uint32_t)(pTask - &pTaskSet->aTasks[0]);
433 *phTask = hTask;
434
435 STAMR3RegisterF(pVM, &pTask->cRuns, STAMTYPE_U32_RESET, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
436 "Number of times the task has been executed.", "/PDM/Tasks/%03u-%s-runs", hTask, pszName);
437 STAMR3RegisterF(pVM, (void *)&pTask->cAlreadyTrigged, STAMTYPE_U32_RESET, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
438 "Number of times the task was re-triggered.", "/PDM/Tasks/%03u-%s-retriggered", hTask, pszName);
439
440 LogFlow(("PDMR3TaskCreate: Allocated %u for %s\n", hTask, pszName));
441 return VINF_SUCCESS;
442}
443
444
445/**
446 * Creates an internal task.
447 *
448 * @returns VBox status code.
449 * @param pVM The cross context VM structure.
450 * @param fFlags PDMTASK_F_XXX.
451 * @param pszName The task name (function name ++).
452 * @param pfnCallback The task callback.
453 * @param pvUser The user argument for the callback.
454 * @param phTask Where to return the task handle.
455 * @thread EMT(0)
456 */
457VMMR3_INT_DECL(int) PDMR3TaskCreateInternal(PVM pVM, uint32_t fFlags, const char *pszName,
458 PFNPDMTASKINT pfnCallback, void *pvUser, PDMTASKHANDLE *phTask)
459{
460 return PDMR3TaskCreate(pVM, fFlags, pszName, PDMTASKTYPE_INTERNAL, pVM, (PFNRT)pfnCallback, pvUser, phTask);
461}
462
463
464/**
465 * Worker for PDMR3TaskDestroyAllByOwner() and PDMR3TaskDestroySpecific().
466 */
467static void pdmR3TaskDestroyOne(PVM pVM, PPDMTASKSET pTaskSet, PPDMTASK pTask, size_t iTask)
468{
469 AssertPtr(pTask->pvOwner);
470
471 /*
472 * Delay if busy.
473 */
474 uint32_t cYields = 64;
475 while ( ASMAtomicReadU32(&pTaskSet->idxRunning) == iTask
476 && cYields > 0
477 && pTaskSet->hThread != NIL_RTTHREAD)
478 {
479 ASMNopPause();
480 RTThreadYield();
481 }
482
483 /*
484 * Zap it (very noisy, but whatever).
485 */
486 LogFlow(("pdmR3TaskDestroyOne: Destroying %zu %s\n", iTask + pTaskSet->uHandleBase, pTask->pszName));
487 AssertPtr(pTask->pvOwner);
488
489 char szPrefix[64];
490 RTStrPrintf(szPrefix, sizeof(szPrefix), "/PDM/Tasks/%03zu-", iTask + pTaskSet->uHandleBase);
491 STAMR3DeregisterByPrefix(pVM->pUVM, szPrefix);
492
493 AssertPtr(pTask->pvOwner);
494 ASMAtomicWriteNullPtr(&pTask->pvOwner);
495 pTask->enmType = (PDMTASKTYPE)0;
496 pTask->fFlags = 0;
497 ASMAtomicWriteNullPtr(&pTask->pfnCallback);
498 ASMAtomicWriteNullPtr(&pTask->pvUser);
499 ASMAtomicWriteNullPtr(&pTask->pszName);
500
501 AssertReturnVoid(pTaskSet->cAllocated > 0);
502 pTaskSet->cAllocated -= 1;
503}
504
505
506/**
507 * Destroys all tasks belonging to @a pvOwner.
508 *
509 * @returns VBox status code.
510 * @param pVM The cross context VM structure.
511 * @param enmType The owner type.
512 * @param pvOwner The owner.
513 */
514VMMR3_INT_DECL(int) PDMR3TaskDestroyAllByOwner(PVM pVM, PDMTASKTYPE enmType, void *pvOwner)
515{
516 /*
517 * Validate input.
518 */
519 AssertReturn(enmType >= PDMTASKTYPE_DEV && enmType < PDMTASKTYPE_INTERNAL, VERR_INVALID_PARAMETER);
520 AssertPtrReturn(pvOwner, VERR_INVALID_POINTER);
521 VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT); /* implicit serialization by requiring EMT(0) */
522
523 /*
524 * Scan all the task sets.
525 */
526 for (size_t i = 0; i < RT_ELEMENTS(pVM->pdm.s.apTaskSets); i++)
527 {
528 PPDMTASKSET pTaskSet = pVM->pdm.s.apTaskSets[i];
529 if (pTaskSet)
530 {
531 ssize_t cLeft = pTaskSet->cAllocated;
532 for (size_t j = 0; j < RT_ELEMENTS(pTaskSet->aTasks) && cLeft > 0; j++)
533 {
534 PPDMTASK pTask = &pTaskSet->aTasks[j];
535 void * const pvTaskOwner = pTask->pvOwner;
536 if (pvTaskOwner)
537 {
538 if ( pvTaskOwner == pvOwner
539 && pTask->enmType == enmType)
540 pdmR3TaskDestroyOne(pVM, pTaskSet, pTask, j);
541 else
542 Assert(pvTaskOwner != pvOwner);
543 cLeft--;
544 }
545 }
546 }
547 else
548 break;
549 }
550
551 return VINF_SUCCESS;
552}
553
554
555/**
556 * Destroys the task @a hTask.
557 *
558 * @returns VBox status code.
559 * @param pVM The cross context VM structure.
560 * @param enmType The owner type.
561 * @param pvOwner The owner.
562 * @param hTask Handle to the task to destroy.
563 */
564VMMR3_INT_DECL(int) PDMR3TaskDestroySpecific(PVM pVM, PDMTASKTYPE enmType, void *pvOwner, PDMTASKHANDLE hTask)
565{
566 /*
567 * Validate the input.
568 */
569 AssertReturn(enmType >= PDMTASKTYPE_DEV && enmType <= PDMTASKTYPE_INTERNAL, VERR_INVALID_PARAMETER);
570 AssertPtrReturn(pvOwner, VERR_INVALID_POINTER);
571
572 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
573
574 size_t const iTask = hTask % RT_ELEMENTS(pVM->pdm.s.apTaskSets[0]->aTasks);
575 size_t const iTaskSet = hTask / RT_ELEMENTS(pVM->pdm.s.apTaskSets[0]->aTasks);
576 AssertReturn(iTaskSet < RT_ELEMENTS(pVM->pdm.s.apTaskSets), VERR_INVALID_HANDLE);
577 PPDMTASKSET const pTaskSet = pVM->pdm.s.apTaskSets[iTaskSet];
578 AssertPtrReturn(pTaskSet, VERR_INVALID_HANDLE);
579 AssertPtrReturn(pTaskSet->u32Magic == PDMTASKSET_MAGIC, VERR_INVALID_MAGIC);
580 PPDMTASK const pTask = &pTaskSet->aTasks[iTask];
581
582 VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT); /* implicit serialization by requiring EMT(0) */
583
584 AssertPtrReturn(pTask->pvOwner == pvOwner, VERR_NOT_OWNER);
585 AssertPtrReturn(pTask->enmType == enmType, VERR_NOT_OWNER);
586
587 /*
588 * Do the job.
589 */
590 pdmR3TaskDestroyOne(pVM, pTaskSet, pTask, iTask);
591
592 return VINF_SUCCESS;
593}
594
595
596/**
597 * Destroys the internal task @a hTask.
598 *
599 * @returns VBox status code.
600 * @param pVM The cross context VM structure.
601 * @param hTask Handle to the task to destroy.
602 */
603VMMR3_INT_DECL(int) PDMR3TaskDestroyInternal(PVM pVM, PDMTASKHANDLE hTask)
604{
605 return PDMR3TaskDestroySpecific(pVM, PDMTASKTYPE_INTERNAL, pVM, hTask);
606}
607
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