VirtualBox

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

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

Runteim/Aio-posix: Respect AIO_LISTIO_MAX for aio_suspend too. Should fix another darwin issue

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.0 KB
Line 
1/* $Id: fileaio-posix.cpp 23396 2009-09-28 17:25:54Z 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 Assert(rcPosix != 0);
664
665 if (rcPosix != EINPROGRESS)
666 {
667 cReqsSubmit--;
668
669 if (rcPosix == EINVAL)
670 {
671 /* Was not submitted. */
672 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
673 }
674 else
675 {
676 /* An error occurred. */
677 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
678
679 /*
680 * Looks like Apple and glibc interpret the standard in different ways.
681 * glibc returns the error code which would be in errno but Apple returns
682 * -1 and sets errno to the appropriate value
683 */
684#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
685 Assert(rcPosix == -1);
686 pReqInt->Rc = RTErrConvertFromErrno(errno);
687#elif defined(RT_OS_LINUX)
688 pReqInt->Rc = RTErrConvertFromErrno(rcPosix);
689#endif
690 pReqInt->cbTransfered = 0;
691 }
692 /* Unlink from the list. */
693 PRTFILEAIOREQINTERNAL pNext, pPrev;
694 pNext = pReqInt->pNext;
695 pPrev = pReqInt->pPrev;
696 if (pNext)
697 pNext->pPrev = pPrev;
698 if (pPrev)
699 pPrev->pNext = pNext;
700 else
701 pHead = pNext;
702 }
703 }
704
705 break;
706 }
707
708 ASMAtomicAddS32(&pCtxInt->cRequests, cReqsSubmit);
709 cReqs -= cReqsSubmit;
710 pahReqs += cReqsSubmit;
711 }
712
713 /*
714 * Check if we have a flush request now.
715 * If not we hit the AIO_LISTIO_MAX limit
716 * and will continue submitting requests
717 * above.
718 */
719 if (cReqs)
720 {
721 pReqInt = pahReqs[0];
722 RTFILEAIOREQ_VALID_RETURN(pReqInt);
723
724
725 if (pReqInt->fFlush)
726 {
727 /*
728 * lio_listio does not work with flush requests so
729 * we have to use aio_fsync directly.
730 */
731 rcPosix = aio_fsync(O_SYNC, &pReqInt->AioCB);
732 if (RT_UNLIKELY(rcPosix < 0))
733 {
734 rc = RTErrConvertFromErrno(errno);
735 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
736 pReqInt->Rc = rc;
737 pReqInt->cbTransfered = 0;
738
739 /* Unlink from the list. */
740 PRTFILEAIOREQINTERNAL pNext, pPrev;
741 pNext = pReqInt->pNext;
742 pPrev = pReqInt->pPrev;
743 if (pNext)
744 pNext->pPrev = pPrev;
745 if (pPrev)
746 pPrev->pNext = pNext;
747 else
748 pHead = pNext;
749 break;
750 }
751
752 ASMAtomicIncS32(&pCtxInt->cRequests);
753 cReqs--;
754 pahReqs++;
755 }
756 }
757 } while (cReqs);
758
759 if (pHead)
760 {
761 /*
762 * Forward successfully submitted requests to the thread waiting for requests.
763 * We search for a free slot first and if we don't find one
764 * we will grab the first one and append our list to the existing entries.
765 */
766 unsigned iSlot = 0;
767 while ( (iSlot < RT_ELEMENTS(pCtxInt->apReqsNewHead))
768 && !ASMAtomicCmpXchgPtr((void * volatile *)&pCtxInt->apReqsNewHead[iSlot], pHead, NULL))
769 iSlot++;
770
771 if (iSlot == RT_ELEMENTS(pCtxInt->apReqsNewHead))
772 {
773 /* Nothing found. */
774 PRTFILEAIOREQINTERNAL pOldHead = (PRTFILEAIOREQINTERNAL)ASMAtomicXchgPtr((void * volatile *)&pCtxInt->apReqsNewHead[0],
775 NULL);
776
777 /* Find the end of the current head and link the old list to the current. */
778 PRTFILEAIOREQINTERNAL pTail = pHead;
779 while (pTail->pNext)
780 pTail = pTail->pNext;
781
782 pTail->pNext = pOldHead;
783
784 ASMAtomicXchgPtr((void * volatile *)&pCtxInt->apReqsNewHead[0], pHead);
785 }
786
787 /* Set the internal wakeup flag and wakeup the thread if possible. */
788 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUpInternal, true);
789 if (!fWokenUp)
790 rtFileAioCtxWakeup(pCtxInt);
791 }
792
793 return rc;
794}
795
796
797RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, unsigned cMillisTimeout,
798 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
799{
800 int rc = VINF_SUCCESS;
801 int cRequestsCompleted = 0;
802 PRTFILEAIOCTXINTERNAL pCtxInt = (PRTFILEAIOCTXINTERNAL)hAioCtx;
803 struct timespec Timeout;
804 struct timespec *pTimeout = NULL;
805 uint64_t StartNanoTS = 0;
806
807 /* Check parameters. */
808 AssertPtrReturn(pCtxInt, VERR_INVALID_HANDLE);
809 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
810 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
811 AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
812 AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
813
814 if (RT_UNLIKELY(ASMAtomicReadS32(&pCtxInt->cRequests) == 0))
815 return VERR_FILE_AIO_NO_REQUEST;
816
817 if (cMillisTimeout != RT_INDEFINITE_WAIT)
818 {
819 Timeout.tv_sec = cMillisTimeout / 1000;
820 Timeout.tv_nsec = (cMillisTimeout % 1000) * 1000000;
821 pTimeout = &Timeout;
822 StartNanoTS = RTTimeNanoTS();
823 }
824
825 /* Wait for at least one. */
826 if (!cMinReqs)
827 cMinReqs = 1;
828
829 /* For the wakeup call. */
830 Assert(pCtxInt->hThreadWait == NIL_RTTHREAD);
831 ASMAtomicWriteHandle(&pCtxInt->hThreadWait, RTThreadSelf());
832
833 /* Update the waiting list once before we enter the loop. */
834 rc = rtFileAioCtxProcessEvents(pCtxInt);
835
836 while ( cMinReqs
837 && RT_SUCCESS_NP(rc))
838 {
839 ASMAtomicXchgBool(&pCtxInt->fWaiting, true);
840 int rcPosix = aio_suspend((const struct aiocb * const *)pCtxInt->apReqs,
841 pCtxInt->iFirstFree, pTimeout);
842 ASMAtomicXchgBool(&pCtxInt->fWaiting, false);
843 if (rcPosix < 0)
844 {
845 /* Check that this is an external wakeup event. */
846 if (errno == EINTR)
847 rc = rtFileAioCtxProcessEvents(pCtxInt);
848 else
849 rc = RTErrConvertFromErrno(errno);
850 }
851 else
852 {
853 /* Requests finished. */
854 unsigned iReqCurr = 0;
855 unsigned cDone = 0;
856
857 /* Remove completed requests from the waiting list. */
858 while ( (iReqCurr < pCtxInt->iFirstFree)
859 && (cDone < cReqs))
860 {
861 PRTFILEAIOREQINTERNAL pReq = pCtxInt->apReqs[iReqCurr];
862 int rcReq = aio_error(&pReq->AioCB);
863
864 if (rcReq != EINPROGRESS)
865 {
866 /* Completed store the return code. */
867 if (rcReq == 0)
868 {
869 pReq->Rc = VINF_SUCCESS;
870 /* Call aio_return() to free ressources. */
871 pReq->cbTransfered = aio_return(&pReq->AioCB);
872 }
873 else
874 {
875#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
876 pReq->Rc = RTErrConvertFromErrno(errno);
877#else
878 pReq->Rc = RTErrConvertFromErrno(rcReq);
879#endif
880 }
881
882 /* Mark the request as finished. */
883 RTFILEAIOREQ_SET_STATE(pReq, COMPLETED);
884 cDone++;
885
886 /* If there are other entries waiting put the head into the now free entry. */
887 if (pCtxInt->pReqsWaitHead)
888 {
889 PRTFILEAIOREQINTERNAL pReqInsert = pCtxInt->pReqsWaitHead;
890
891 pCtxInt->pReqsWaitHead = pReqInsert->pNext;
892 if (!pCtxInt->pReqsWaitHead)
893 {
894 /* List is empty now. Clear tail too. */
895 pCtxInt->pReqsWaitTail = NULL;
896 }
897
898 pReqInsert->iWaitingList = pReq->iWaitingList;
899 pCtxInt->apReqs[pReqInsert->iWaitingList] = pReqInsert;
900 iReqCurr++;
901 }
902 else
903 {
904 /*
905 * Move the last entry into the current position to avoid holes
906 * but only if it is not the last element already.
907 */
908 if (pReq->iWaitingList < pCtxInt->iFirstFree - 1)
909 {
910 pCtxInt->apReqs[pReq->iWaitingList] = pCtxInt->apReqs[--pCtxInt->iFirstFree];
911 pCtxInt->apReqs[pReq->iWaitingList]->iWaitingList = pReq->iWaitingList;
912 pCtxInt->apReqs[pCtxInt->iFirstFree] = NULL;
913 }
914 else
915 pCtxInt->iFirstFree--;
916 }
917
918 /* Put the request into the completed list. */
919 pahReqs[cRequestsCompleted++] = pReq;
920 pReq->iWaitingList = RTFILEAIOCTX_WAIT_ENTRY_INVALID;
921 }
922 else
923 iReqCurr++;
924 }
925
926 AssertMsg( (cDone <= cMinReqs)
927 && (cDone <= cReqs), ("Overflow cReqs=%u cMinReqs=%u cDone=%u\n",
928 cReqs, cMinReqs, cDone));
929 cReqs -= cDone;
930 cMinReqs -= cDone;
931 ASMAtomicSubS32(&pCtxInt->cRequests, cDone);
932
933 if ((cMillisTimeout != RT_INDEFINITE_WAIT) && (cMinReqs > 0))
934 {
935 uint64_t TimeDiff;
936
937 /* Recalculate the timeout. */
938 TimeDiff = RTTimeSystemNanoTS() - StartNanoTS;
939 Timeout.tv_sec = Timeout.tv_sec - (TimeDiff / 1000000);
940 Timeout.tv_nsec = Timeout.tv_nsec - (TimeDiff % 1000000);
941 }
942
943 /* Check for new elements. */
944 rc = rtFileAioCtxProcessEvents(pCtxInt);
945 }
946 }
947
948 *pcReqs = cRequestsCompleted;
949 Assert(pCtxInt->hThreadWait == RTThreadSelf());
950 ASMAtomicWriteHandle(&pCtxInt->hThreadWait, NIL_RTTHREAD);
951
952 return rc;
953}
954
955
956RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
957{
958 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
959 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
960
961 /** @todo r=bird: Define the protocol for how to resume work after calling
962 * this function. */
963
964 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, true);
965 if (!fWokenUp)
966 rtFileAioCtxWakeup(pCtxInt);
967
968 return VINF_SUCCESS;
969}
970
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