VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/fileaio-posix.cpp@ 22509

Last change on this file since 22509 was 20961, checked in by vboxsync, 15 years ago

fixed typo, it means occurred, not occured

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.1 KB
Line 
1/* $Id: fileaio-posix.cpp 20961 2009-06-26 08:45:18Z vboxsync $ */
2/** @file
3 * IPRT - File async I/O, native implementation for POSIX compliant host platforms.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_DIR
36#include <iprt/asm.h>
37#include <iprt/file.h>
38#include <iprt/mem.h>
39#include <iprt/assert.h>
40#include <iprt/string.h>
41#include <iprt/err.h>
42#include <iprt/log.h>
43#include <iprt/thread.h>
44#include <iprt/semaphore.h>
45#include "internal/fileaio.h"
46
47#if defined(RT_OS_DARWIN)
48# include <sys/types.h>
49# include <sys/sysctl.h> /* for sysctlbyname */
50#endif
51#include <aio.h>
52#include <errno.h>
53#include <time.h>
54
55/*
56 * Linux does not define this value.
57 * Just define it with really big
58 * value.
59 */
60#ifndef AIO_LISTIO_MAX
61# define AIO_LISTIO_MAX UINT32_MAX
62#endif
63
64/*******************************************************************************
65* Structures and Typedefs *
66*******************************************************************************/
67/**
68 * Async I/O request state.
69 */
70typedef struct RTFILEAIOREQINTERNAL
71{
72 /** The aio control block. FIRST ELEMENT! */
73 struct aiocb AioCB;
74 /** Next element in the chain. */
75 struct RTFILEAIOREQINTERNAL *pNext;
76 /** Previous element in the chain. */
77 struct RTFILEAIOREQINTERNAL *pPrev;
78 /** Current state the request is in. */
79 RTFILEAIOREQSTATE enmState;
80 /** Flag whether this is a flush request. */
81 bool fFlush;
82 /** Flag indicating if the request was canceled. */
83 volatile bool fCanceled;
84 /** Opaque user data. */
85 void *pvUser;
86 /** Number of bytes actually transfered. */
87 size_t cbTransfered;
88 /** Status code. */
89 int Rc;
90 /** Completion context we are assigned to. */
91 struct RTFILEAIOCTXINTERNAL *pCtxInt;
92 /** Entry in the waiting list the request is in. */
93 unsigned iWaitingList;
94 /** Magic value (RTFILEAIOREQ_MAGIC). */
95 uint32_t u32Magic;
96} RTFILEAIOREQINTERNAL, *PRTFILEAIOREQINTERNAL;
97
98/**
99 * Async I/O completion context state.
100 */
101typedef struct RTFILEAIOCTXINTERNAL
102{
103 /** Current number of requests active on this context. */
104 volatile int32_t cRequests;
105 /** Maximum number of requests this context can handle. */
106 uint32_t cMaxRequests;
107 /** The ID of the thread which is currently waiting for requests. */
108 volatile RTTHREAD hThreadWait;
109 /** Flag whether the thread was woken up. */
110 volatile bool fWokenUp;
111 /** Flag whether the thread is currently waiting in the syscall. */
112 volatile bool fWaiting;
113 /** Magic value (RTFILEAIOCTX_MAGIC). */
114 uint32_t u32Magic;
115 /** Flag whether the thread was woken up due to a internal event. */
116 volatile bool fWokenUpInternal;
117 /** List of new requests which needs to be inserted into apReqs by the
118 * waiting thread. */
119 volatile PRTFILEAIOREQINTERNAL apReqsNewHead[5];
120 /** Special entry for requests which are canceled. Because only one
121 * request can be canceled at a time and the thread canceling the request
122 * has to wait we need only one entry. */
123 volatile PRTFILEAIOREQINTERNAL pReqToCancel;
124 /** Event semaphore the canceling thread is waiting for completion of
125 * the operation. */
126 RTSEMEVENT SemEventCancel;
127 /** Number of elements in the waiting list. */
128 unsigned cReqsWait;
129 /** First free slot in the waiting list. */
130 unsigned iFirstFree;
131 /** List of requests we are currently waiting on.
132 * Size depends on cMaxRequests. */
133 volatile PRTFILEAIOREQINTERNAL apReqs[1];
134} RTFILEAIOCTXINTERNAL, *PRTFILEAIOCTXINTERNAL;
135
136/**
137 * Internal worker for waking up the waiting thread.
138 */
139static void rtFileAioCtxWakeup(PRTFILEAIOCTXINTERNAL pCtxInt)
140{
141 /*
142 * Read the thread handle before the status flag.
143 * If we read the handle after the flag we might
144 * end up with an invalid handle because the thread
145 * waiting in RTFileAioCtxWakeup() might get scheduled
146 * before we read the flag and returns.
147 * We can ensure that the handle is valid if fWaiting is true
148 * when reading the handle before the status flag.
149 */
150 RTTHREAD hThread;
151 ASMAtomicReadHandle(&pCtxInt->hThreadWait, &hThread);
152 bool fWaiting = ASMAtomicReadBool(&pCtxInt->fWaiting);
153 if (fWaiting)
154 {
155 /*
156 * If a thread waits the handle must be valid.
157 * It is possible that the thread returns from
158 * aio_suspend() before the signal is send.
159 * This is no problem because we already set fWokenUp
160 * to true which will let the thread return VERR_INTERRUPTED
161 * and the next call to RTFileAioCtxWait() will not
162 * return VERR_INTERRUPTED because signals are not saved
163 * and will simply vanish if the destination thread can't
164 * receive it.
165 */
166 Assert(hThread != NIL_RTTHREAD);
167 RTThreadPoke(hThread);
168 }
169}
170
171/**
172 * Internal worker processing events and inserting new requests into the waiting list.
173 */
174static int rtFileAioCtxProcessEvents(PRTFILEAIOCTXINTERNAL pCtxInt)
175{
176 int rc = VINF_SUCCESS;
177
178 /* Process new requests first. */
179 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUpInternal, false);
180 if (fWokenUp)
181 {
182 for (unsigned iSlot = 0; iSlot < RT_ELEMENTS(pCtxInt->apReqsNewHead); iSlot++)
183 {
184 PRTFILEAIOREQINTERNAL pReqHead = (PRTFILEAIOREQINTERNAL)ASMAtomicXchgPtr((void* volatile*)&pCtxInt->apReqsNewHead[iSlot],
185 NULL);
186
187 while (pReqHead)
188 {
189 pCtxInt->apReqs[pCtxInt->iFirstFree] = pReqHead;
190 pReqHead->iWaitingList = pCtxInt->iFirstFree;
191 pReqHead = pReqHead->pNext;
192
193 /* Clear pointer to next and previous element just for safety. */
194 pCtxInt->apReqs[pCtxInt->iFirstFree]->pNext = NULL;
195 pCtxInt->apReqs[pCtxInt->iFirstFree]->pPrev = NULL;
196 pCtxInt->iFirstFree++;
197 Assert(pCtxInt->iFirstFree <= pCtxInt->cMaxRequests);
198 }
199 }
200
201 /* Check if a request needs to be canceled. */
202 PRTFILEAIOREQINTERNAL pReqToCancel = (PRTFILEAIOREQINTERNAL)ASMAtomicReadPtr((void* volatile*)&pCtxInt->pReqToCancel);
203 if (pReqToCancel)
204 {
205 /* Put it out of the waiting list. */
206 pCtxInt->apReqs[pReqToCancel->iWaitingList] = pCtxInt->apReqs[--pCtxInt->iFirstFree];
207 pCtxInt->apReqs[pReqToCancel->iWaitingList]->iWaitingList = pReqToCancel->iWaitingList;
208 ASMAtomicDecS32(&pCtxInt->cRequests);
209 RTSemEventSignal(pCtxInt->SemEventCancel);
210 }
211 }
212 else
213 {
214 if (ASMAtomicXchgBool(&pCtxInt->fWokenUp, false))
215 rc = VERR_INTERRUPTED;
216 }
217
218 return rc;
219}
220
221RTR3DECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits)
222{
223 int rcBSD = 0;
224 AssertPtrReturn(pAioLimits, VERR_INVALID_POINTER);
225
226#if defined(RT_OS_DARWIN)
227 int cReqsOutstandingMax = 0;
228 size_t cbParameter = sizeof(int);
229
230 rcBSD = sysctlbyname("kern.aioprocmax", /* name */
231 &cReqsOutstandingMax, /* Where to store the old value. */
232 &cbParameter, /* Size of the memory pointed to. */
233 NULL, /* Where the new value is located. */
234 NULL); /* Where the size of the new value is stored. */
235 if (rcBSD == -1)
236 return RTErrConvertFromErrno(errno);
237
238 pAioLimits->cReqsOutstandingMax = cReqsOutstandingMax;
239 pAioLimits->cbBufferAlignment = 0;
240#else
241 pAioLimits->cReqsOutstandingMax = RTFILEAIO_UNLIMITED_REQS;
242 pAioLimits->cbBufferAlignment = 0;
243#endif
244
245 return VINF_SUCCESS;
246}
247
248RTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
249{
250 AssertPtrReturn(phReq, VERR_INVALID_POINTER);
251
252 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOREQINTERNAL));
253 if (RT_UNLIKELY(!pReqInt))
254 return VERR_NO_MEMORY;
255
256 pReqInt->pCtxInt = NULL;
257 pReqInt->u32Magic = RTFILEAIOREQ_MAGIC;
258 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
259
260 *phReq = (RTFILEAIOREQ)pReqInt;
261
262 return VINF_SUCCESS;
263}
264
265
266RTDECL(int) RTFileAioReqDestroy(RTFILEAIOREQ hReq)
267{
268 /*
269 * Validate the handle and ignore nil.
270 */
271 if (hReq == NIL_RTFILEAIOREQ)
272 return VINF_SUCCESS;
273 PRTFILEAIOREQINTERNAL pReqInt = hReq;
274 RTFILEAIOREQ_VALID_RETURN(pReqInt);
275 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
276
277 /*
278 * Trash the magic and free it.
279 */
280 ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
281 RTMemFree(pReqInt);
282 return VINF_SUCCESS;
283}
284
285/**
286 * Worker setting up the request.
287 */
288DECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
289 unsigned uTransferDirection,
290 RTFOFF off, void *pvBuf, size_t cbTransfer,
291 void *pvUser)
292{
293 /*
294 * Validate the input.
295 */
296 PRTFILEAIOREQINTERNAL pReqInt = hReq;
297 RTFILEAIOREQ_VALID_RETURN(pReqInt);
298 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
299 Assert(hFile != NIL_RTFILE);
300 AssertPtr(pvBuf);
301 Assert(off >= 0);
302 Assert(cbTransfer > 0);
303
304 memset(&pReqInt->AioCB, 0, sizeof(struct aiocb));
305 pReqInt->AioCB.aio_lio_opcode = uTransferDirection;
306 pReqInt->AioCB.aio_fildes = (int)hFile;
307 pReqInt->AioCB.aio_offset = off;
308 pReqInt->AioCB.aio_nbytes = cbTransfer;
309 pReqInt->AioCB.aio_buf = pvBuf;
310 pReqInt->pvUser = pvUser;
311 pReqInt->pCtxInt = NULL;
312 pReqInt->Rc = VERR_FILE_AIO_IN_PROGRESS;
313 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
314
315 return VINF_SUCCESS;
316}
317
318
319RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
320 void *pvBuf, size_t cbRead, void *pvUser)
321{
322 return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_READ,
323 off, pvBuf, cbRead, pvUser);
324}
325
326
327RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
328 void *pvBuf, size_t cbWrite, void *pvUser)
329{
330 return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_WRITE,
331 off, pvBuf, cbWrite, pvUser);
332}
333
334
335RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
336{
337 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)hReq;
338
339 RTFILEAIOREQ_VALID_RETURN(pReqInt);
340 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
341 Assert(hFile != NIL_RTFILE);
342
343 pReqInt->fFlush = true;
344 pReqInt->AioCB.aio_fildes = (int)hFile;
345 pReqInt->pvUser = pvUser;
346 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
347
348 return VINF_SUCCESS;
349}
350
351
352RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
353{
354 PRTFILEAIOREQINTERNAL pReqInt = hReq;
355 RTFILEAIOREQ_VALID_RETURN_RC(pReqInt, NULL);
356
357 return pReqInt->pvUser;
358}
359
360
361RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq)
362{
363 PRTFILEAIOREQINTERNAL pReqInt = hReq;
364 RTFILEAIOREQ_VALID_RETURN(pReqInt);
365 RTFILEAIOREQ_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_NOT_SUBMITTED);
366
367 ASMAtomicXchgBool(&pReqInt->fCanceled, true);
368
369 int rcPosix = aio_cancel(pReqInt->AioCB.aio_fildes, &pReqInt->AioCB);
370
371 if (rcPosix == AIO_CANCELED)
372 {
373 PRTFILEAIOCTXINTERNAL pCtxInt = pReqInt->pCtxInt;
374 /*
375 * Notify the waiting thread that the request was canceled.
376 */
377 AssertMsg(VALID_PTR(pCtxInt),
378 ("Invalid state. Request was canceled but wasn't submitted\n"));
379
380 Assert(!pCtxInt->pReqToCancel);
381 ASMAtomicWritePtr((void* volatile*)&pCtxInt->pReqToCancel, pReqInt);
382 rtFileAioCtxWakeup(pCtxInt);
383
384 /* Wait for acknowledge. */
385 int rc = RTSemEventWait(pCtxInt->SemEventCancel, RT_INDEFINITE_WAIT);
386 AssertRC(rc);
387
388 ASMAtomicWritePtr((void* volatile*)&pCtxInt->pReqToCancel, NULL);
389 pReqInt->Rc = VERR_FILE_AIO_CANCELED;
390 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
391 return VINF_SUCCESS;
392 }
393 else if (rcPosix == AIO_ALLDONE)
394 return VERR_FILE_AIO_COMPLETED;
395 else if (rcPosix == AIO_NOTCANCELED)
396 return VERR_FILE_AIO_IN_PROGRESS;
397 else
398 return RTErrConvertFromErrno(errno);
399}
400
401
402RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
403{
404 PRTFILEAIOREQINTERNAL pReqInt = hReq;
405 RTFILEAIOREQ_VALID_RETURN(pReqInt);
406 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
407 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, PREPARED, VERR_FILE_AIO_NOT_SUBMITTED);
408 AssertPtrNull(pcbTransfered);
409
410 if ( (RT_SUCCESS(pReqInt->Rc))
411 && (pcbTransfered))
412 *pcbTransfered = pReqInt->cbTransfered;
413
414 return pReqInt->Rc;
415}
416
417
418RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax)
419{
420 PRTFILEAIOCTXINTERNAL pCtxInt;
421 AssertPtrReturn(phAioCtx, VERR_INVALID_POINTER);
422
423 pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ( sizeof(RTFILEAIOCTXINTERNAL)
424 + cAioReqsMax * sizeof(PRTFILEAIOREQINTERNAL));
425 if (RT_UNLIKELY(!pCtxInt))
426 return VERR_NO_MEMORY;
427
428 /* Create event semaphore. */
429 int rc = RTSemEventCreate(&pCtxInt->SemEventCancel);
430 if (RT_FAILURE(rc))
431 {
432 RTMemFree(pCtxInt);
433 return rc;
434 }
435
436 pCtxInt->u32Magic = RTFILEAIOCTX_MAGIC;
437 pCtxInt->cMaxRequests = cAioReqsMax;
438 *phAioCtx = (RTFILEAIOCTX)pCtxInt;
439
440 return VINF_SUCCESS;
441}
442
443
444RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
445{
446 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
447
448 AssertPtrReturn(pCtxInt, VERR_INVALID_HANDLE);
449
450 if (RT_UNLIKELY(pCtxInt->cRequests))
451 return VERR_FILE_AIO_BUSY;
452
453 RTSemEventDestroy(pCtxInt->SemEventCancel);
454 RTMemFree(pCtxInt);
455
456 return VINF_SUCCESS;
457}
458
459
460RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
461{
462 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
463
464 if (hAioCtx == NIL_RTFILEAIOCTX)
465 return RTFILEAIO_UNLIMITED_REQS;
466 else
467 return pCtxInt->cMaxRequests;
468}
469
470RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile)
471{
472 return VINF_SUCCESS;
473}
474
475RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
476{
477 int rc = VINF_SUCCESS;
478 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
479
480 /* Parameter checks */
481 AssertPtrReturn(pCtxInt, VERR_INVALID_HANDLE);
482 AssertReturn(cReqs != 0, VERR_INVALID_POINTER);
483 AssertPtrReturn(pahReqs, VERR_INVALID_PARAMETER);
484
485 /* Check that we don't exceed the limit */
486 if (ASMAtomicUoReadS32(&pCtxInt->cRequests) + cReqs > pCtxInt->cMaxRequests)
487 return VERR_FILE_AIO_LIMIT_EXCEEDED;
488
489 PRTFILEAIOREQINTERNAL pHead = NULL;
490
491 do
492 {
493 int rcPosix = 0;
494 size_t cReqsSubmit = 0;
495 size_t i = 0;
496 PRTFILEAIOREQINTERNAL pReqInt;
497
498 while ( (i < cReqs)
499 && (i < AIO_LISTIO_MAX))
500 {
501 pReqInt = pahReqs[i];
502 if (RTFILEAIOREQ_IS_NOT_VALID(pReqInt))
503 {
504 /* Undo everything and stop submitting. */
505 for (size_t iUndo = 0; iUndo < i; iUndo++)
506 {
507 pReqInt = pahReqs[iUndo];
508 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
509 pReqInt->pCtxInt = NULL;
510
511 /* Unlink from the list again. */
512 PRTFILEAIOREQINTERNAL pNext, pPrev;
513 pNext = pReqInt->pNext;
514 pPrev = pReqInt->pPrev;
515 if (pNext)
516 pNext->pPrev = pPrev;
517 if (pPrev)
518 pPrev->pNext = pNext;
519 else
520 pHead = pNext;
521 }
522 rc = VERR_INVALID_HANDLE;
523 break;
524 }
525
526 pReqInt->pCtxInt = pCtxInt;
527
528 /* Link them together. */
529 pReqInt->pNext = pHead;
530 if (pHead)
531 pHead->pPrev = pReqInt;
532 pReqInt->pPrev = NULL;
533 pHead = pReqInt;
534 RTFILEAIOREQ_SET_STATE(pReqInt, SUBMITTED);
535
536 if (pReqInt->fFlush)
537 break;
538
539 cReqsSubmit++;
540 i++;
541 }
542
543 if (cReqsSubmit)
544 {
545 rcPosix = lio_listio(LIO_NOWAIT, (struct aiocb **)pahReqs, cReqsSubmit, NULL);
546 if (RT_UNLIKELY(rcPosix < 0))
547 {
548 if (rcPosix == EAGAIN)
549 rc = VERR_FILE_AIO_INSUFFICIENT_RESSOURCES;
550 else
551 rc = RTErrConvertFromErrno(errno);
552
553 /* Check which ones were not submitted. */
554 for (i = 0; i < cReqs; i++)
555 {
556 pReqInt = pahReqs[i];
557 rcPosix = aio_error(&pReqInt->AioCB);
558 if (rcPosix != EINPROGRESS)
559 {
560 if (rcPosix == EINVAL)
561 {
562 /* Was not submitted. */
563 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
564 }
565 else
566 {
567 /* An error occurred. */
568 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
569 pReqInt->Rc = RTErrConvertFromErrno(rcPosix);
570 pReqInt->cbTransfered = 0;
571 }
572 /* Unlink from the list. */
573 PRTFILEAIOREQINTERNAL pNext, pPrev;
574 pNext = pReqInt->pNext;
575 pPrev = pReqInt->pPrev;
576 if (pNext)
577 pNext->pPrev = pPrev;
578 if (pPrev)
579 pPrev->pNext = pNext;
580 else
581 pHead = pNext;
582 }
583 }
584
585 break;
586 }
587
588 ASMAtomicAddS32(&pCtxInt->cRequests, cReqsSubmit);
589 cReqs -= cReqsSubmit;
590 pahReqs += cReqsSubmit;
591 }
592
593 /* Check if we have a flush request now. */
594 if (cReqs)
595 {
596 pReqInt = pahReqs[0];
597 RTFILEAIOREQ_VALID_RETURN(pReqInt);
598
599 Assert(pReqInt->fFlush);
600
601 /*
602 * lio_listio does not work with flush requests so
603 * we have to use aio_fsync directly.
604 */
605 rcPosix = aio_fsync(O_SYNC, &pReqInt->AioCB);
606 if (RT_UNLIKELY(rcPosix < 0))
607 {
608 rc = RTErrConvertFromErrno(errno);
609 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
610 pReqInt->Rc = rc;
611 pReqInt->cbTransfered = 0;
612
613 /* Unlink from the list. */
614 PRTFILEAIOREQINTERNAL pNext, pPrev;
615 pNext = pReqInt->pNext;
616 pPrev = pReqInt->pPrev;
617 if (pNext)
618 pNext->pPrev = pPrev;
619 if (pPrev)
620 pPrev->pNext = pNext;
621 else
622 pHead = pNext;
623 break;
624 }
625
626 ASMAtomicIncS32(&pCtxInt->cRequests);
627 cReqs--;
628 pahReqs++;
629 }
630 } while (cReqs);
631
632 if (pHead)
633 {
634 /*
635 * Forward successfully submitted requests to the thread waiting for requests.
636 * We search for a free slot first and if we don't find one
637 * we will grab the first one and append our list to the existing entries.
638 */
639 unsigned iSlot = 0;
640 while ( (iSlot < RT_ELEMENTS(pCtxInt->apReqsNewHead))
641 && !ASMAtomicCmpXchgPtr((void * volatile *)&pCtxInt->apReqsNewHead[iSlot], pHead, NULL))
642 iSlot++;
643
644 if (iSlot == RT_ELEMENTS(pCtxInt->apReqsNewHead))
645 {
646 /* Nothing found. */
647 PRTFILEAIOREQINTERNAL pOldHead = (PRTFILEAIOREQINTERNAL)ASMAtomicXchgPtr((void * volatile *)&pCtxInt->apReqsNewHead[0],
648 NULL);
649
650 /* Find the end of the current head and link the old list to the current. */
651 PRTFILEAIOREQINTERNAL pTail = pHead;
652 while (pTail->pNext)
653 pTail = pTail->pNext;
654
655 pTail->pNext = pOldHead;
656
657 ASMAtomicXchgPtr((void * volatile *)&pCtxInt->apReqsNewHead[0], pHead);
658 }
659
660 /* Set the internal wakeup flag and wakeup the thread if possible. */
661 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUpInternal, true);
662 if (!fWokenUp)
663 rtFileAioCtxWakeup(pCtxInt);
664 }
665
666 return rc;
667}
668
669
670RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, unsigned cMillisTimeout,
671 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
672{
673 int rc = VINF_SUCCESS;
674 int cRequestsCompleted = 0;
675 PRTFILEAIOCTXINTERNAL pCtxInt = (PRTFILEAIOCTXINTERNAL)hAioCtx;
676 struct timespec Timeout;
677 struct timespec *pTimeout = NULL;
678 uint64_t StartNanoTS = 0;
679
680 /* Check parameters. */
681 AssertPtrReturn(pCtxInt, VERR_INVALID_HANDLE);
682 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
683 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
684 AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
685 AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
686
687 if (RT_UNLIKELY(ASMAtomicReadS32(&pCtxInt->cRequests) == 0))
688 return VERR_FILE_AIO_NO_REQUEST;
689
690 if (cMillisTimeout != RT_INDEFINITE_WAIT)
691 {
692 Timeout.tv_sec = cMillisTimeout / 1000;
693 Timeout.tv_nsec = (cMillisTimeout % 1000) * 1000000;
694 pTimeout = &Timeout;
695 StartNanoTS = RTTimeNanoTS();
696 }
697
698 /* Wait for at least one. */
699 if (!cMinReqs)
700 cMinReqs = 1;
701
702 /* For the wakeup call. */
703 Assert(pCtxInt->hThreadWait == NIL_RTTHREAD);
704 ASMAtomicWriteHandle(&pCtxInt->hThreadWait, RTThreadSelf());
705
706 /* Update the waiting list once before we enter the loop. */
707 rc = rtFileAioCtxProcessEvents(pCtxInt);
708
709 while ( cMinReqs
710 && RT_SUCCESS_NP(rc))
711 {
712 ASMAtomicXchgBool(&pCtxInt->fWaiting, true);
713 int rcPosix = aio_suspend((const struct aiocb * const *)pCtxInt->apReqs,
714 pCtxInt->iFirstFree, pTimeout);
715 ASMAtomicXchgBool(&pCtxInt->fWaiting, false);
716 if (rcPosix < 0)
717 {
718 /* Check that this is an external wakeup event. */
719 if (errno == EINTR)
720 rc = rtFileAioCtxProcessEvents(pCtxInt);
721 else
722 rc = RTErrConvertFromErrno(errno);
723 }
724 else
725 {
726 /* Requests finished. */
727 unsigned iReqCurr = 0;
728 int cDone = 0;
729
730 /* Remove completed requests from the waiting list. */
731 while (iReqCurr < pCtxInt->iFirstFree)
732 {
733 PRTFILEAIOREQINTERNAL pReq = pCtxInt->apReqs[iReqCurr];
734 int rcReq = aio_error(&pReq->AioCB);
735
736 if (rcReq != EINPROGRESS)
737 {
738 /* Completed store the return code. */
739 if (rcReq == 0)
740 {
741 pReq->Rc = VINF_SUCCESS;
742 /* Call aio_return() to free ressources. */
743 pReq->cbTransfered = aio_return(&pReq->AioCB);
744 }
745 else
746 pReq->Rc = RTErrConvertFromErrno(rcReq);
747
748 /* Mark the request as finished. */
749 RTFILEAIOREQ_SET_STATE(pReq, COMPLETED);
750 cDone++;
751
752 /*
753 * Move the last entry into the current position to avoid holes
754 * but only if it is not the last element already.
755 */
756 if (pReq->iWaitingList < pCtxInt->iFirstFree - 1)
757 {
758 pCtxInt->apReqs[pReq->iWaitingList] = pCtxInt->apReqs[--pCtxInt->iFirstFree];
759 pCtxInt->apReqs[pReq->iWaitingList]->iWaitingList = pReq->iWaitingList;
760 pCtxInt->apReqs[pCtxInt->iFirstFree] = NULL;
761 }
762 else
763 pCtxInt->iFirstFree--;
764
765 /* Put the request into the completed list. */
766 pahReqs[cRequestsCompleted++] = pReq;
767 }
768 else
769 iReqCurr++;
770 }
771
772 cReqs -= cDone;
773 cMinReqs -= cDone;
774 ASMAtomicSubS32(&pCtxInt->cRequests, cDone);
775
776 if ((cMillisTimeout != RT_INDEFINITE_WAIT) && (cMinReqs > 0))
777 {
778 uint64_t TimeDiff;
779
780 /* Recalculate the timeout. */
781 TimeDiff = RTTimeSystemNanoTS() - StartNanoTS;
782 Timeout.tv_sec = Timeout.tv_sec - (TimeDiff / 1000000);
783 Timeout.tv_nsec = Timeout.tv_nsec - (TimeDiff % 1000000);
784 }
785
786 /* Check for new elements. */
787 rc = rtFileAioCtxProcessEvents(pCtxInt);
788 }
789 }
790
791 *pcReqs = cRequestsCompleted;
792 Assert(pCtxInt->hThreadWait == RTThreadSelf());
793 ASMAtomicWriteHandle(&pCtxInt->hThreadWait, NIL_RTTHREAD);
794
795 return rc;
796}
797
798
799RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
800{
801 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
802 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
803
804 /** @todo r=bird: Define the protocol for how to resume work after calling
805 * this function. */
806
807 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, true);
808 if (!fWokenUp)
809 rtFileAioCtxWakeup(pCtxInt);
810
811 return VINF_SUCCESS;
812}
813
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