VirtualBox

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

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

try to fix the OSE burn

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