1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (nbio_linux.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <file/nbio.h>
25 #if defined(__linux__)
38 #include <sys/syscall.h>
39 #include <linux/aio_abi.h>
51 /* there's also a Unix AIO thingy, but it's not in glibc
52 * and we don't want more dependencies */
54 static int io_setup(unsigned nr, aio_context_t * ctxp)
56 return syscall(__NR_io_setup, nr, ctxp);
59 static int io_destroy(aio_context_t ctx)
61 return syscall(__NR_io_destroy, ctx);
64 static int io_submit(aio_context_t ctx, long nr, struct iocb ** cbp)
66 return syscall(__NR_io_submit, ctx, nr, cbp);
69 static int io_cancel(aio_context_t ctx, struct iocb * iocb, struct io_event * result)
71 return syscall(__NR_io_cancel, ctx, iocb, result);
74 static int io_getevents(aio_context_t ctx, long min_nr, long nr,
75 struct io_event * events, struct timespec * timeout)
77 return syscall(__NR_io_getevents, ctx, min_nr, nr, events, timeout);
80 static void nbio_begin_op(struct nbio_linux_t* handle, uint16_t op)
82 struct iocb * cbp = &handle->cb;
84 memset(&handle->cb, 0, sizeof(handle->cb));
86 handle->cb.aio_fildes = handle->fd;
87 handle->cb.aio_lio_opcode = op;
89 handle->cb.aio_buf = (uint64_t)(uintptr_t)handle->ptr;
90 handle->cb.aio_offset = 0;
91 handle->cb.aio_nbytes = handle->len;
93 if (io_submit(handle->ctx, 1, &cbp) != 1)
99 static void *nbio_linux_open(const char * filename, unsigned mode)
101 static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC };
103 aio_context_t ctx = 0;
104 struct nbio_linux_t* handle = NULL;
105 int fd = open(filename, o_flags[mode]|O_CLOEXEC, 0644);
109 if (io_setup(128, &ctx) < 0)
115 handle = (struct nbio_linux_t*)malloc(sizeof(struct nbio_linux_t));
118 handle->len = lseek(fd, 0, SEEK_END);
119 handle->ptr = malloc(handle->len);
120 handle->busy = false;
125 static void nbio_linux_begin_read(void *data)
127 struct nbio_linux_t* handle = (struct nbio_linux_t*)data;
129 nbio_begin_op(handle, IOCB_CMD_PREAD);
132 static void nbio_linux_begin_write(void *data)
134 struct nbio_linux_t* handle = (struct nbio_linux_t*)data;
136 nbio_begin_op(handle, IOCB_CMD_PWRITE);
139 static bool nbio_linux_iterate(void *data)
141 struct nbio_linux_t* handle = (struct nbio_linux_t*)data;
147 if (io_getevents(handle->ctx, 0, 1, &ev, NULL) == 1)
148 handle->busy = false;
150 return !handle->busy;
153 static void nbio_linux_resize(void *data, size_t len)
155 struct nbio_linux_t* handle = (struct nbio_linux_t*)data;
159 /* This works perfectly fine if this check is removed, but it
160 * won't work on other nbio implementations */
161 /* therefore, it's blocked so nobody accidentally relies on it */
162 if (len < handle->len)
165 if (ftruncate(handle->fd, len) != 0)
166 abort(); /* this one returns void and I can't find any other way
167 for it to report failure */
169 handle->ptr = realloc(handle->ptr, len);
173 static void *nbio_linux_get_ptr(void *data, size_t* len)
175 struct nbio_linux_t* handle = (struct nbio_linux_t*)data;
185 static void nbio_linux_cancel(void *data)
187 struct nbio_linux_t* handle = (struct nbio_linux_t*)data;
194 io_cancel(handle->ctx, &handle->cb, &ev);
195 handle->busy = false;
199 static void nbio_linux_free(void *data)
201 struct nbio_linux_t* handle = (struct nbio_linux_t*)data;
205 io_destroy(handle->ctx);
211 nbio_intf_t nbio_linux = {
213 nbio_linux_begin_read,
214 nbio_linux_begin_write,
223 nbio_intf_t nbio_linux = {