VirtualBox

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

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

Runtime/Aio: Prevent inclusion of swabb.h because g++ doesn't like the code. We don't need byte swapping for the things we need from aio_abi.h anyway.

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