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