forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait.cpp
More file actions
677 lines (575 loc) · 20.1 KB
/
wait.cpp
File metadata and controls
677 lines (575 loc) · 20.1 KB
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
wait.cpp
Abstract:
Implementation of waiting functions as described in
the WIN32 API
Revision History:
--*/
#include "pal/thread.hpp"
#include "pal/synchobjects.hpp"
#include "pal/malloc.hpp"
#include "pal/dbgmsg.h"
SET_DEFAULT_DEBUG_CHANNEL(SYNC);
#define MAXIMUM_STACK_WAITOBJ_ARRAY_SIZE (MAXIMUM_WAIT_OBJECTS / 4)
using namespace CorUnix;
static PalObjectTypeId sg_rgWaitObjectsIds[] =
{
otiAutoResetEvent,
otiManualResetEvent,
otiMutex,
otiSemaphore,
otiProcess,
otiThread
};
static CAllowedObjectTypes sg_aotWaitObject(sg_rgWaitObjectsIds,
sizeof(sg_rgWaitObjectsIds)/sizeof(sg_rgWaitObjectsIds[0]));
/*++
Function:
WaitForSingleObject
See MSDN doc.
--*/
DWORD
PALAPI
WaitForSingleObject(IN HANDLE hHandle,
IN DWORD dwMilliseconds)
{
DWORD dwRet;
PERF_ENTRY(WaitForSingleObject);
ENTRY("WaitForSingleObject(hHandle=%p, dwMilliseconds=%u)\n",
hHandle, dwMilliseconds);
CPalThread * pThread = InternalGetCurrentThread();
dwRet = InternalWaitForMultipleObjectsEx(pThread, 1, &hHandle, FALSE,
dwMilliseconds, FALSE);
LOGEXIT("WaitForSingleObject returns DWORD %u\n", dwRet);
PERF_EXIT(WaitForSingleObject);
return dwRet;
}
/*++
Function:
WaitForSingleObjectEx
See MSDN doc.
--*/
DWORD
PALAPI
WaitForSingleObjectEx(IN HANDLE hHandle,
IN DWORD dwMilliseconds,
IN BOOL bAlertable)
{
DWORD dwRet;
PERF_ENTRY(WaitForSingleObjectEx);
ENTRY("WaitForSingleObjectEx(hHandle=%p, dwMilliseconds=%u, bAlertable=%s)\n",
hHandle, dwMilliseconds, bAlertable ? "TRUE" : "FALSE");
CPalThread * pThread = InternalGetCurrentThread();
dwRet = InternalWaitForMultipleObjectsEx(pThread, 1, &hHandle, FALSE,
dwMilliseconds, bAlertable);
LOGEXIT("WaitForSingleObjectEx returns DWORD %u\n", dwRet);
PERF_EXIT(WaitForSingleObjectEx);
return dwRet;
}
/*++
Function:
WaitForMultipleObjects
See MSDN doc.
--*/
DWORD
PALAPI
WaitForMultipleObjects(IN DWORD nCount,
IN CONST HANDLE *lpHandles,
IN BOOL bWaitAll,
IN DWORD dwMilliseconds)
{
DWORD dwRet;
PERF_ENTRY(WaitForMultipleObjects);
ENTRY("WaitForMultipleObjects(nCount=%d, lpHandles=%p,"
" bWaitAll=%d, dwMilliseconds=%u)\n",
nCount, lpHandles, bWaitAll, dwMilliseconds);
CPalThread * pThread = InternalGetCurrentThread();
dwRet = InternalWaitForMultipleObjectsEx(pThread, nCount, lpHandles,
bWaitAll, dwMilliseconds, FALSE);
LOGEXIT("WaitForMultipleObjects returns DWORD %u\n", dwRet);
PERF_EXIT(WaitForMultipleObjects);
return dwRet;
}
/*++
Function:
WaitForMultipleObjectsEx
See MSDN doc for info about this function.
--*/
DWORD
PALAPI
WaitForMultipleObjectsEx(IN DWORD nCount,
IN CONST HANDLE *lpHandles,
IN BOOL bWaitAll,
IN DWORD dwMilliseconds,
IN BOOL bAlertable)
{
DWORD dwRet;
PERF_ENTRY(WaitForMultipleObjectsEx);
ENTRY("WaitForMultipleObjectsEx(nCount=%d, lpHandles=%p,"
" bWaitAll=%d, dwMilliseconds=%u, bAlertable=d)\n",
nCount, lpHandles, bWaitAll, dwMilliseconds, bAlertable);
CPalThread * pThread = InternalGetCurrentThread();
dwRet = InternalWaitForMultipleObjectsEx(pThread, nCount, lpHandles, bWaitAll,
dwMilliseconds, bAlertable);
LOGEXIT("WaitForMultipleObjectsEx returns DWORD %u\n", dwRet);
PERF_EXIT(WaitForMultipleObjectsEx);
return dwRet;
}
/*++
Function:
Sleep
See MSDN doc.
--*/
VOID
PALAPI
Sleep(IN DWORD dwMilliseconds)
{
PERF_ENTRY(Sleep);
ENTRY("Sleep(dwMilliseconds=%u)\n", dwMilliseconds);
CPalThread * pThread = InternalGetCurrentThread();
PAL_ERROR palErr = InternalSleepEx(pThread, dwMilliseconds, FALSE);
if (NO_ERROR != palErr)
{
ERROR("Sleep(dwMilliseconds=%u) failed [error=%u]\n",
dwMilliseconds, palErr);
pThread->SetLastError(palErr);
}
LOGEXIT("Sleep returns VOID\n");
PERF_EXIT(Sleep);
}
/*++
Function:
SleepEx
See MSDN doc.
--*/
DWORD
PALAPI
SleepEx(IN DWORD dwMilliseconds,
IN BOOL bAlertable)
{
DWORD dwRet;
PERF_ENTRY(SleepEx);
ENTRY("SleepEx(dwMilliseconds=%u, bAlertable=%d)\n", dwMilliseconds, bAlertable);
CPalThread * pThread = InternalGetCurrentThread();
dwRet = InternalSleepEx(pThread, dwMilliseconds, bAlertable);
LOGEXIT("SleepEx returns DWORD %u\n", dwRet);
PERF_EXIT(SleepEx);
return dwRet;
}
/*++
Function:
QueueUserAPC
See MSDN doc.
--*/
DWORD
PALAPI
QueueUserAPC(
PAPCFUNC pfnAPC,
HANDLE hThread,
ULONG_PTR dwData)
{
CPalThread * pCurrentThread = NULL;
CPalThread * pTargetThread = NULL;
IPalObject * pTargetThreadObject = NULL;
PAL_ERROR palErr;
DWORD dwRet;
PERF_ENTRY(QueueUserAPC);
ENTRY("QueueUserAPC(pfnAPC=%p, hThread=%p, dwData=%#x)\n",
pfnAPC, hThread, dwData);
/* NOTE: Windows does not check the validity of pfnAPC, even if it is
NULL. It just does an access violation later on when the APC call
is attempted */
pCurrentThread = InternalGetCurrentThread();
palErr = InternalGetThreadDataFromHandle(
pCurrentThread,
hThread,
0, // THREAD_SET_CONTEXT
&pTargetThread,
&pTargetThreadObject
);
if (NO_ERROR != palErr)
{
ERROR("Unable to obtain thread data for handle %p (error %x)!\n",
hThread, palErr);
goto QueueUserAPC_exit;
}
palErr = g_pSynchronizationManager->QueueUserAPC(pCurrentThread, pTargetThread,
pfnAPC, dwData);
QueueUserAPC_exit:
if (pTargetThreadObject)
{
pTargetThreadObject->ReleaseReference(pCurrentThread);
}
dwRet = (NO_ERROR == palErr) ? 1 : 0;
LOGEXIT("QueueUserAPC returns DWORD %d\n", dwRet);
PERF_EXIT(QueueUserAPC);
return dwRet;
}
DWORD CorUnix::InternalWaitForMultipleObjectsEx(
CPalThread * pThread,
DWORD nCount,
CONST HANDLE *lpHandles,
BOOL bWaitAll,
DWORD dwMilliseconds,
BOOL bAlertable)
{
DWORD dwRet = WAIT_FAILED;
PAL_ERROR palErr = NO_ERROR;
int i, iSignaledObjCount, iSignaledObjIndex = -1;
bool fWAll = (bool)bWaitAll, fNeedToBlock = false;
bool fAbandoned = false;
WaitType wtWaitType;
IPalObject * pIPalObjStackArray[MAXIMUM_STACK_WAITOBJ_ARRAY_SIZE] = { NULL };
ISynchWaitController * pISyncStackArray[MAXIMUM_STACK_WAITOBJ_ARRAY_SIZE] = { NULL };
IPalObject ** ppIPalObjs = pIPalObjStackArray;
ISynchWaitController ** ppISyncWaitCtrlrs = pISyncStackArray;
if ((nCount == 0) || (nCount > MAXIMUM_WAIT_OBJECTS))
{
ppIPalObjs = NULL; // make delete at the end safe
ppISyncWaitCtrlrs = NULL; // make delete at the end safe
ERROR("Invalid object count=%d [range: 1 to %d]\n",
nCount, MAXIMUM_WAIT_OBJECTS)
pThread->SetLastError(ERROR_INVALID_PARAMETER);
goto WFMOExIntExit;
}
else if (nCount == 1)
{
fWAll = false; // makes no difference when nCount is 1
wtWaitType = SingleObject;
}
else
{
wtWaitType = fWAll ? MultipleObjectsWaitAll : MultipleObjectsWaitOne;
if (nCount > MAXIMUM_STACK_WAITOBJ_ARRAY_SIZE)
{
ppIPalObjs = InternalNewArray<IPalObject*>(nCount);
ppISyncWaitCtrlrs = InternalNewArray<ISynchWaitController*>(nCount);
if ((NULL == ppIPalObjs) || (NULL == ppISyncWaitCtrlrs))
{
ERROR("Out of memory allocating internal structures\n");
pThread->SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto WFMOExIntExit;
}
}
}
palErr = g_pObjectManager->ReferenceMultipleObjectsByHandleArray(pThread,
(VOID **)lpHandles,
nCount,
&sg_aotWaitObject,
SYNCHRONIZE,
ppIPalObjs);
if (NO_ERROR != palErr)
{
ERROR("Unable to obtain object for some or all of the handles [error=%u]\n",
palErr);
if (palErr == ERROR_INVALID_HANDLE)
pThread->SetLastError(ERROR_INVALID_HANDLE);
else
pThread->SetLastError(ERROR_INTERNAL_ERROR);
goto WFMOExIntExit;
}
if (fWAll)
{
// For a wait-all operation, check for duplicate wait objects in the array. This just uses a brute-force O(n^2)
// algorithm, but since MAXIMUM_WAIT_OBJECTS is small, the worst case is not so bad, and the average case would involve
// significantly fewer items.
for (DWORD i = 0; i < nCount - 1; ++i)
{
IPalObject *const objectToCheck = ppIPalObjs[i];
for (DWORD j = i + 1; j < nCount; ++j)
{
if (ppIPalObjs[j] == objectToCheck)
{
ERROR("Duplicate handle provided for a wait-all operation [error=%u]\n", ERROR_INVALID_PARAMETER);
pThread->SetLastError(ERROR_INVALID_PARAMETER);
goto WFMOExIntCleanup;
}
}
}
}
palErr = g_pSynchronizationManager->GetSynchWaitControllersForObjects(
pThread, ppIPalObjs, nCount, ppISyncWaitCtrlrs);
if (NO_ERROR != palErr)
{
ERROR("Unable to obtain ISynchWaitController interface for some or all "
"of the objects [error=%u]\n", palErr);
pThread->SetLastError(ERROR_INTERNAL_ERROR);
goto WFMOExIntCleanup;
}
if (bAlertable)
{
// First check for pending APC. We need to do that while holding the global
// synch lock implicitely grabbed by GetSynchWaitControllersForObjects
if (g_pSynchronizationManager->AreAPCsPending(pThread))
{
// If there is any pending APC we need to release the
// implicit global synch lock before calling into it
for (i = 0; (i < (int)nCount) && (NULL != ppISyncWaitCtrlrs[i]); i++)
{
ppISyncWaitCtrlrs[i]->ReleaseController();
ppISyncWaitCtrlrs[i] = NULL;
}
palErr = g_pSynchronizationManager->DispatchPendingAPCs(pThread);
if (NO_ERROR == palErr)
{
dwRet = WAIT_IO_COMPLETION;
}
else
{
ASSERT("Awakened for APC, but no APC is pending\n");
pThread->SetLastError(ERROR_INTERNAL_ERROR);
dwRet = WAIT_FAILED;
}
goto WFMOExIntCleanup;
}
}
iSignaledObjCount = 0;
iSignaledObjIndex = -1;
for (i=0;i<(int)nCount;i++)
{
bool fValue;
palErr = ppISyncWaitCtrlrs[i]->CanThreadWaitWithoutBlocking(&fValue, &fAbandoned);
if (NO_ERROR != palErr)
{
ERROR("ISynchWaitController::CanThreadWaitWithoutBlocking() failed for "
"%d-th object [handle=%p error=%u]\n", i, lpHandles[i], palErr);
pThread->SetLastError(ERROR_INTERNAL_ERROR);
goto WFMOExIntReleaseControllers;
}
if (fValue)
{
iSignaledObjCount++;
iSignaledObjIndex = i;
if (!fWAll)
break;
}
}
fNeedToBlock = (iSignaledObjCount == 0) || (fWAll && (iSignaledObjCount < (int)nCount));
if (!fNeedToBlock)
{
// At least one object signaled, or bWaitAll==TRUE and all object signaled.
// No need to wait, let's unsignal the object(s) and return without blocking
int iStartIdx, iEndIdx;
if (fWAll)
{
iStartIdx = 0;
iEndIdx = nCount;
}
else
{
iStartIdx = iSignaledObjIndex;
iEndIdx = iStartIdx + 1;
}
// Unsignal objects
if( iStartIdx < 0 )
{
ERROR("Buffer underflow due to iStartIdx < 0");
pThread->SetLastError(ERROR_INTERNAL_ERROR);
dwRet = WAIT_FAILED;
goto WFMOExIntCleanup;
}
for (i = iStartIdx; i < iEndIdx; i++)
{
palErr = ppISyncWaitCtrlrs[i]->ReleaseWaitingThreadWithoutBlocking();
if (NO_ERROR != palErr)
{
ERROR("ReleaseWaitingThreadWithoutBlocking() failed for %d-th "
"object [handle=%p error=%u]\n",
i, lpHandles[i], palErr);
pThread->SetLastError(palErr);
goto WFMOExIntReleaseControllers;
}
}
dwRet = (fAbandoned ? WAIT_ABANDONED_0 : WAIT_OBJECT_0);
}
else if (0 == dwMilliseconds)
{
// Not enough objects signaled, but timeout is zero: no actual wait
dwRet = WAIT_TIMEOUT;
fNeedToBlock = false;
}
else
{
// Register the thread for waiting on all objects
for (i=0;i<(int)nCount;i++)
{
palErr = ppISyncWaitCtrlrs[i]->RegisterWaitingThread(
wtWaitType,
i,
(TRUE == bAlertable));
if (NO_ERROR != palErr)
{
ERROR("RegisterWaitingThread() failed for %d-th object "
"[handle=%p error=%u]\n", i, lpHandles[i], palErr);
pThread->SetLastError(palErr);
goto WFMOExIntReleaseControllers;
}
}
}
WFMOExIntReleaseControllers:
// Release all controllers before going to sleep
for (i = 0; i < (int)nCount; i++)
{
ppISyncWaitCtrlrs[i]->ReleaseController();
ppISyncWaitCtrlrs[i] = NULL;
}
if (NO_ERROR != palErr)
goto WFMOExIntCleanup;
if (fNeedToBlock)
{
ThreadWakeupReason twrWakeupReason;
//
// Going to sleep
//
palErr = g_pSynchronizationManager->BlockThread(pThread,
dwMilliseconds,
(TRUE == bAlertable),
false,
&twrWakeupReason,
(DWORD *)&iSignaledObjIndex);
//
// Awakened
//
if (NO_ERROR != palErr)
{
ERROR("IPalSynchronizationManager::BlockThread failed for thread "
"pThread=%p [error=%u]\n", pThread, palErr);
pThread->SetLastError(palErr);
goto WFMOExIntCleanup;
}
switch (twrWakeupReason)
{
case WaitSucceeded:
dwRet = WAIT_OBJECT_0; // offset added later
break;
case MutexAbondoned:
dwRet = WAIT_ABANDONED_0; // offset added later
break;
case WaitTimeout:
dwRet = WAIT_TIMEOUT;
break;
case Alerted:
_ASSERT_MSG(bAlertable,
"Awakened for APC from a non-alertable wait\n");
dwRet = WAIT_IO_COMPLETION;
palErr = g_pSynchronizationManager->DispatchPendingAPCs(pThread);
_ASSERT_MSG(NO_ERROR == palErr,
"Awakened for APC, but no APC is pending\n");
break;
case WaitFailed:
default:
ERROR("Thread %p awakened with some failure\n", pThread);
dwRet = WAIT_FAILED;
break;
}
}
if (!fWAll && ((WAIT_OBJECT_0 == dwRet) || (WAIT_ABANDONED_0 == dwRet)))
{
_ASSERT_MSG(0 <= iSignaledObjIndex,
"Failed to identify signaled/abandoned object\n");
_ASSERT_MSG(iSignaledObjIndex >= 0 && nCount > static_cast<DWORD>(iSignaledObjIndex),
"SignaledObjIndex object out of range "
"[index=%d obj_count=%u\n",
iSignaledObjCount, nCount);
if (iSignaledObjIndex < 0)
{
pThread->SetLastError(ERROR_INTERNAL_ERROR);
dwRet = WAIT_FAILED;
goto WFMOExIntCleanup;
}
dwRet += iSignaledObjIndex;
}
WFMOExIntCleanup:
for (i = 0; i < (int)nCount; i++)
{
ppIPalObjs[i]->ReleaseReference(pThread);
ppIPalObjs[i] = NULL;
}
WFMOExIntExit:
if (nCount > MAXIMUM_STACK_WAITOBJ_ARRAY_SIZE)
{
InternalDeleteArray(ppIPalObjs);
InternalDeleteArray(ppISyncWaitCtrlrs);
}
return dwRet;
}
PAL_ERROR CorUnix::InternalSleepEx (
CPalThread * pThread,
DWORD dwMilliseconds,
BOOL bAlertable)
{
PAL_ERROR palErr = NO_ERROR;
DWORD dwRet = WAIT_FAILED;
int iSignaledObjIndex;
TRACE("Sleeping %u ms [bAlertable=%d]", dwMilliseconds, (int)bAlertable);
if (bAlertable)
{
// In this case do not use AreAPCsPending. In fact, since we are
// not holding the synch lock(s) an APC posting may race with
// AreAPCsPending.
palErr = g_pSynchronizationManager->DispatchPendingAPCs(pThread);
if (NO_ERROR == palErr)
{
dwRet = WAIT_IO_COMPLETION;
goto InternalSleepExExit;
}
else if (ERROR_NOT_FOUND == palErr)
{
// No APC was pending, just continue
palErr = NO_ERROR;
}
}
if (dwMilliseconds > 0)
{
ThreadWakeupReason twrWakeupReason;
palErr = g_pSynchronizationManager->BlockThread(pThread,
dwMilliseconds,
(TRUE == bAlertable),
true,
&twrWakeupReason,
(DWORD *)&iSignaledObjIndex);
if (NO_ERROR != palErr)
{
ERROR("IPalSynchronizationManager::BlockThread failed for thread "
"pThread=%p [error=%u]\n", pThread, palErr);
goto InternalSleepExExit;
}
switch (twrWakeupReason)
{
case WaitSucceeded:
case WaitTimeout:
dwRet = 0;
break;
case Alerted:
_ASSERT_MSG(bAlertable,
"Awakened for APC from a non-alertable wait\n");
dwRet = WAIT_IO_COMPLETION;
palErr = g_pSynchronizationManager->DispatchPendingAPCs(pThread);
_ASSERT_MSG(NO_ERROR == palErr,
"Awakened for APC, but no APC is pending\n");
break;
case MutexAbondoned:
ASSERT("Thread %p awakened with reason=MutexAbondoned from a "
"SleepEx\n", pThread);
palErr = ERROR_INTERNAL_ERROR;
break;
case WaitFailed:
default:
ERROR("Thread %p awakened with some failure\n", pThread);
palErr = ERROR_INTERNAL_ERROR;
break;
}
}
else
{
dwRet = 0;
}
TRACE("Done sleeping %u ms [bAlertable=%d]", dwMilliseconds, (int)bAlertable);
InternalSleepExExit:
return dwRet;
}