Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
waio.h
Go to the documentation of this file.
1/* Copyright (C) 2022 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
44union sigval // unused
45{
46 int sival_int; // Integer signal value.
47 void* sival_ptr; // Pointer signal value.
48};
49
50struct sigevent // unused
51{
52 int sigev_notify; // notification mode
53 int sigev_signo; // signal number
54 union sigval sigev_value; // signal value
56};
57
58
59//
60// <aio.h>
61//
62
63struct 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
79enum
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
96extern int aio_read(struct aiocb*);
97extern int aio_write(struct aiocb*);
98extern int lio_listio(int, struct aiocb* const[], int, struct sigevent*);
99
100// (if never called, IOCP notifications will pile up.)
101extern int aio_suspend(const struct aiocb* const[], int, const struct timespec*);
102
103// @return status of transfer (0 or an errno)
104extern 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.
109extern ssize_t aio_return(struct aiocb*);
110
111extern int aio_cancel(int, struct aiocb*);
112
113extern 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.
121extern Status waio_open(const OsPath& pathname, int oflag, ...);
122
123extern 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.
141Status waio_Preallocate(int fd, off_t size);
142
143#endif // #ifndef INCLUDED_WAIO
Definition: path.h:80
i64 Status
Error handling system.
Definition: status.h:173
Definition: waio.h:64
off_t aio_offset
Definition: waio.h:66
volatile void * aio_buf
Definition: waio.h:67
size_t aio_nbytes
Definition: waio.h:68
struct sigevent aio_sigevent
Definition: waio.h:70
int aio_reqprio
Definition: waio.h:69
int aio_lio_opcode
Definition: waio.h:71
void * ovl
Definition: waio.h:76
int aio_fildes
Definition: waio.h:65
Definition: waio.h:51
void(* sigev_notify_function)(union sigval)
Definition: waio.h:55
int sigev_signo
Definition: waio.h:53
union sigval sigev_value
Definition: waio.h:54
int sigev_notify
Definition: waio.h:52
Definition: waio.h:45
void * sival_ptr
Definition: waio.h:47
int sival_int
Definition: waio.h:46
ssize_t aio_return(struct aiocb *)
Definition: waio.cpp:647
int aio_read(struct aiocb *)
Definition: waio.cpp:556
int lio_listio(int, struct aiocb *const[], int, struct sigevent *)
Definition: waio.cpp:570
int aio_fsync(int, struct aiocb *)
Definition: waio.cpp:687
@ LIO_WRITE
Definition: waio.h:93
@ LIO_NOWAIT
Definition: waio.h:88
@ LIO_READ
Definition: waio.h:92
@ AIO_CANCELED
Definition: waio.h:83
@ LIO_NOP
Definition: waio.h:91
@ AIO_NOTCANCELED
Definition: waio.h:84
@ LIO_WAIT
Definition: waio.h:87
@ AIO_ALLDONE
Definition: waio.h:82
int aio_error(const struct aiocb *)
Definition: waio.cpp:634
Status waio_close(int fd)
Definition: waio.cpp:425
int aio_suspend(const struct aiocb *const[], int, const struct timespec *)
Definition: waio.cpp:591
int aio_write(struct aiocb *)
Definition: waio.cpp:563
Status waio_open(const OsPath &pathname, int oflag,...)
Definition: waio.cpp:409
int aio_cancel(int, struct aiocb *)
Definition: waio.cpp:671
Status waio_Preallocate(int fd, off_t size)
Definition: waio.cpp:437
__int64 off_t
Definition: wposix_types.h:91
intptr_t ssize_t
Definition: wposix_types.h:82