Pyrogenesis  trunk
waio.h
Go to the documentation of this file.
1 /* Copyright (c) 2011 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * emulate POSIX asynchronous I/O on Windows.
25  */
26 
27 #ifndef INCLUDED_WAIO
28 #define INCLUDED_WAIO
29 
30 #include "lib/status.h"
31 #include "lib/os_path.h"
32 #include "lib/posix/posix_time.h" // timespec
34 
35 // Note: transfer buffers, offsets, and lengths must be sector-aligned
36 // (we don't bother copying to an align buffer because our block cache
37 // already requires splitting IOs into naturally-aligned blocks)
38 
39 
40 //
41 // <signal.h>
42 //
43 
44 union sigval // unused
45 {
46  int sival_int; // Integer signal value.
47  void* sival_ptr; // Pointer signal value.
48 };
49 
50 struct sigevent // unused
51 {
52  int sigev_notify; // notification mode
53  int sigev_signo; // signal number
54  union sigval sigev_value; // signal value
55  void (*sigev_notify_function)(union sigval);
56 };
57 
58 
59 //
60 // <aio.h>
61 //
62 
63 struct aiocb
64 {
65  int aio_fildes; // File descriptor.
66  off_t aio_offset; // File offset.
67  volatile void* aio_buf; // Location of buffer.
68  size_t aio_nbytes; // Length of transfer.
69  int aio_reqprio; // Request priority offset. (unused)
70  struct sigevent aio_sigevent; // Signal number and value. (unused)
71  int aio_lio_opcode; // Operation to be performed.
72 
73  // internal use only; must be zero-initialized before
74  // calling the first aio_read/aio_write/lio_listio
75  // (aio_return resets it to 0)
76  void* ovl;
77 };
78 
79 enum
80 {
81  // aio_cancel return
82  AIO_ALLDONE, // None of the requested operations could be canceled since they are already complete.
83  AIO_CANCELED, // All requested operations have been canceled.
84  AIO_NOTCANCELED, // Some of the requested operations could not be canceled since they are in progress.
85 
86  // lio_listio mode
87  LIO_WAIT, // wait until all I/O is complete
89 
90  // lio_listio ops
94 };
95 
96 extern int aio_read(struct aiocb*);
97 extern int aio_write(struct aiocb*);
98 extern int lio_listio(int, struct aiocb* const[], int, struct sigevent*);
99 
100 // (if never called, IOCP notifications will pile up.)
101 extern int aio_suspend(const struct aiocb* const[], int, const struct timespec*);
102 
103 // @return status of transfer (0 or an errno)
104 extern int aio_error(const struct aiocb*);
105 
106 // @return bytes transferred or -1 on error.
107 // frees internal storage related to the request and MUST be called
108 // exactly once for each aiocb after aio_error != EINPROGRESS.
109 extern ssize_t aio_return(struct aiocb*);
110 
111 extern int aio_cancel(int, struct aiocb*);
112 
113 extern int aio_fsync(int, struct aiocb*);
114 
115 // open the file for aio (not possible via _wsopen_s since it cannot
116 // set FILE_FLAG_NO_BUFFERING).
117 //
118 // @return the smallest available file descriptor. NB: these numbers
119 // are not 0-based to avoid confusion with lowio descriptors and
120 // must only be used with waio functions.
121 extern Status waio_open(const OsPath& pathname, int oflag, ...);
122 
123 extern Status waio_close(int fd);
124 
125 // call this before writing a large file to preallocate clusters, thus
126 // reducing fragmentation.
127 //
128 // @param fd file descriptor from _wsopen_s OR waio_open
129 // @param size is rounded up to a multiple of maxSectorSize (required by
130 // SetEndOfFile; this could be avoided by using the undocumented
131 // NtSetInformationFile or SetFileInformationByHandle on Vista and later).
132 // use wtruncate after I/O is complete to chop off the excess padding.
133 //
134 // NB: writes that extend a file (i.e. ALL WRITES when creating new files)
135 // are synchronous, which prevents overlapping I/O and other work.
136 // (http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B156932)
137 // if Windows XP and the SE_MANAGE_VOLUME_NAME privileges are available,
138 // this function sets the valid data length to avoid the synchronous zero-fill.
139 // to avoid exposing the previous disk contents until the application
140 // successfully writes to the file, deny sharing when opening the file.
141 LIB_API Status waio_Preallocate(int fd, off_t size);
142 
143 #endif // #ifndef INCLUDED_WAIO
Definition: waio.h:88
Definition: waio.h:44
int sigev_signo
Definition: waio.h:53
size_t aio_nbytes
Definition: waio.h:68
Definition: waio.h:82
int aio_read(struct aiocb *)
Definition: waio.cpp:556
int aio_fsync(int, struct aiocb *)
Definition: waio.cpp:687
int lio_listio(int, struct aiocb *const [], int, struct sigevent *)
Definition: waio.cpp:570
volatile void * aio_buf
Definition: waio.h:67
int sival_int
Definition: waio.h:46
ssize_t aio_return(struct aiocb *)
Definition: waio.cpp:647
Definition: waio.h:50
int aio_lio_opcode
Definition: waio.h:71
__int64 off_t
Definition: wposix_types.h:91
Definition: path.h:77
Definition: waio.h:93
Definition: waio.h:83
LIB_API Status waio_Preallocate(int fd, off_t size)
Definition: waio.cpp:437
void * ovl
Definition: waio.h:76
i64 Status
Error handling system.
Definition: status.h:171
int aio_reqprio
Definition: waio.h:69
intptr_t ssize_t
Definition: wposix_types.h:82
void * sival_ptr
Definition: waio.h:47
int aio_error(const struct aiocb *)
Definition: waio.cpp:634
Status waio_open(const OsPath &pathname, int oflag,...)
Definition: waio.cpp:409
Definition: waio.h:63
int aio_write(struct aiocb *)
Definition: waio.cpp:563
int aio_cancel(int, struct aiocb *)
Definition: waio.cpp:671
Status waio_close(int fd)
Definition: waio.cpp:425
off_t aio_offset
Definition: waio.h:66
Definition: wtime.h:62
Definition: waio.h:92
Definition: waio.h:91
int sigev_notify
Definition: waio.h:52
int aio_suspend(const struct aiocb *const [], int, const struct timespec *)
Definition: waio.cpp:591
int aio_fildes
Definition: waio.h:65
Definition: waio.h:87
Definition: waio.h:84