VirtualBox

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

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

Runtime/Aio: * Move the validation macros to a new header because they are used in

the other implmenetations as well.

  • Fix two bugs in the linux implementation:
    1. If a request is canceled the request count is not decremented making it impossible to destroy a context.
    2. Fix a race in the wakeup method which could lead to a nil thread handle.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.8 KB
Line 
1/* $Id: fileaio-linux.cpp 19126 2009-04-22 22:52:02Z 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#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
461 return rc;
462}
463
464
465RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
466{
467 /* Validate the handle and ignore nil. */
468 if (hAioCtx == NIL_RTFILEAIOCTX)
469 return VINF_SUCCESS;
470 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
471 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
472
473 /* Cannot destroy a busy context. */
474 if (RT_UNLIKELY(pCtxInt->cRequests))
475 return VERR_FILE_AIO_BUSY;
476
477 /* The native bit first, then mark it as dead and free it. */
478 int rc = rtFileAsyncIoLinuxDestroy(pCtxInt->AioContext);
479 if (RT_FAILURE(rc))
480 return rc;
481 ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
482 RTMemFree(pCtxInt);
483
484 return VINF_SUCCESS;
485}
486
487
488RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
489{
490 /* Nil means global here. */
491 if (hAioCtx == NIL_RTFILEAIOCTX)
492 return RTFILEAIO_UNLIMITED_REQS; /** @todo r=bird: I'm a bit puzzled by this return value since it
493 * is completely useless in RTFileAioCtxCreate. */
494
495 /* Return 0 if the handle is invalid, it's better than garbage I think... */
496 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
497 RTFILEAIOCTX_VALID_RETURN_RC(pCtxInt, 0);
498
499 return pCtxInt->cRequestsMax;
500}
501
502
503RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
504{
505 /*
506 * Parameter validation.
507 */
508 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
509 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
510 AssertReturn(cReqs > 0, VERR_INVALID_PARAMETER);
511 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
512 uint32_t i = cReqs;
513
514 /*
515 * Vaildate requests and associate with the context.
516 */
517 while (i-- > 0)
518 {
519 PRTFILEAIOREQINTERNAL pReqInt = pahReqs[i];
520 RTFILEAIOREQ_VALID_RETURN(pReqInt);
521
522 pReqInt->AioContext = pCtxInt->AioContext;
523 pReqInt->pCtxInt = pCtxInt;
524 }
525
526 /*
527 * Add the submitted requests to the counter
528 * to prevent destroying the context while
529 * it is still used.
530 */
531 ASMAtomicAddS32(&pCtxInt->cRequests, cReqs);
532
533 /*
534 * We cast phReqs to the Linux iocb structure to avoid copying the requests
535 * into a temporary array. This is possible because the iocb structure is
536 * the first element in the request structure (see PRTFILEAIOCTXINTERNAL).
537 */
538 int rc = rtFileAsyncIoLinuxSubmit(pCtxInt->AioContext, cReqs, (PLNXKAIOIOCB *)pahReqs);
539 if (RT_FAILURE(rc))
540 ASMAtomicSubS32(&pCtxInt->cRequests, cReqs);
541
542 return rc;
543}
544
545
546RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, unsigned cMillisTimeout,
547 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
548{
549 /*
550 * Validate the parameters, making sure to always set pcReqs.
551 */
552 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
553 *pcReqs = 0; /* always set */
554 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
555 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
556 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
557 AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
558 AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
559
560 /*
561 * Can't wait if there are not requests around.
562 */
563 if (RT_UNLIKELY(ASMAtomicUoReadS32(&pCtxInt->cRequests) == 0))
564 return VERR_FILE_AIO_NO_REQUEST;
565
566 /*
567 * Convert the timeout if specified.
568 */
569 struct timespec *pTimeout = NULL;
570 struct timespec Timeout = {0,0};
571 uint64_t StartNanoTS = 0;
572 if (cMillisTimeout != RT_INDEFINITE_WAIT)
573 {
574 Timeout.tv_sec = cMillisTimeout / 1000;
575 Timeout.tv_nsec = cMillisTimeout % 1000 * 1000000;
576 pTimeout = &Timeout;
577 StartNanoTS = RTTimeNanoTS();
578 }
579
580 /* Wait for at least one. */
581 if (!cMinReqs)
582 cMinReqs = 1;
583
584 /* For the wakeup call. */
585 Assert(pCtxInt->hThreadWait == NIL_RTTHREAD);
586 ASMAtomicWriteHandle(&pCtxInt->hThreadWait, RTThreadSelf());
587
588 /*
589 * Loop until we're woken up, hit an error (incl timeout), or
590 * have collected the desired number of requests.
591 */
592 int rc = VINF_SUCCESS;
593 int cRequestsCompleted = 0;
594 while (!pCtxInt->fWokenUp)
595 {
596 LNXKAIOIOEVENT aPortEvents[AIO_MAXIMUM_REQUESTS_PER_CONTEXT];
597 int cRequestsToWait = RT_MIN(cReqs, AIO_MAXIMUM_REQUESTS_PER_CONTEXT);
598 ASMAtomicXchgBool(&pCtxInt->fWaiting, true);
599 rc = rtFileAsyncIoLinuxGetEvents(pCtxInt->AioContext, cMinReqs, cRequestsToWait, &aPortEvents[0], pTimeout);
600 ASMAtomicXchgBool(&pCtxInt->fWaiting, false);
601 if (RT_FAILURE(rc))
602 break;
603 uint32_t const cDone = rc;
604 rc = VINF_SUCCESS;
605
606 /*
607 * Process received events / requests.
608 */
609 for (uint32_t i = 0; i < cDone; i++)
610 {
611 /*
612 * The iocb is the first element in our request structure.
613 * So we can safely cast it directly to the handle (see above)
614 */
615 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)aPortEvents[i].pIoCB;
616 AssertPtr(pReqInt);
617 Assert(pReqInt->u32Magic == RTFILEAIOREQ_MAGIC);
618
619 /** @todo aeichner: The rc field contains the result code
620 * like you can find in errno for the normal read/write ops.
621 * But there is a second field called rc2. I don't know the
622 * purpose for it yet.
623 */
624 if (RT_UNLIKELY(aPortEvents[i].rc < 0))
625 pReqInt->Rc = RTErrConvertFromErrno(aPortEvents[i].rc);
626 else
627 {
628 pReqInt->Rc = VINF_SUCCESS;
629 pReqInt->cbTransfered = aPortEvents[i].rc;
630 }
631
632 /* Mark the request as finished. */
633 pReqInt->fFinished = true;
634
635 pahReqs[cRequestsCompleted++] = (RTFILEAIOREQ)pReqInt;
636 }
637
638 /*
639 * Done Yet? If not advance and try again.
640 */
641 if (cDone >= cMinReqs)
642 break;
643 cMinReqs -= cDone;
644 cReqs -= cDone;
645
646 if (cMillisTimeout != RT_INDEFINITE_WAIT)
647 {
648 /* The API doesn't return ETIMEDOUT, so we have to fix that ourselves. */
649 uint64_t NanoTS = RTTimeNanoTS();
650 uint64_t cMilliesElapsed = (NanoTS - StartNanoTS) / 1000000;
651 if (cMilliesElapsed >= cMillisTimeout)
652 {
653 rc = VERR_TIMEOUT;
654 break;
655 }
656
657 /* The syscall supposedly updates it, but we're paranoid. :-) */
658 Timeout.tv_sec = (cMillisTimeout - (unsigned)cMilliesElapsed) / 1000;
659 Timeout.tv_nsec = (cMillisTimeout - (unsigned)cMilliesElapsed) % 1000 * 1000000;
660 }
661 }
662
663 /*
664 * Update the context state and set the return value.
665 */
666 *pcReqs = cRequestsCompleted;
667 ASMAtomicSubS32(&pCtxInt->cRequests, cRequestsCompleted);
668 Assert(pCtxInt->hThreadWait == RTThreadSelf());
669 ASMAtomicWriteHandle(&pCtxInt->hThreadWait, NIL_RTTHREAD);
670
671 /*
672 * Clear the wakeup flag and set rc.
673 */
674 if ( pCtxInt->fWokenUp
675 && RT_SUCCESS(rc))
676 {
677 ASMAtomicXchgBool(&pCtxInt->fWokenUp, false);
678 rc = VERR_INTERRUPTED;
679 }
680
681 return rc;
682}
683
684
685RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
686{
687 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
688 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
689
690 /** @todo r=bird: Define the protocol for how to resume work after calling
691 * this function. */
692
693 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, true);
694
695 /*
696 * Read the thread handle before the status flag.
697 * If we read the handle after the flag we might
698 * end up with an invalid handle because the thread
699 * waiting in RTFileAioCtxWakeup() might get scheduled
700 * before we read the flag and returns.
701 * We can ensure that the handle is valid if fWaiting is true
702 * when reading the handle before the status flag.
703 */
704 RTTHREAD hThread;
705 ASMAtomicReadHandle(&pCtxInt->hThreadWait, &hThread);
706 bool fWaiting = ASMAtomicReadBool(&pCtxInt->fWaiting);
707 if ( !fWokenUp
708 && fWaiting)
709 {
710 /*
711 * If a thread waits the handle must be valid.
712 * It is possible that the thread returns from
713 * rtFileAsyncIoLinuxGetEvents() before the signal
714 * is send.
715 * This is no problem because we already set fWokenUp
716 * to true which will let the thread return VERR_INTERRUPTED
717 * and the next call to RTFileAioCtxWait() will not
718 * return VERR_INTERRUPTED because signals are not saved
719 * and will simply vanish if the destination thread can't
720 * receive it.
721 */
722 Assert(hThread != NIL_RTTHREAD);
723 RTThreadPoke(hThread);
724 }
725
726 return VINF_SUCCESS;
727}
728
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