Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
waio.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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"
33
34#include <ctime>
35
36// Note: transfer buffers, offsets, and lengths must be sector-aligned
37// (we don't bother copying to an align buffer because our block cache
38// already requires splitting IOs into naturally-aligned blocks)
39
40
41//
42// <signal.h>
43//
44
45union sigval // unused
46{
47 int sival_int; // Integer signal value.
48 void* sival_ptr; // Pointer signal value.
49};
50
51struct sigevent // unused
52{
53 int sigev_notify; // notification mode
54 int sigev_signo; // signal number
55 union sigval sigev_value; // signal value
57};
58
59
60//
61// <aio.h>
62//
63
64struct aiocb
65{
66 int aio_fildes; // File descriptor.
67 off_t aio_offset; // File offset.
68 volatile void* aio_buf; // Location of buffer.
69 size_t aio_nbytes; // Length of transfer.
70 int aio_reqprio; // Request priority offset. (unused)
71 struct sigevent aio_sigevent; // Signal number and value. (unused)
72 int aio_lio_opcode; // Operation to be performed.
73
74 // internal use only; must be zero-initialized before
75 // calling the first aio_read/aio_write/lio_listio
76 // (aio_return resets it to 0)
77 void* ovl;
78};
79
80enum
81{
82 // aio_cancel return
83 AIO_ALLDONE, // None of the requested operations could be canceled since they are already complete.
84 AIO_CANCELED, // All requested operations have been canceled.
85 AIO_NOTCANCELED, // Some of the requested operations could not be canceled since they are in progress.
86
87 // lio_listio mode
88 LIO_WAIT, // wait until all I/O is complete
90
91 // lio_listio ops
95};
96
97extern int aio_read(struct aiocb*);
98extern int aio_write(struct aiocb*);
99extern int lio_listio(int, struct aiocb* const[], int, struct sigevent*);
100
101// (if never called, IOCP notifications will pile up.)
102extern int aio_suspend(const struct aiocb* const[], int, const struct timespec*);
103
104// @return status of transfer (0 or an errno)
105extern int aio_error(const struct aiocb*);
106
107// @return bytes transferred or -1 on error.
108// frees internal storage related to the request and MUST be called
109// exactly once for each aiocb after aio_error != EINPROGRESS.
110extern ssize_t aio_return(struct aiocb*);
111
112extern int aio_cancel(int, struct aiocb*);
113
114extern int aio_fsync(int, struct aiocb*);
115
116// open the file for aio (not possible via _wsopen_s since it cannot
117// set FILE_FLAG_NO_BUFFERING).
118//
119// @return the smallest available file descriptor. NB: these numbers
120// are not 0-based to avoid confusion with lowio descriptors and
121// must only be used with waio functions.
122extern Status waio_open(const OsPath& pathname, int oflag, ...);
123
124extern Status waio_close(int fd);
125
126// call this before writing a large file to preallocate clusters, thus
127// reducing fragmentation.
128//
129// @param fd file descriptor from _wsopen_s OR waio_open
130// @param size is rounded up to a multiple of maxSectorSize (required by
131// SetEndOfFile; this could be avoided by using the undocumented
132// NtSetInformationFile or SetFileInformationByHandle on Vista and later).
133// use truncate after I/O is complete to chop off the excess padding.
134//
135// NB: writes that extend a file (i.e. ALL WRITES when creating new files)
136// are synchronous, which prevents overlapping I/O and other work.
137// (http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B156932)
138// if Windows XP and the SE_MANAGE_VOLUME_NAME privileges are available,
139// this function sets the valid data length to avoid the synchronous zero-fill.
140// to avoid exposing the previous disk contents until the application
141// successfully writes to the file, deny sharing when opening the file.
142Status waio_Preallocate(int fd, off_t size);
143
144#endif // #ifndef INCLUDED_WAIO
Definition: path.h:84
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: waio.h:65
off_t aio_offset
Definition: waio.h:67
volatile void * aio_buf
Definition: waio.h:68
size_t aio_nbytes
Definition: waio.h:69
struct sigevent aio_sigevent
Definition: waio.h:71
int aio_reqprio
Definition: waio.h:70
int aio_lio_opcode
Definition: waio.h:72
void * ovl
Definition: waio.h:77
int aio_fildes
Definition: waio.h:66
Definition: waio.h:52
void(* sigev_notify_function)(union sigval)
Definition: waio.h:56
int sigev_signo
Definition: waio.h:54
union sigval sigev_value
Definition: waio.h:55
int sigev_notify
Definition: waio.h:53
Definition: waio.h:46
void * sival_ptr
Definition: waio.h:48
int sival_int
Definition: waio.h:47
@ LIO_WRITE
Definition: waio.h:94
@ LIO_NOWAIT
Definition: waio.h:89
@ LIO_READ
Definition: waio.h:93
@ AIO_CANCELED
Definition: waio.h:84
@ LIO_NOP
Definition: waio.h:92
@ AIO_NOTCANCELED
Definition: waio.h:85
@ LIO_WAIT
Definition: waio.h:88
@ AIO_ALLDONE
Definition: waio.h:83
ssize_t aio_return(struct aiocb *)
Definition: waio.cpp:638
int aio_read(struct aiocb *)
Definition: waio.cpp:548
int lio_listio(int, struct aiocb *const[], int, struct sigevent *)
Definition: waio.cpp:562
int aio_fsync(int, struct aiocb *)
Definition: waio.cpp:678
int aio_error(const struct aiocb *)
Definition: waio.cpp:625
Status waio_close(int fd)
Definition: waio.cpp:417
int aio_suspend(const struct aiocb *const[], int, const struct timespec *)
Definition: waio.cpp:582
int aio_write(struct aiocb *)
Definition: waio.cpp:555
Status waio_open(const OsPath &pathname, int oflag,...)
Definition: waio.cpp:401
int aio_cancel(int, struct aiocb *)
Definition: waio.cpp:662
Status waio_Preallocate(int fd, off_t size)
Definition: waio.cpp:429
__int64 off_t
Definition: wposix_types.h:47
intptr_t ssize_t
Definition: wposix_types.h:36