VirtualBox

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

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

Trying to fix OSE take 2

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