VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/fileaio-linux.cpp@ 19346

Last change on this file since 19346 was 19346, checked in by vboxsync, 16 years ago

Runtime/Aio: Updates

  • Add POSIX backend needed for the Darwin host (not very well tested yet)
  • Typos in the solaris and linux backend
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.0 KB
Line 
1/* $Id: fileaio-linux.cpp 19346 2009-05-05 00:39:14Z vboxsync $ */
2/** @file
3 * IPRT - File async I/O, native implementation for the Linux host platform.
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.215389.xyz. 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/** @page pg_rtfileaio_linux RTFile Async I/O - Linux Implementation Notes
32 * @internal
33 *
34 * Linux implements the kernel async I/O API through the io_* syscalls. They are
35 * not exposed in the glibc (the aio_* API uses userspace threads and blocking
36 * I/O operations to simulate async behavior). There is an external library
37 * called libaio which implements these syscalls but because we don't want to
38 * have another dependency and this library is not installed by default and the
39 * interface is really simple we use the kernel interface directly using wrapper
40 * functions.
41 *
42 * The interface has some limitations. The first one is that the file must be
43 * opened with O_DIRECT. This disables caching done by the kernel which can be
44 * compensated if the user of this API implements caching itself. The next
45 * limitation is that data buffers must be aligned at a 512 byte boundary or the
46 * request will fail.
47 */
48/** @todo r=bird: What's this about "must be opened with O_DIRECT"? An
49 * explanation would be nice, esp. seeing what Linus is quoted saying
50 * about it in the open man page... */
51
52/*******************************************************************************
53* Header Files *
54*******************************************************************************/
55#define LOG_GROUP RTLOGGROUP_FILE
56#include <iprt/asm.h>
57#include <iprt/mem.h>
58#include <iprt/assert.h>
59#include <iprt/string.h>
60#include <iprt/err.h>
61#include <iprt/log.h>
62#include <iprt/thread.h>
63#include "internal/fileaio.h"
64
65#include <linux/aio_abi.h>
66#include <unistd.h>
67#include <sys/syscall.h>
68#include <errno.h>
69
70#include <iprt/file.h>
71
72
73/*******************************************************************************
74* Structures and Typedefs *
75*******************************************************************************/
76/**
77 * The iocb structure of a request which is passed to the kernel.
78 *
79 * We redefined this here because the version in the header lacks padding
80 * for 32bit.
81 */
82typedef struct LNXKAIOIOCB
83{
84 /** Opaque pointer to data which is returned on an I/O event. */
85 void *pvUser;
86#ifdef RT_ARCH_X86
87 uint32_t u32Padding0;
88#endif
89 /** Contains the request number and is set by the kernel. */
90 uint32_t u32Key;
91 /** Reserved. */
92 uint32_t u32Reserved0;
93 /** The I/O opcode. */
94 uint16_t u16IoOpCode;
95 /** Request priority. */
96 int16_t i16Priority;
97 /** The file descriptor. */
98 uint32_t File;
99 /** The userspace pointer to the buffer containing/receiving the data. */
100 void *pvBuf;
101#ifdef RT_ARCH_X86
102 uint32_t u32Padding1;
103#endif
104 /** How many bytes to transfer. */
105#ifdef RT_ARCH_X86
106 uint32_t cbTransfer;
107 uint32_t u32Padding2;
108#elif defined(RT_ARCH_AMD64)
109 uint64_t cbTransfer;
110#else
111# error "Unknown architecture"
112#endif
113 /** At which offset to start the transfer. */
114 int64_t off;
115 /** Reserved. */
116 uint64_t u64Reserved1;
117 /** Flags */
118 uint32_t fFlags;
119 /** Readyness signal file descriptor. */
120 uint32_t u32ResFd;
121} LNXKAIOIOCB, *PLNXKAIOIOCB;
122
123/**
124 * I/O event structure to notify about completed requests.
125 * Redefined here too because of the padding.
126 */
127typedef struct LNXKAIOIOEVENT
128{
129 /** The pvUser field from the iocb. */
130 void *pvUser;
131#ifdef RT_ARCH_X86
132 uint32_t u32Padding0;
133#endif
134 /** The LNXKAIOIOCB object this event is for. */
135 PLNXKAIOIOCB *pIoCB;
136#ifdef RT_ARCH_X86
137 uint32_t u32Padding1;
138#endif
139 /** The result code of the operation .*/
140#ifdef RT_ARCH_X86
141 int32_t rc;
142 uint32_t u32Padding2;
143#elif defined(RT_ARCH_AMD64)
144 int64_t rc;
145#else
146# error "Unknown architecture"
147#endif
148 /** Secondary result code. */
149#ifdef RT_ARCH_X86
150 int32_t rc2;
151 uint32_t u32Padding3;
152#elif defined(RT_ARCH_AMD64)
153 int64_t rc2;
154#else
155# error "Unknown architecture"
156#endif
157} LNXKAIOIOEVENT, *PLNXKAIOIOEVENT;
158
159
160/**
161 * Async I/O completion context state.
162 */
163typedef struct RTFILEAIOCTXINTERNAL
164{
165 /** Handle to the async I/O context. */
166 aio_context_t AioContext;
167 /** Maximum number of requests this context can handle. */
168 int cRequestsMax;
169 /** Current number of requests active on this context. */
170 volatile int32_t cRequests;
171 /** The ID of the thread which is currently waiting for requests. */
172 volatile RTTHREAD hThreadWait;
173 /** Flag whether the thread was woken up. */
174 volatile bool fWokenUp;
175 /** Flag whether the thread is currently waiting in the syscall. */
176 volatile bool fWaiting;
177 /** Magic value (RTFILEAIOCTX_MAGIC). */
178 uint32_t u32Magic;
179} RTFILEAIOCTXINTERNAL;
180/** Pointer to an internal context structure. */
181typedef RTFILEAIOCTXINTERNAL *PRTFILEAIOCTXINTERNAL;
182
183/**
184 * Async I/O request state.
185 */
186typedef struct RTFILEAIOREQINTERNAL
187{
188 /** The aio control block. This must be the FIRST elment in
189 * the structure! (see notes below) */
190 LNXKAIOIOCB AioCB;
191 /** The I/O context this request is associated with. */
192 aio_context_t AioContext;
193 /** Return code the request completed with. */
194 int Rc;
195 /** Flag whether the request is in process or not. */
196 bool fFinished;
197 /** Number of bytes actually trasnfered. */
198 size_t cbTransfered;
199 /** Completion context we are assigned to. */
200 PRTFILEAIOCTXINTERNAL pCtxInt;
201 /** Magic value (RTFILEAIOREQ_MAGIC). */
202 uint32_t u32Magic;
203} RTFILEAIOREQINTERNAL;
204/** Pointer to an internal request structure. */
205typedef RTFILEAIOREQINTERNAL *PRTFILEAIOREQINTERNAL;
206
207
208/*******************************************************************************
209* Defined Constants And Macros *
210*******************************************************************************/
211/** The max number of events to get in one call. */
212#define AIO_MAXIMUM_REQUESTS_PER_CONTEXT 64
213
214
215/**
216 * Creates a new async I/O context.
217 */
218DECLINLINE(int) rtFileAsyncIoLinuxCreate(unsigned cEvents, aio_context_t *pAioContext)
219{
220 int rc = syscall(__NR_io_setup, cEvents, pAioContext);
221 if (RT_UNLIKELY(rc == -1))
222 return RTErrConvertFromErrno(errno);
223
224 return VINF_SUCCESS;
225}
226
227/**
228 * Destroys a async I/O context.
229 */
230DECLINLINE(int) rtFileAsyncIoLinuxDestroy(aio_context_t AioContext)
231{
232 int rc = syscall(__NR_io_destroy, AioContext);
233 if (RT_UNLIKELY(rc == -1))
234 return RTErrConvertFromErrno(errno);
235
236 return VINF_SUCCESS;
237}
238
239/**
240 * Submits an array of I/O requests to the kernel.
241 */
242DECLINLINE(int) rtFileAsyncIoLinuxSubmit(aio_context_t AioContext, long cReqs, LNXKAIOIOCB **ppIoCB)
243{
244 int rc = syscall(__NR_io_submit, AioContext, cReqs, ppIoCB);
245 if (RT_UNLIKELY(rc == -1))
246 return RTErrConvertFromErrno(errno);
247
248 return VINF_SUCCESS;
249}
250
251/**
252 * Cancels a I/O request.
253 */
254DECLINLINE(int) rtFileAsyncIoLinuxCancel(aio_context_t AioContext, PLNXKAIOIOCB pIoCB, PLNXKAIOIOEVENT pIoResult)
255{
256 int rc = syscall(__NR_io_cancel, AioContext, pIoCB, pIoResult);
257 if (RT_UNLIKELY(rc == -1))
258 return RTErrConvertFromErrno(errno);
259
260 return VINF_SUCCESS;
261}
262
263/**
264 * Waits for I/O events.
265 * @returns Number of events (natural number w/ 0), IPRT error code (negative).
266 */
267DECLINLINE(int) rtFileAsyncIoLinuxGetEvents(aio_context_t AioContext, long cReqsMin, long cReqs,
268 PLNXKAIOIOEVENT paIoResults, struct timespec *pTimeout)
269{
270 int rc = syscall(__NR_io_getevents, AioContext, cReqsMin, cReqs, paIoResults, pTimeout);
271 if (RT_UNLIKELY(rc == -1))
272 return RTErrConvertFromErrno(errno);
273
274 return rc;
275}
276
277RTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
278{
279 AssertPtrReturn(phReq, VERR_INVALID_POINTER);
280
281 /*
282 * Allocate a new request and initialize it.
283 */
284 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(*pReqInt));
285 if (RT_UNLIKELY(!pReqInt))
286 return VERR_NO_MEMORY;
287
288 pReqInt->fFinished = false;
289 pReqInt->pCtxInt = NULL;
290 pReqInt->u32Magic = RTFILEAIOREQ_MAGIC;
291
292 *phReq = (RTFILEAIOREQ)pReqInt;
293 return VINF_SUCCESS;
294}
295
296
297RTDECL(void) RTFileAioReqDestroy(RTFILEAIOREQ hReq)
298{
299 /*
300 * Validate the handle and ignore nil.
301 */
302 if (hReq == NIL_RTFILEAIOREQ)
303 return;
304 PRTFILEAIOREQINTERNAL pReqInt = hReq;
305 RTFILEAIOREQ_VALID_RETURN_VOID(pReqInt);
306
307 /*
308 * Trash the magic and free it.
309 */
310 ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
311 RTMemFree(pReqInt);
312}
313
314
315/**
316 * Worker setting up the request.
317 */
318DECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
319 uint16_t uTransferDirection,
320 RTFOFF off, void *pvBuf, size_t cbTransfer,
321 void *pvUser)
322{
323 /*
324 * Validate the input.
325 */
326 PRTFILEAIOREQINTERNAL pReqInt = hReq;
327 RTFILEAIOREQ_VALID_RETURN(pReqInt);
328 Assert(hFile != NIL_RTFILE);
329 AssertPtr(pvBuf);
330 Assert(off >= 0);
331 Assert(cbTransfer > 0);
332
333 /*
334 * Setup the control block and clear the finished flag.
335 */
336 pReqInt->AioCB.u16IoOpCode = uTransferDirection;
337 pReqInt->AioCB.File = (uint32_t)hFile;
338 pReqInt->AioCB.off = off;
339 pReqInt->AioCB.cbTransfer = cbTransfer;
340 pReqInt->AioCB.pvBuf = pvBuf;
341 pReqInt->AioCB.pvUser = pvUser;
342
343 pReqInt->fFinished = false;
344 pReqInt->pCtxInt = NULL;
345
346 return VINF_SUCCESS;
347}
348
349
350RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
351 void *pvBuf, size_t cbRead, void *pvUser)
352{
353 return rtFileAioReqPrepareTransfer(hReq, hFile, IOCB_CMD_PREAD,
354 off, pvBuf, cbRead, pvUser);
355}
356
357
358RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
359 void *pvBuf, size_t cbWrite, void *pvUser)
360{
361 return rtFileAioReqPrepareTransfer(hReq, hFile, IOCB_CMD_PWRITE,
362 off, pvBuf, cbWrite, pvUser);
363}
364
365
366RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
367{
368 PRTFILEAIOREQINTERNAL pReqInt = hReq;
369 RTFILEAIOREQ_VALID_RETURN(pReqInt);
370 AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_HANDLE);
371
372 /** @todo: Flushing is not neccessary on Linux because O_DIRECT is mandatory
373 * which disables caching.
374 * We could setup a fake request which isn't really executed
375 * to avoid platform dependent code in the caller.
376 */
377#if 0
378 return rtFileAsyncPrepareTransfer(pRequest, File, TRANSFERDIRECTION_FLUSH,
379 0, NULL, 0, pvUser);
380#endif
381 return VERR_NOT_IMPLEMENTED;
382}
383
384
385RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
386{
387 PRTFILEAIOREQINTERNAL pReqInt = hReq;
388 RTFILEAIOREQ_VALID_RETURN_RC(pReqInt, NULL);
389
390 return pReqInt->AioCB.pvUser;
391}
392
393
394RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq)
395{
396 PRTFILEAIOREQINTERNAL pReqInt = hReq;
397 RTFILEAIOREQ_VALID_RETURN(pReqInt);
398
399 LNXKAIOIOEVENT AioEvent;
400 int rc = rtFileAsyncIoLinuxCancel(pReqInt->AioContext, &pReqInt->AioCB, &AioEvent);
401 if (RT_SUCCESS(rc))
402 {
403 /*
404 * Decrement request count because the request will never arrive at the
405 * completion port.
406 */
407 AssertMsg(VALID_PTR(pReqInt->pCtxInt),
408 ("Invalid state. Request was canceled but wasn't submitted\n"));
409
410 ASMAtomicDecS32(&pReqInt->pCtxInt->cRequests);
411 return VINF_SUCCESS;
412 }
413 if (rc == VERR_TRY_AGAIN)
414 return VERR_FILE_AIO_IN_PROGRESS;
415 return rc;
416}
417
418
419RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
420{
421 PRTFILEAIOREQINTERNAL pReqInt = hReq;
422 RTFILEAIOREQ_VALID_RETURN(pReqInt);
423 AssertPtrNull(pcbTransfered);
424
425 if (!pReqInt->fFinished)
426 return VERR_FILE_AIO_IN_PROGRESS;
427
428 if ( pcbTransfered
429 && RT_SUCCESS(pReqInt->Rc))
430 *pcbTransfered = pReqInt->cbTransfered;
431
432 return pReqInt->Rc;
433}
434
435
436RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax)
437{
438 PRTFILEAIOCTXINTERNAL pCtxInt;
439 AssertPtrReturn(phAioCtx, VERR_INVALID_POINTER);
440
441 /* The kernel interface needs a maximum. */
442 if (cAioReqsMax == RTFILEAIO_UNLIMITED_REQS)
443 return VERR_OUT_OF_RANGE;
444
445 pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOCTXINTERNAL));
446 if (RT_UNLIKELY(!pCtxInt))
447 return VERR_NO_MEMORY;
448
449 /* Init the event handle. */
450 int rc = rtFileAsyncIoLinuxCreate(cAioReqsMax, &pCtxInt->AioContext);
451 if (RT_SUCCESS(rc))
452 {
453 pCtxInt->fWokenUp = false;
454 pCtxInt->fWaiting = false;
455 pCtxInt->hThreadWait = NIL_RTTHREAD;
456 pCtxInt->cRequestsMax = cAioReqsMax;
457 pCtxInt->u32Magic = RTFILEAIOCTX_MAGIC;
458 *phAioCtx = (RTFILEAIOCTX)pCtxInt;
459 }
460 else
461 RTMemFree(pCtxInt);
462
463 return rc;
464}
465
466
467RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
468{
469 /* Validate the handle and ignore nil. */
470 if (hAioCtx == NIL_RTFILEAIOCTX)
471 return VINF_SUCCESS;
472 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
473 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
474
475 /* Cannot destroy a busy context. */
476 if (RT_UNLIKELY(pCtxInt->cRequests))
477 return VERR_FILE_AIO_BUSY;
478
479 /* The native bit first, then mark it as dead and free it. */
480 int rc = rtFileAsyncIoLinuxDestroy(pCtxInt->AioContext);
481 if (RT_FAILURE(rc))
482 return rc;
483 ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
484 RTMemFree(pCtxInt);
485
486 return VINF_SUCCESS;
487}
488
489
490RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
491{
492 /* Nil means global here. */
493 if (hAioCtx == NIL_RTFILEAIOCTX)
494 return RTFILEAIO_UNLIMITED_REQS; /** @todo r=bird: I'm a bit puzzled by this return value since it
495 * is completely useless in RTFileAioCtxCreate. */
496
497 /* Return 0 if the handle is invalid, it's better than garbage I think... */
498 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
499 RTFILEAIOCTX_VALID_RETURN_RC(pCtxInt, 0);
500
501 return pCtxInt->cRequestsMax;
502}
503
504RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile)
505{
506 /* Nothing to do. */
507 return VINF_SUCCESS;
508}
509
510RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs, size_t *pcReqs)
511{
512 /*
513 * Parameter validation.
514 */
515 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
516 *pcReqs = 0;
517 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
518 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
519 AssertReturn(cReqs > 0, VERR_INVALID_PARAMETER);
520 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
521 uint32_t i = cReqs;
522
523 /*
524 * Vaildate requests and associate with the context.
525 */
526 while (i-- > 0)
527 {
528 PRTFILEAIOREQINTERNAL pReqInt = pahReqs[i];
529 RTFILEAIOREQ_VALID_RETURN(pReqInt);
530
531 pReqInt->AioContext = pCtxInt->AioContext;
532 pReqInt->pCtxInt = pCtxInt;
533 }
534
535 /*
536 * Add the submitted requests to the counter
537 * to prevent destroying the context while
538 * it is still used.
539 */
540 ASMAtomicAddS32(&pCtxInt->cRequests, cReqs);
541
542 /*
543 * We cast phReqs to the Linux iocb structure to avoid copying the requests
544 * into a temporary array. This is possible because the iocb structure is
545 * the first element in the request structure (see PRTFILEAIOCTXINTERNAL).
546 */
547 int rc = rtFileAsyncIoLinuxSubmit(pCtxInt->AioContext, cReqs, (PLNXKAIOIOCB *)pahReqs);
548 if (RT_FAILURE(rc))
549 ASMAtomicSubS32(&pCtxInt->cRequests, cReqs);
550 else
551 *pcReqs = cReqs;
552
553 return rc;
554}
555
556
557RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, unsigned cMillisTimeout,
558 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
559{
560 /*
561 * Validate the parameters, making sure to always set pcReqs.
562 */
563 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
564 *pcReqs = 0; /* always set */
565 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
566 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
567 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
568 AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
569 AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
570
571 /*
572 * Can't wait if there are not requests around.
573 */
574 if (RT_UNLIKELY(ASMAtomicUoReadS32(&pCtxInt->cRequests) == 0))
575 return VERR_FILE_AIO_NO_REQUEST;
576
577 /*
578 * Convert the timeout if specified.
579 */
580 struct timespec *pTimeout = NULL;
581 struct timespec Timeout = {0,0};
582 uint64_t StartNanoTS = 0;
583 if (cMillisTimeout != RT_INDEFINITE_WAIT)
584 {
585 Timeout.tv_sec = cMillisTimeout / 1000;
586 Timeout.tv_nsec = cMillisTimeout % 1000 * 1000000;
587 pTimeout = &Timeout;
588 StartNanoTS = RTTimeNanoTS();
589 }
590
591 /* Wait for at least one. */
592 if (!cMinReqs)
593 cMinReqs = 1;
594
595 /* For the wakeup call. */
596 Assert(pCtxInt->hThreadWait == NIL_RTTHREAD);
597 ASMAtomicWriteHandle(&pCtxInt->hThreadWait, RTThreadSelf());
598
599 /*
600 * Loop until we're woken up, hit an error (incl timeout), or
601 * have collected the desired number of requests.
602 */
603 int rc = VINF_SUCCESS;
604 int cRequestsCompleted = 0;
605 while (!pCtxInt->fWokenUp)
606 {
607 LNXKAIOIOEVENT aPortEvents[AIO_MAXIMUM_REQUESTS_PER_CONTEXT];
608 int cRequestsToWait = RT_MIN(cReqs, AIO_MAXIMUM_REQUESTS_PER_CONTEXT);
609 ASMAtomicXchgBool(&pCtxInt->fWaiting, true);
610 rc = rtFileAsyncIoLinuxGetEvents(pCtxInt->AioContext, cMinReqs, cRequestsToWait, &aPortEvents[0], pTimeout);
611 ASMAtomicXchgBool(&pCtxInt->fWaiting, false);
612 if (RT_FAILURE(rc))
613 break;
614 uint32_t const cDone = rc;
615 rc = VINF_SUCCESS;
616
617 /*
618 * Process received events / requests.
619 */
620 for (uint32_t i = 0; i < cDone; i++)
621 {
622 /*
623 * The iocb is the first element in our request structure.
624 * So we can safely cast it directly to the handle (see above)
625 */
626 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)aPortEvents[i].pIoCB;
627 AssertPtr(pReqInt);
628 Assert(pReqInt->u32Magic == RTFILEAIOREQ_MAGIC);
629
630 /** @todo aeichner: The rc field contains the result code
631 * like you can find in errno for the normal read/write ops.
632 * But there is a second field called rc2. I don't know the
633 * purpose for it yet.
634 */
635 if (RT_UNLIKELY(aPortEvents[i].rc < 0))
636 pReqInt->Rc = RTErrConvertFromErrno(aPortEvents[i].rc);
637 else
638 {
639 pReqInt->Rc = VINF_SUCCESS;
640 pReqInt->cbTransfered = aPortEvents[i].rc;
641 }
642
643 /* Mark the request as finished. */
644 pReqInt->fFinished = true;
645
646 pahReqs[cRequestsCompleted++] = (RTFILEAIOREQ)pReqInt;
647 }
648
649 /*
650 * Done Yet? If not advance and try again.
651 */
652 if (cDone >= cMinReqs)
653 break;
654 cMinReqs -= cDone;
655 cReqs -= cDone;
656
657 if (cMillisTimeout != RT_INDEFINITE_WAIT)
658 {
659 /* The API doesn't return ETIMEDOUT, so we have to fix that ourselves. */
660 uint64_t NanoTS = RTTimeNanoTS();
661 uint64_t cMilliesElapsed = (NanoTS - StartNanoTS) / 1000000;
662 if (cMilliesElapsed >= cMillisTimeout)
663 {
664 rc = VERR_TIMEOUT;
665 break;
666 }
667
668 /* The syscall supposedly updates it, but we're paranoid. :-) */
669 Timeout.tv_sec = (cMillisTimeout - (unsigned)cMilliesElapsed) / 1000;
670 Timeout.tv_nsec = (cMillisTimeout - (unsigned)cMilliesElapsed) % 1000 * 1000000;
671 }
672 }
673
674 /*
675 * Update the context state and set the return value.
676 */
677 *pcReqs = cRequestsCompleted;
678 ASMAtomicSubS32(&pCtxInt->cRequests, cRequestsCompleted);
679 Assert(pCtxInt->hThreadWait == RTThreadSelf());
680 ASMAtomicWriteHandle(&pCtxInt->hThreadWait, NIL_RTTHREAD);
681
682 /*
683 * Clear the wakeup flag and set rc.
684 */
685 if ( pCtxInt->fWokenUp
686 && RT_SUCCESS(rc))
687 {
688 ASMAtomicXchgBool(&pCtxInt->fWokenUp, false);
689 rc = VERR_INTERRUPTED;
690 }
691
692 return rc;
693}
694
695
696RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
697{
698 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
699 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
700
701 /** @todo r=bird: Define the protocol for how to resume work after calling
702 * this function. */
703
704 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, true);
705
706 /*
707 * Read the thread handle before the status flag.
708 * If we read the handle after the flag we might
709 * end up with an invalid handle because the thread
710 * waiting in RTFileAioCtxWakeup() might get scheduled
711 * before we read the flag and returns.
712 * We can ensure that the handle is valid if fWaiting is true
713 * when reading the handle before the status flag.
714 */
715 RTTHREAD hThread;
716 ASMAtomicReadHandle(&pCtxInt->hThreadWait, &hThread);
717 bool fWaiting = ASMAtomicReadBool(&pCtxInt->fWaiting);
718 if ( !fWokenUp
719 && fWaiting)
720 {
721 /*
722 * If a thread waits the handle must be valid.
723 * It is possible that the thread returns from
724 * rtFileAsyncIoLinuxGetEvents() before the signal
725 * is send.
726 * This is no problem because we already set fWokenUp
727 * to true which will let the thread return VERR_INTERRUPTED
728 * and the next call to RTFileAioCtxWait() will not
729 * return VERR_INTERRUPTED because signals are not saved
730 * and will simply vanish if the destination thread can't
731 * receive it.
732 */
733 Assert(hThread != NIL_RTTHREAD);
734 RTThreadPoke(hThread);
735 }
736
737 return VINF_SUCCESS;
738}
739
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette