VirtualBox

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

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

IPRT: svn props for tstFileAio.cpp and fileaio-linux.cpp. (Please use svn-ps.sh -a filename.ext when adding new files (svn-ps.cmd on windows.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.3 KB
Line 
1/* $Id: fileaio-linux.cpp 19054 2009-04-21 09:23: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/magics.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 request state.
162 */
163typedef struct RTFILEAIOREQINTERNAL
164{
165 /** The aio control block. This must be the FIRST elment in
166 * the structure! (see notes below) */
167 LNXKAIOIOCB AioCB;
168 /** The I/O context this request is associated with. */
169 aio_context_t AioContext;
170 /** Return code the request completed with. */
171 int Rc;
172 /** Flag whether the request is in process or not. */
173 bool fFinished;
174 /** Number of bytes actually trasnfered. */
175 size_t cbTransfered;
176 /** Magic value (RTFILEAIOREQ_MAGIC). */
177 uint32_t u32Magic;
178} RTFILEAIOREQINTERNAL;
179/** Pointer to an internal request structure. */
180typedef RTFILEAIOREQINTERNAL *PRTFILEAIOREQINTERNAL;
181
182/**
183 * Async I/O completion context state.
184 */
185typedef struct RTFILEAIOCTXINTERNAL
186{
187 /** Handle to the async I/O context. */
188 aio_context_t AioContext;
189 /** Maximum number of requests this context can handle. */
190 int cRequestsMax;
191 /** Current number of requests active on this context. */
192 volatile int32_t cRequests;
193 /** The ID of the thread which is currently waiting for requests. */
194 volatile RTTHREAD hThreadWait;
195 /** Flag whether the thread was woken up. */
196 volatile bool fWokenUp;
197 /** Flag whether the thread is currently waiting in the syscall. */
198 volatile bool fWaiting;
199 /** Magic value (RTFILEAIOCTX_MAGIC). */
200 uint32_t u32Magic;
201} RTFILEAIOCTXINTERNAL;
202/** Pointer to an internal context structure. */
203typedef RTFILEAIOCTXINTERNAL *PRTFILEAIOCTXINTERNAL;
204
205
206/*******************************************************************************
207* Defined Constants And Macros *
208*******************************************************************************/
209/** The max number of events to get in one call. */
210#define AIO_MAXIMUM_REQUESTS_PER_CONTEXT 64
211
212/** Validates a context handle and returns VERR_INVALID_HANDLE if not valid. */
213#define RTFILEAIOREQ_VALID_RETURN_RC(pReq, rc) \
214 do { \
215 AssertPtrReturn((pReq), (rc)); \
216 AssertReturn((pReq)->u32Magic == RTFILEAIOREQ_MAGIC, (rc)); \
217 } while (0)
218
219/** Validates a context handle and returns VERR_INVALID_HANDLE if not valid. */
220#define RTFILEAIOREQ_VALID_RETURN(pReq) RTFILEAIOREQ_VALID_RETURN_RC((pReq), VERR_INVALID_HANDLE)
221
222/** Validates a context handle and returns (void) if not valid. */
223#define RTFILEAIOREQ_VALID_RETURN_VOID(pReq) \
224 do { \
225 AssertPtrReturnVoid(pReq); \
226 AssertReturnVoid((pReq)->u32Magic == RTFILEAIOREQ_MAGIC); \
227 } while (0)
228
229/** Validates a context handle and returns the specified rc if not valid. */
230#define RTFILEAIOCTX_VALID_RETURN_RC(pCtx, rc) \
231 do { \
232 AssertPtrReturn((pCtx), (rc)); \
233 AssertReturn((pCtx)->u32Magic == RTFILEAIOCTX_MAGIC, (rc)); \
234 } while (0)
235
236/** Validates a context handle and returns VERR_INVALID_HANDLE if not valid. */
237#define RTFILEAIOCTX_VALID_RETURN(pCtx) RTFILEAIOCTX_VALID_RETURN_RC((pCtx), VERR_INVALID_HANDLE)
238
239
240/**
241 * Creates a new async I/O context.
242 */
243DECLINLINE(int) rtFileAsyncIoLinuxCreate(unsigned cEvents, aio_context_t *pAioContext)
244{
245 int rc = syscall(__NR_io_setup, cEvents, pAioContext);
246 if (RT_UNLIKELY(rc == -1))
247 return RTErrConvertFromErrno(errno);
248
249 return VINF_SUCCESS;
250}
251
252/**
253 * Destroys a async I/O context.
254 */
255DECLINLINE(int) rtFileAsyncIoLinuxDestroy(aio_context_t AioContext)
256{
257 int rc = syscall(__NR_io_destroy, AioContext);
258 if (RT_UNLIKELY(rc == -1))
259 return RTErrConvertFromErrno(errno);
260
261 return VINF_SUCCESS;
262}
263
264/**
265 * Submits an array of I/O requests to the kernel.
266 */
267DECLINLINE(int) rtFileAsyncIoLinuxSubmit(aio_context_t AioContext, long cReqs, LNXKAIOIOCB **ppIoCB)
268{
269 int rc = syscall(__NR_io_submit, AioContext, cReqs, ppIoCB);
270 if (RT_UNLIKELY(rc == -1))
271 return RTErrConvertFromErrno(errno);
272
273 return VINF_SUCCESS;
274}
275
276/**
277 * Cancels a I/O request.
278 */
279DECLINLINE(int) rtFileAsyncIoLinuxCancel(aio_context_t AioContext, PLNXKAIOIOCB pIoCB, PLNXKAIOIOEVENT pIoResult)
280{
281 int rc = syscall(__NR_io_cancel, AioContext, pIoCB, pIoResult);
282 if (RT_UNLIKELY(rc == -1))
283 return RTErrConvertFromErrno(errno);
284
285 return VINF_SUCCESS;
286}
287
288/**
289 * Waits for I/O events.
290 * @returns Number of events (natural number w/ 0), IPRT error code (negative).
291 */
292DECLINLINE(int) rtFileAsyncIoLinuxGetEvents(aio_context_t AioContext, long cReqsMin, long cReqs,
293 PLNXKAIOIOEVENT paIoResults, struct timespec *pTimeout)
294{
295 int rc = syscall(__NR_io_getevents, AioContext, cReqsMin, cReqs, paIoResults, pTimeout);
296 if (RT_UNLIKELY(rc == -1))
297 return RTErrConvertFromErrno(errno);
298
299 return rc;
300}
301
302
303RTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
304{
305 AssertPtrReturn(phReq, VERR_INVALID_POINTER);
306
307 /*
308 * Allocate a new request and initialize it.
309 */
310 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(*pReqInt));
311 if (RT_UNLIKELY(!pReqInt))
312 return VERR_NO_MEMORY;
313
314 pReqInt->fFinished = false;
315 pReqInt->u32Magic = RTFILEAIOREQ_MAGIC;
316
317 *phReq = (RTFILEAIOREQ)pReqInt;
318 return VINF_SUCCESS;
319}
320
321
322RTDECL(void) RTFileAioReqDestroy(RTFILEAIOREQ hReq)
323{
324 /*
325 * Validate the handle and ignore nil.
326 */
327 if (hReq == NIL_RTFILEAIOREQ)
328 return;
329 PRTFILEAIOREQINTERNAL pReqInt = hReq;
330 RTFILEAIOREQ_VALID_RETURN_VOID(pReqInt);
331
332 /*
333 * Trash the magic and free it.
334 */
335 ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
336 RTMemFree(pReqInt);
337}
338
339
340/**
341 * Worker setting up the request.
342 */
343DECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
344 uint16_t uTransferDirection,
345 RTFOFF off, void *pvBuf, size_t cbTransfer,
346 void *pvUser)
347{
348 /*
349 * Validate the input.
350 */
351 PRTFILEAIOREQINTERNAL pReqInt = hReq;
352 RTFILEAIOREQ_VALID_RETURN(pReqInt);
353 Assert(hFile != NIL_RTFILE);
354 AssertPtr(pvBuf);
355 Assert(off >= 0);
356 Assert(cbTransfer > 0);
357
358 /*
359 * Setup the control block and clear the finished flag.
360 */
361 pReqInt->AioCB.u16IoOpCode = uTransferDirection;
362 pReqInt->AioCB.File = (uint32_t)hFile;
363 pReqInt->AioCB.off = off;
364 pReqInt->AioCB.cbTransfer = cbTransfer;
365 pReqInt->AioCB.pvBuf = pvBuf;
366 pReqInt->AioCB.pvUser = pvUser;
367
368 pReqInt->fFinished = false;
369
370 return VINF_SUCCESS;
371}
372
373
374RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
375 void *pvBuf, size_t cbRead, void *pvUser)
376{
377 return rtFileAioReqPrepareTransfer(hReq, hFile, IOCB_CMD_PREAD,
378 off, pvBuf, cbRead, pvUser);
379}
380
381
382RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
383 void *pvBuf, size_t cbWrite, void *pvUser)
384{
385 return rtFileAioReqPrepareTransfer(hReq, hFile, IOCB_CMD_PWRITE,
386 off, pvBuf, cbWrite, pvUser);
387}
388
389
390RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
391{
392 PRTFILEAIOREQINTERNAL pReqInt = hReq;
393 RTFILEAIOREQ_VALID_RETURN(pReqInt);
394 AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_HANDLE);
395
396 /** @todo: Flushing is not neccessary on Linux because O_DIRECT is mandatory
397 * which disables caching.
398 * We could setup a fake request which isn't really executed
399 * to avoid platform dependent code in the caller.
400 */
401#if 0
402 return rtFileAsyncPrepareTransfer(pRequest, File, TRANSFERDIRECTION_FLUSH,
403 0, NULL, 0, pvUser);
404#endif
405 return VERR_NOT_IMPLEMENTED;
406}
407
408
409RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
410{
411 PRTFILEAIOREQINTERNAL pReqInt = hReq;
412 RTFILEAIOREQ_VALID_RETURN_RC(pReqInt, NULL);
413
414 return pReqInt->AioCB.pvUser;
415}
416
417
418RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq)
419{
420 PRTFILEAIOREQINTERNAL pReqInt = hReq;
421 RTFILEAIOREQ_VALID_RETURN(pReqInt);
422
423 LNXKAIOIOEVENT AioEvent;
424 int rc = rtFileAsyncIoLinuxCancel(pReqInt->AioContext, &pReqInt->AioCB, &AioEvent);
425 if (RT_SUCCESS(rc))
426 {
427 /* Examine rc in the event structure. */
428 return VINF_SUCCESS;
429 }
430 if (rc == VERR_TRY_AGAIN)
431 return VERR_FILE_AIO_IN_PROGRESS;
432 return rc;
433}
434
435
436RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
437{
438 PRTFILEAIOREQINTERNAL pReqInt = hReq;
439 RTFILEAIOREQ_VALID_RETURN(pReqInt);
440 AssertPtrNull(pcbTransfered);
441
442 if (!pReqInt->fFinished)
443 return VERR_FILE_AIO_IN_PROGRESS;
444
445 if ( pcbTransfered
446 && RT_SUCCESS(pReqInt->Rc))
447 *pcbTransfered = pReqInt->cbTransfered;
448
449 return pReqInt->Rc;
450}
451
452
453RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax)
454{
455 PRTFILEAIOCTXINTERNAL pCtxInt;
456 AssertPtrReturn(phAioCtx, VERR_INVALID_POINTER);
457
458 /* The kernel interface needs a maximum. */
459 if (cAioReqsMax == RTFILEAIO_UNLIMITED_REQS)
460 return VERR_OUT_OF_RANGE;
461
462 pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOCTXINTERNAL));
463 if (RT_UNLIKELY(!pCtxInt))
464 return VERR_NO_MEMORY;
465
466 /* Init the event handle. */
467 int rc = rtFileAsyncIoLinuxCreate(cAioReqsMax, &pCtxInt->AioContext);
468 if (RT_SUCCESS(rc))
469 {
470 pCtxInt->fWokenUp = false;
471 pCtxInt->fWaiting = false;
472 pCtxInt->hThreadWait = NIL_RTTHREAD;
473 pCtxInt->cRequestsMax = cAioReqsMax;
474 pCtxInt->u32Magic = RTFILEAIOCTX_MAGIC;
475 *phAioCtx = (RTFILEAIOCTX)pCtxInt;
476 }
477
478 return rc;
479}
480
481
482RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
483{
484 /* Validate the handle and ignore nil. */
485 if (hAioCtx == NIL_RTFILEAIOCTX)
486 return VINF_SUCCESS;
487 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
488 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
489
490 /* Cannot destroy a busy context. */
491 if (RT_UNLIKELY(pCtxInt->cRequests))
492 return VERR_FILE_AIO_BUSY;
493
494 /* The native bit first, then mark it as dead and free it. */
495 int rc = rtFileAsyncIoLinuxDestroy(pCtxInt->AioContext);
496 if (RT_FAILURE(rc))
497 return rc;
498 ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
499 RTMemFree(pCtxInt);
500
501 return VINF_SUCCESS;
502}
503
504
505RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
506{
507 /* Nil means global here. */
508 if (hAioCtx == NIL_RTFILEAIOCTX)
509 return RTFILEAIO_UNLIMITED_REQS; /** @todo r=bird: I'm a bit puzzled by this return value since it
510 * is completely useless in RTFileAioCtxCreate. */
511
512 /* Return 0 if the handle is invalid, it's better than garbage I think... */
513 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
514 RTFILEAIOCTX_VALID_RETURN_RC(pCtxInt, 0);
515
516 return pCtxInt->cRequestsMax;
517}
518
519
520RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
521{
522 /*
523 * Parameter validation.
524 */
525 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
526 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
527 AssertReturn(cReqs > 0, VERR_INVALID_PARAMETER);
528 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
529 uint32_t i = cReqs;
530 while (i-- > 0)
531 {
532 PRTFILEAIOREQINTERNAL pReqInt = pahReqs[i];
533 RTFILEAIOREQ_VALID_RETURN(pReqInt);
534 }
535
536 /*
537 * Add descriptive comment.
538 */
539 /** @todo r=bird: Why this particular order?
540 * Perhaps combine the AioContext initialization with the validation? */
541 ASMAtomicAddS32(&pCtxInt->cRequests, cReqs);
542
543 for (unsigned i = 0; i < cReqs; i++)
544 {
545 PRTFILEAIOREQINTERNAL pReqInt = pahReqs[i];
546 pReqInt->AioContext = pCtxInt->AioContext;
547 }
548
549 /*
550 * We cast phReqs to the Linux iocb structure to avoid copying the requests
551 * into a temporary array. This is possible because the iocb structure is
552 * the first element in the request structure (see PRTFILEAIOCTXINTERNAL).
553 */
554 int rc = rtFileAsyncIoLinuxSubmit(pCtxInt->AioContext, cReqs, (PLNXKAIOIOCB *)pahReqs);
555 if (RT_FAILURE(rc))
556 ASMAtomicSubS32(&pCtxInt->cRequests, cReqs);
557
558 return rc;
559}
560
561
562RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, unsigned cMillisTimeout,
563 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
564{
565 /*
566 * Validate the parameters, making sure to always set pcReqs.
567 */
568 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
569 *pcReqs = 0; /* always set */
570 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
571 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
572 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
573 AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
574 AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
575
576 /*
577 * Can't wait if there are not requests around.
578 */
579 if (RT_UNLIKELY(ASMAtomicUoReadS32(&pCtxInt->cRequests) == 0))
580 return VERR_FILE_AIO_NO_REQUEST;
581
582 /*
583 * Convert the timeout if specified.
584 */
585 struct timespec *pTimeout = NULL;
586 struct timespec Timeout = {0,0};
587 uint64_t StartNanoTS = 0;
588 if (cMillisTimeout != RT_INDEFINITE_WAIT)
589 {
590 Timeout.tv_sec = cMillisTimeout / 1000;
591 Timeout.tv_nsec = cMillisTimeout % 1000 * 1000000;
592 pTimeout = &Timeout;
593 StartNanoTS = RTTimeNanoTS();
594 }
595
596 /* Wait for at least one. */
597 if (!cMinReqs)
598 cMinReqs = 1;
599
600 /* For the wakeup call. */
601 Assert(pCtxInt->hThreadWait == NIL_RTTHREAD);
602 pCtxInt->hThreadWait = RTThreadSelf();
603
604 /*
605 * Loop until we're woken up, hit an error (incl timeout), or
606 * have collected the desired number of requests.
607 */
608 int rc = VINF_SUCCESS;
609 int cRequestsCompleted = 0;
610 while (!pCtxInt->fWokenUp)
611 {
612 LNXKAIOIOEVENT aPortEvents[AIO_MAXIMUM_REQUESTS_PER_CONTEXT];
613 int cRequestsToWait = RT_MIN(cReqs, AIO_MAXIMUM_REQUESTS_PER_CONTEXT);
614 ASMAtomicXchgBool(&pCtxInt->fWaiting, true);
615 rc = rtFileAsyncIoLinuxGetEvents(pCtxInt->AioContext, cMinReqs, cRequestsToWait, &aPortEvents[0], pTimeout);
616 ASMAtomicXchgBool(&pCtxInt->fWaiting, false);
617 if (RT_FAILURE(rc))
618 break;
619 uint32_t const cDone = rc;
620 rc = VINF_SUCCESS;
621
622 /*
623 * Process received events / requests.
624 */
625 for (uint32_t i = 0; i < cDone; i++)
626 {
627 /*
628 * The iocb is the first element in our request structure.
629 * So we can safely cast it directly to the handle (see above)
630 */
631 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)aPortEvents[i].pIoCB;
632 AssertPtr(pReqInt);
633 Assert(pReqInt->u32Magic == RTFILEAIOREQ_MAGIC);
634
635 /** @todo aeichner: The rc field contains the result code
636 * like you can find in errno for the normal read/write ops.
637 * But there is a second field called rc2. I don't know the
638 * purpose for it yet.
639 */
640 if (RT_UNLIKELY(aPortEvents[i].rc < 0))
641 pReqInt->Rc = RTErrConvertFromErrno(aPortEvents[i].rc);
642 else
643 {
644 pReqInt->Rc = VINF_SUCCESS;
645 pReqInt->cbTransfered = aPortEvents[i].rc;
646 }
647
648 /* Mark the request as finished. */
649 pReqInt->fFinished = true;
650
651 pahReqs[cRequestsCompleted++] = (RTFILEAIOREQ)pReqInt;
652 }
653
654 /*
655 * Done Yet? If not advance and try again.
656 */
657 if (cDone >= cMinReqs)
658 break;
659 cMinReqs -= cDone;
660 cReqs -= cDone;
661
662 if (cMillisTimeout != RT_INDEFINITE_WAIT)
663 {
664 /* The API doesn't return ETIMEDOUT, so we have to fix that ourselves. */
665 uint64_t NanoTS = RTTimeNanoTS();
666 uint64_t cMilliesElapsed = (NanoTS - StartNanoTS) / 1000000;
667 if (cMilliesElapsed >= cMillisTimeout)
668 {
669 rc = VERR_TIMEOUT;
670 break;
671 }
672
673 /* The syscall supposedly updates it, but we're paranoid. :-) */
674 Timeout.tv_sec = (cMillisTimeout - (unsigned)cMilliesElapsed) / 1000;
675 Timeout.tv_nsec = (cMillisTimeout - (unsigned)cMilliesElapsed) % 1000 * 1000000;
676 }
677 }
678
679 /*
680 * Update the context state and set the return value.
681 */
682 *pcReqs = cRequestsCompleted;
683 ASMAtomicSubS32(&pCtxInt->cRequests, cRequestsCompleted);
684 Assert(pCtxInt->hThreadWait == RTThreadSelf());
685 pCtxInt->hThreadWait = NIL_RTTHREAD;
686
687 /*
688 * Clear the wakeup flag and set rc.
689 */
690 if ( pCtxInt->fWokenUp
691 && RT_SUCCESS(rc))
692 {
693 ASMAtomicXchgBool(&pCtxInt->fWokenUp, false);
694 rc = VERR_INTERRUPTED;
695 }
696
697 return rc;
698}
699
700
701RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
702{
703 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
704 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
705
706 /** @todo r=bird: Define the protocol for how to resume work after calling
707 * this function. */
708
709 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, true);
710 if ( !fWokenUp
711 && pCtxInt->fWaiting)
712 RTThreadPoke(pCtxInt->hThreadWait);
713
714 return VINF_SUCCESS;
715}
716
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