VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/fileaio-win.cpp@ 28800

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 KB
Line 
1/* $Id: fileaio-win.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - File async I/O, native implementation for the Windows host platform.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DIR
32
33#include <iprt/asm.h>
34#include <iprt/file.h>
35#include <iprt/mem.h>
36#include <iprt/assert.h>
37#include <iprt/string.h>
38#include <iprt/err.h>
39#include <iprt/log.h>
40#include "internal/fileaio.h"
41
42#include <Windows.h>
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47
48/**
49 * Transfer direction.
50 */
51typedef enum TRANSFERDIRECTION
52{
53 TRANSFERDIRECTION_INVALID = 0,
54 /** Read. */
55 TRANSFERDIRECTION_READ,
56 /** Write. */
57 TRANSFERDIRECTION_WRITE,
58 /** The usual 32-bit hack. */
59 TRANSFERDIRECTION_32BIT_HACK = 0x7fffffff
60} TRANSFERDIRECTION;
61
62/**
63 * Async I/O completion context state.
64 */
65typedef struct RTFILEAIOCTXINTERNAL
66{
67 /** handle to I/O completion port. */
68 HANDLE hIoCompletionPort;
69 /** Current number of requests pending. */
70 volatile int32_t cRequests;
71 /** Flag whether the thread was woken up. */
72 volatile bool fWokenUp;
73 /** Flag whether the thread is currently waiting. */
74 volatile bool fWaiting;
75 /** Magic value (RTFILEAIOCTX_MAGIC). */
76 uint32_t u32Magic;
77} RTFILEAIOCTXINTERNAL;
78/** Pointer to an internal context structure. */
79typedef RTFILEAIOCTXINTERNAL *PRTFILEAIOCTXINTERNAL;
80
81/**
82 * Async I/O request state.
83 */
84typedef struct RTFILEAIOREQINTERNAL
85{
86 /** Overlapped structure. */
87 OVERLAPPED Overlapped;
88 /** Current state the request is in. */
89 RTFILEAIOREQSTATE enmState;
90 /** The file handle. */
91 HANDLE hFile;
92 /** Kind of transfer Read/Write. */
93 TRANSFERDIRECTION enmTransferDirection;
94 /** Number of bytes to transfer. */
95 size_t cbTransfer;
96 /** Pointer to the buffer. */
97 void *pvBuf;
98 /** Opaque user data. */
99 void *pvUser;
100 /** Flag whether the request completed. */
101 bool fCompleted;
102 /** Number of bytes transfered successfully. */
103 size_t cbTransfered;
104 /** Error code of the completed request. */
105 int Rc;
106 /** Completion context we are assigned to. */
107 PRTFILEAIOCTXINTERNAL pCtxInt;
108 /** Magic value (RTFILEAIOREQ_MAGIC). */
109 uint32_t u32Magic;
110} RTFILEAIOREQINTERNAL;
111/** Pointer to an internal request structure. */
112typedef RTFILEAIOREQINTERNAL *PRTFILEAIOREQINTERNAL;
113
114/*******************************************************************************
115* Defined Constants And Macros *
116*******************************************************************************/
117/** Id for the wakeup event. */
118#define AIO_CONTEXT_WAKEUP_EVENT 1
119/** Converts a pointer to an OVERLAPPED structure to a internal request. */
120#define OVERLAPPED_2_RTFILEAIOREQINTERNAL(pOverlapped) ( (PRTFILEAIOREQINTERNAL)((uintptr_t)(pOverlapped) - RT_OFFSETOF(RTFILEAIOREQINTERNAL, Overlapped)) )
121
122RTR3DECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits)
123{
124 int rcBSD = 0;
125 AssertPtrReturn(pAioLimits, VERR_INVALID_POINTER);
126
127 /* No limits known. */
128 pAioLimits->cReqsOutstandingMax = RTFILEAIO_UNLIMITED_REQS;
129 pAioLimits->cbBufferAlignment = 0;
130
131 return VINF_SUCCESS;
132}
133
134RTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
135{
136 AssertPtrReturn(phReq, VERR_INVALID_POINTER);
137
138 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOREQINTERNAL));
139 if (RT_UNLIKELY(!pReqInt))
140 return VERR_NO_MEMORY;
141
142 pReqInt->pCtxInt = NULL;
143 pReqInt->fCompleted = false;
144 pReqInt->u32Magic = RTFILEAIOREQ_MAGIC;
145 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
146
147 *phReq = (RTFILEAIOREQ)pReqInt;
148
149 return VINF_SUCCESS;
150}
151
152RTDECL(int) RTFileAioReqDestroy(RTFILEAIOREQ hReq)
153{
154 /*
155 * Validate the handle and ignore nil.
156 */
157 if (hReq == NIL_RTFILEAIOREQ)
158 return VINF_SUCCESS;
159 PRTFILEAIOREQINTERNAL pReqInt = hReq;
160 RTFILEAIOREQ_VALID_RETURN(pReqInt);
161 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
162
163 /*
164 * Trash the magic and free it.
165 */
166 ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
167 RTMemFree(pReqInt);
168 return VINF_SUCCESS;
169}
170
171/**
172 * Worker setting up the request.
173 */
174DECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
175 TRANSFERDIRECTION enmTransferDirection,
176 RTFOFF off, void *pvBuf, size_t cbTransfer,
177 void *pvUser)
178{
179 /*
180 * Validate the input.
181 */
182 PRTFILEAIOREQINTERNAL pReqInt = hReq;
183 RTFILEAIOREQ_VALID_RETURN(pReqInt);
184 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
185 Assert(hFile != NIL_RTFILE);
186 AssertPtr(pvBuf);
187 Assert(off >= 0);
188 Assert(cbTransfer > 0);
189
190 pReqInt->enmTransferDirection = enmTransferDirection;
191 pReqInt->hFile = (HANDLE)hFile;
192 pReqInt->Overlapped.Offset = (DWORD)(off & 0xffffffff);
193 pReqInt->Overlapped.OffsetHigh = (DWORD)(off >> 32);
194 pReqInt->cbTransfer = cbTransfer;
195 pReqInt->pvBuf = pvBuf;
196 pReqInt->pvUser = pvUser;
197 pReqInt->fCompleted = false;
198
199 return VINF_SUCCESS;
200}
201
202RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
203 void *pvBuf, size_t cbRead, void *pvUser)
204{
205 return rtFileAioReqPrepareTransfer(hReq, hFile, TRANSFERDIRECTION_READ,
206 off, pvBuf, cbRead, pvUser);
207}
208
209RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
210 void const *pvBuf, size_t cbWrite, void *pvUser)
211{
212 return rtFileAioReqPrepareTransfer(hReq, hFile, TRANSFERDIRECTION_WRITE,
213 off, (void *)pvBuf, cbWrite, pvUser);
214}
215
216RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
217{
218 PRTFILEAIOREQINTERNAL pReqInt = hReq;
219 RTFILEAIOREQ_VALID_RETURN(pReqInt);
220 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
221 AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_HANDLE);
222
223 /** @todo: Flushing is not available */
224#if 0
225 return rtFileAsyncPrepareTransfer(pRequest, File, TRANSFERDIRECTION_FLUSH,
226 0, NULL, 0, pvUser);
227#endif
228 return VERR_NOT_IMPLEMENTED;
229}
230
231RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
232{
233 PRTFILEAIOREQINTERNAL pReqInt = hReq;
234 RTFILEAIOREQ_VALID_RETURN_RC(pReqInt, NULL);
235
236 return pReqInt->pvUser;
237}
238
239RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq)
240{
241 PRTFILEAIOREQINTERNAL pReqInt = hReq;
242 RTFILEAIOREQ_VALID_RETURN(pReqInt);
243 RTFILEAIOREQ_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_NOT_SUBMITTED);
244
245 /**
246 * @todo r=aeichner It is not possible to cancel specific
247 * requests on Windows before Vista.
248 * CancelIo cancels all requests for a file issued by the
249 * calling thread and CancelIoEx which does what we need
250 * is only available from Vista and up.
251 * The solution is to return VERR_FILE_AIO_IN_PROGRESS
252 * if the request didn't completed yet (checked above).
253 * Shouldn't be a big issue because a request is normally
254 * only canceled if it exceeds a timeout which is quite huge.
255 */
256 return VERR_FILE_AIO_COMPLETED;
257}
258
259RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
260{
261 int rc = VINF_SUCCESS;
262 PRTFILEAIOREQINTERNAL pReqInt = hReq;
263 RTFILEAIOREQ_VALID_RETURN(pReqInt);
264 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
265 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, PREPARED, VERR_FILE_AIO_NOT_SUBMITTED);
266
267 rc = pReqInt->Rc;
268 if (pcbTransfered && RT_SUCCESS(rc))
269 *pcbTransfered = pReqInt->cbTransfered;
270
271 return rc;
272}
273
274RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax)
275{
276 PRTFILEAIOCTXINTERNAL pCtxInt;
277 AssertPtrReturn(phAioCtx, VERR_INVALID_POINTER);
278
279 pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOCTXINTERNAL));
280 if (RT_UNLIKELY(!pCtxInt))
281 return VERR_NO_MEMORY;
282
283 pCtxInt->hIoCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
284 NULL,
285 0,
286 0);
287 if (RT_UNLIKELY(!pCtxInt->hIoCompletionPort))
288 {
289 RTMemFree(pCtxInt);
290 return VERR_NO_MEMORY;
291 }
292
293 pCtxInt->u32Magic = RTFILEAIOCTX_MAGIC;
294
295 *phAioCtx = (RTFILEAIOCTX)pCtxInt;
296
297 return VINF_SUCCESS;
298}
299
300RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
301{
302 /* Validate the handle and ignore nil. */
303 if (hAioCtx == NIL_RTFILEAIOCTX)
304 return VINF_SUCCESS;
305 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
306 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
307
308 /* Cannot destroy a busy context. */
309 if (RT_UNLIKELY(pCtxInt->cRequests))
310 return VERR_FILE_AIO_BUSY;
311
312 CloseHandle(pCtxInt->hIoCompletionPort);
313 ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
314 RTMemFree(pCtxInt);
315
316 return VINF_SUCCESS;
317}
318
319RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile)
320{
321 int rc = VINF_SUCCESS;
322 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
323 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
324
325 HANDLE hTemp = CreateIoCompletionPort((HANDLE)hFile, pCtxInt->hIoCompletionPort, 0, 1);
326 if (hTemp != pCtxInt->hIoCompletionPort)
327 rc = RTErrConvertFromWin32(GetLastError());
328
329 return rc;
330}
331
332RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
333{
334 return RTFILEAIO_UNLIMITED_REQS;
335}
336
337RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
338{
339 /*
340 * Parameter validation.
341 */
342 int rc = VINF_SUCCESS;
343 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
344 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
345 AssertReturn(cReqs > 0, VERR_INVALID_PARAMETER);
346 Assert(cReqs <= INT32_MAX);
347 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
348 size_t i;
349
350 for (i = 0; i < cReqs; i++)
351 {
352 PRTFILEAIOREQINTERNAL pReqInt = pahReqs[i];
353 BOOL fSucceeded;
354
355 Assert(pReqInt->cbTransfer == (DWORD)pReqInt->cbTransfer);
356 if (pReqInt->enmTransferDirection == TRANSFERDIRECTION_READ)
357 {
358 fSucceeded = ReadFile(pReqInt->hFile, pReqInt->pvBuf,
359 (DWORD)pReqInt->cbTransfer, NULL,
360 &pReqInt->Overlapped);
361 }
362 else if (pReqInt->enmTransferDirection == TRANSFERDIRECTION_WRITE)
363 {
364 fSucceeded = WriteFile(pReqInt->hFile, pReqInt->pvBuf,
365 (DWORD)pReqInt->cbTransfer, NULL,
366 &pReqInt->Overlapped);
367 }
368 else
369 AssertMsgFailed(("Invalid transfer direction\n"));
370
371 if (RT_UNLIKELY(!fSucceeded && GetLastError() != ERROR_IO_PENDING))
372 {
373 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
374 rc = RTErrConvertFromWin32(GetLastError());
375 pReqInt->Rc = rc;
376 break;
377 }
378 RTFILEAIOREQ_SET_STATE(pReqInt, SUBMITTED);
379 }
380
381 ASMAtomicAddS32(&pCtxInt->cRequests, (int32_t)i);
382
383 return rc;
384}
385
386RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
387 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
388{
389 /*
390 * Validate the parameters, making sure to always set pcReqs.
391 */
392 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
393 *pcReqs = 0; /* always set */
394 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
395 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
396 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
397 AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
398 AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
399
400 /*
401 * Can't wait if there are no requests around.
402 */
403 if (RT_UNLIKELY(ASMAtomicUoReadS32(&pCtxInt->cRequests) == 0))
404 return VERR_FILE_AIO_NO_REQUEST;
405
406 /* Wait for at least one. */
407 if (!cMinReqs)
408 cMinReqs = 1;
409
410 /*
411 * Loop until we're woken up, hit an error (incl timeout), or
412 * have collected the desired number of requests.
413 */
414 int rc = VINF_SUCCESS;
415 int cRequestsCompleted = 0;
416 while ( !pCtxInt->fWokenUp
417 && cMinReqs > 0)
418 {
419 uint64_t StartNanoTS = 0;
420 DWORD dwTimeout = cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies;
421 DWORD cbTransfered;
422 LPOVERLAPPED pOverlapped;
423 ULONG_PTR lCompletionKey;
424 BOOL fSucceeded;
425
426 if (cMillies != RT_INDEFINITE_WAIT)
427 StartNanoTS = RTTimeNanoTS();
428
429 ASMAtomicXchgBool(&pCtxInt->fWaiting, true);
430 fSucceeded = GetQueuedCompletionStatus(pCtxInt->hIoCompletionPort,
431 &cbTransfered,
432 &lCompletionKey,
433 &pOverlapped,
434 dwTimeout);
435 ASMAtomicXchgBool(&pCtxInt->fWaiting, false);
436 if (!fSucceeded)
437 {
438 /* Includes VERR_TIMEOUT */
439 rc = RTErrConvertFromWin32(GetLastError());
440 break;
441 }
442
443 /* Check if we got woken up. */
444 if (lCompletionKey == AIO_CONTEXT_WAKEUP_EVENT)
445 break;
446
447 /* A request completed. */
448 PRTFILEAIOREQINTERNAL pReqInt = OVERLAPPED_2_RTFILEAIOREQINTERNAL(pOverlapped);
449 AssertPtr(pReqInt);
450 Assert(pReqInt->u32Magic == RTFILEAIOREQ_MAGIC);
451
452 /* Mark the request as finished. */
453 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
454
455 /* completion status. */
456 fSucceeded = GetOverlappedResult(pReqInt->hFile,
457 &pReqInt->Overlapped,
458 &cbTransfered,
459 FALSE);
460 pReqInt->cbTransfered = cbTransfered;
461 pReqInt->Rc = VINF_SUCCESS;
462
463 pahReqs[cRequestsCompleted++] = (RTFILEAIOREQ)pReqInt;
464
465 /* Update counter. */
466 cMinReqs--;
467
468 if (cMillies != RT_INDEFINITE_WAIT)
469 {
470 /* Recalculate timeout. */
471 uint64_t NanoTS = RTTimeNanoTS();
472 uint64_t cMilliesElapsed = (NanoTS - StartNanoTS) / 1000000;
473 if (cMilliesElapsed < cMillies)
474 cMillies -= cMilliesElapsed;
475 else
476 cMillies = 0;
477 }
478 }
479
480 /*
481 * Update the context state and set the return value.
482 */
483 *pcReqs = cRequestsCompleted;
484 ASMAtomicSubS32(&pCtxInt->cRequests, cRequestsCompleted);
485
486 /*
487 * Clear the wakeup flag and set rc.
488 */
489 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, false);
490
491 if ( fWokenUp
492 && RT_SUCCESS(rc))
493 rc = VERR_INTERRUPTED;
494
495 return rc;
496}
497
498RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
499{
500 int rc = VINF_SUCCESS;
501 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
502 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
503
504 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, true);
505 bool fWaiting = ASMAtomicReadBool(&pCtxInt->fWaiting);
506
507 if ( !fWokenUp
508 && fWaiting)
509 {
510 BOOL fSucceeded = PostQueuedCompletionStatus(pCtxInt->hIoCompletionPort,
511 0, AIO_CONTEXT_WAKEUP_EVENT,
512 NULL);
513
514 if (!fSucceeded)
515 rc = RTErrConvertFromWin32(GetLastError());
516 }
517
518 return rc;
519}
520
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