VirtualBox

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

Last change on this file since 23601 was 23411, checked in by vboxsync, 15 years ago

Runtime/Aio-posix: stricter checking

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