VirtualBox

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

Last change on this file since 34801 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1/* $Id: fileaio-win.cpp 33540 2010-10-28 09:27:05Z 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.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
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 transferred 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 return VERR_NOT_SUPPORTED;
224}
225
226RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
227{
228 PRTFILEAIOREQINTERNAL pReqInt = hReq;
229 RTFILEAIOREQ_VALID_RETURN_RC(pReqInt, NULL);
230
231 return pReqInt->pvUser;
232}
233
234RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq)
235{
236 PRTFILEAIOREQINTERNAL pReqInt = hReq;
237 RTFILEAIOREQ_VALID_RETURN(pReqInt);
238 RTFILEAIOREQ_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_NOT_SUBMITTED);
239
240 /**
241 * @todo r=aeichner It is not possible to cancel specific
242 * requests on Windows before Vista.
243 * CancelIo cancels all requests for a file issued by the
244 * calling thread and CancelIoEx which does what we need
245 * is only available from Vista and up.
246 * The solution is to return VERR_FILE_AIO_IN_PROGRESS
247 * if the request didn't completed yet (checked above).
248 * Shouldn't be a big issue because a request is normally
249 * only canceled if it exceeds a timeout which is quite huge.
250 */
251 return VERR_FILE_AIO_COMPLETED;
252}
253
254RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
255{
256 int rc = VINF_SUCCESS;
257 PRTFILEAIOREQINTERNAL pReqInt = hReq;
258 RTFILEAIOREQ_VALID_RETURN(pReqInt);
259 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
260 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, PREPARED, VERR_FILE_AIO_NOT_SUBMITTED);
261
262 rc = pReqInt->Rc;
263 if (pcbTransfered && RT_SUCCESS(rc))
264 *pcbTransfered = pReqInt->cbTransfered;
265
266 return rc;
267}
268
269RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax)
270{
271 PRTFILEAIOCTXINTERNAL pCtxInt;
272 AssertPtrReturn(phAioCtx, VERR_INVALID_POINTER);
273
274 pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOCTXINTERNAL));
275 if (RT_UNLIKELY(!pCtxInt))
276 return VERR_NO_MEMORY;
277
278 pCtxInt->hIoCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
279 NULL,
280 0,
281 0);
282 if (RT_UNLIKELY(!pCtxInt->hIoCompletionPort))
283 {
284 RTMemFree(pCtxInt);
285 return VERR_NO_MEMORY;
286 }
287
288 pCtxInt->u32Magic = RTFILEAIOCTX_MAGIC;
289
290 *phAioCtx = (RTFILEAIOCTX)pCtxInt;
291
292 return VINF_SUCCESS;
293}
294
295RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
296{
297 /* Validate the handle and ignore nil. */
298 if (hAioCtx == NIL_RTFILEAIOCTX)
299 return VINF_SUCCESS;
300 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
301 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
302
303 /* Cannot destroy a busy context. */
304 if (RT_UNLIKELY(pCtxInt->cRequests))
305 return VERR_FILE_AIO_BUSY;
306
307 CloseHandle(pCtxInt->hIoCompletionPort);
308 ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
309 RTMemFree(pCtxInt);
310
311 return VINF_SUCCESS;
312}
313
314RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile)
315{
316 int rc = VINF_SUCCESS;
317 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
318 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
319
320 HANDLE hTemp = CreateIoCompletionPort((HANDLE)hFile, pCtxInt->hIoCompletionPort, 0, 1);
321 if (hTemp != pCtxInt->hIoCompletionPort)
322 rc = RTErrConvertFromWin32(GetLastError());
323
324 return rc;
325}
326
327RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
328{
329 return RTFILEAIO_UNLIMITED_REQS;
330}
331
332RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
333{
334 /*
335 * Parameter validation.
336 */
337 int rc = VINF_SUCCESS;
338 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
339 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
340 AssertReturn(cReqs > 0, VERR_INVALID_PARAMETER);
341 Assert(cReqs <= INT32_MAX);
342 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
343 size_t i;
344
345 for (i = 0; i < cReqs; i++)
346 {
347 PRTFILEAIOREQINTERNAL pReqInt = pahReqs[i];
348 BOOL fSucceeded;
349
350 Assert(pReqInt->cbTransfer == (DWORD)pReqInt->cbTransfer);
351 if (pReqInt->enmTransferDirection == TRANSFERDIRECTION_READ)
352 {
353 fSucceeded = ReadFile(pReqInt->hFile, pReqInt->pvBuf,
354 (DWORD)pReqInt->cbTransfer, NULL,
355 &pReqInt->Overlapped);
356 }
357 else if (pReqInt->enmTransferDirection == TRANSFERDIRECTION_WRITE)
358 {
359 fSucceeded = WriteFile(pReqInt->hFile, pReqInt->pvBuf,
360 (DWORD)pReqInt->cbTransfer, NULL,
361 &pReqInt->Overlapped);
362 }
363 else
364 AssertMsgFailed(("Invalid transfer direction\n"));
365
366 if (RT_UNLIKELY(!fSucceeded && GetLastError() != ERROR_IO_PENDING))
367 {
368 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
369 rc = RTErrConvertFromWin32(GetLastError());
370 pReqInt->Rc = rc;
371 break;
372 }
373 RTFILEAIOREQ_SET_STATE(pReqInt, SUBMITTED);
374 }
375
376 ASMAtomicAddS32(&pCtxInt->cRequests, (int32_t)i);
377
378 return rc;
379}
380
381RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
382 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
383{
384 /*
385 * Validate the parameters, making sure to always set pcReqs.
386 */
387 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
388 *pcReqs = 0; /* always set */
389 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
390 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
391 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
392 AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
393 AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
394
395 /*
396 * Can't wait if there are no requests around.
397 */
398 if (RT_UNLIKELY(ASMAtomicUoReadS32(&pCtxInt->cRequests) == 0))
399 return VERR_FILE_AIO_NO_REQUEST;
400
401 /* Wait for at least one. */
402 if (!cMinReqs)
403 cMinReqs = 1;
404
405 /*
406 * Loop until we're woken up, hit an error (incl timeout), or
407 * have collected the desired number of requests.
408 */
409 int rc = VINF_SUCCESS;
410 int cRequestsCompleted = 0;
411 while ( !pCtxInt->fWokenUp
412 && cMinReqs > 0)
413 {
414 uint64_t StartNanoTS = 0;
415 DWORD dwTimeout = cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies;
416 DWORD cbTransfered;
417 LPOVERLAPPED pOverlapped;
418 ULONG_PTR lCompletionKey;
419 BOOL fSucceeded;
420
421 if (cMillies != RT_INDEFINITE_WAIT)
422 StartNanoTS = RTTimeNanoTS();
423
424 ASMAtomicXchgBool(&pCtxInt->fWaiting, true);
425 fSucceeded = GetQueuedCompletionStatus(pCtxInt->hIoCompletionPort,
426 &cbTransfered,
427 &lCompletionKey,
428 &pOverlapped,
429 dwTimeout);
430 ASMAtomicXchgBool(&pCtxInt->fWaiting, false);
431 if ( !fSucceeded
432 && !pOverlapped)
433 {
434 /* The call failed to dequeue a completion packet, includes VERR_TIMEOUT */
435 rc = RTErrConvertFromWin32(GetLastError());
436 break;
437 }
438
439 /* Check if we got woken up. */
440 if (lCompletionKey == AIO_CONTEXT_WAKEUP_EVENT)
441 {
442 Assert(fSucceeded && !pOverlapped);
443 break;
444 }
445
446 /* A request completed. */
447 PRTFILEAIOREQINTERNAL pReqInt = OVERLAPPED_2_RTFILEAIOREQINTERNAL(pOverlapped);
448 AssertPtr(pReqInt);
449 Assert(pReqInt->u32Magic == RTFILEAIOREQ_MAGIC);
450
451 /* Mark the request as finished. */
452 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
453
454 pReqInt->cbTransfered = cbTransfered;
455 if (fSucceeded)
456 pReqInt->Rc = VINF_SUCCESS;
457 else
458 pReqInt->Rc = RTErrConvertFromWin32(GetLastError());
459
460 pahReqs[cRequestsCompleted++] = (RTFILEAIOREQ)pReqInt;
461
462 /* Update counter. */
463 cMinReqs--;
464
465 if (cMillies != RT_INDEFINITE_WAIT)
466 {
467 /* Recalculate timeout. */
468 uint64_t NanoTS = RTTimeNanoTS();
469 uint64_t cMilliesElapsed = (NanoTS - StartNanoTS) / 1000000;
470 if (cMilliesElapsed < cMillies)
471 cMillies -= cMilliesElapsed;
472 else
473 cMillies = 0;
474 }
475 }
476
477 /*
478 * Update the context state and set the return value.
479 */
480 *pcReqs = cRequestsCompleted;
481 ASMAtomicSubS32(&pCtxInt->cRequests, cRequestsCompleted);
482
483 /*
484 * Clear the wakeup flag and set rc.
485 */
486 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, false);
487
488 if ( fWokenUp
489 && RT_SUCCESS(rc))
490 rc = VERR_INTERRUPTED;
491
492 return rc;
493}
494
495RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
496{
497 int rc = VINF_SUCCESS;
498 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
499 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
500
501 bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, true);
502 bool fWaiting = ASMAtomicReadBool(&pCtxInt->fWaiting);
503
504 if ( !fWokenUp
505 && fWaiting)
506 {
507 BOOL fSucceeded = PostQueuedCompletionStatus(pCtxInt->hIoCompletionPort,
508 0, AIO_CONTEXT_WAKEUP_EVENT,
509 NULL);
510
511 if (!fSucceeded)
512 rc = RTErrConvertFromWin32(GetLastError());
513 }
514
515 return rc;
516}
517
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