bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
sekaiacg | cf8c128 | 2021-12-23 18:57:05 +0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _POSIXASYNCIO_H |
| 18 | #define _POSIXASYNCIO_H |
| 19 | |
sekaiacg | cf8c128 | 2021-12-23 18:57:05 +0800 | [diff] [blame] | 20 | #include <condition_variable> |
| 21 | #include <mutex> |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 22 | #include <sys/cdefs.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <time.h> |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
| 27 | /** |
| 28 | * Provides a subset of POSIX aio operations. |
| 29 | */ |
| 30 | |
| 31 | struct aiocb { |
sekaiacg | cf8c128 | 2021-12-23 18:57:05 +0800 | [diff] [blame] | 32 | int aio_fildes; |
| 33 | void *aio_buf; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 34 | |
sekaiacg | cf8c128 | 2021-12-23 18:57:05 +0800 | [diff] [blame] | 35 | off64_t aio_offset; |
| 36 | size_t aio_nbytes; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 37 | |
sekaiacg | cf8c128 | 2021-12-23 18:57:05 +0800 | [diff] [blame] | 38 | // Used internally |
| 39 | bool read; |
| 40 | bool queued; |
| 41 | ssize_t ret; |
| 42 | int error; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 43 | |
sekaiacg | cf8c128 | 2021-12-23 18:57:05 +0800 | [diff] [blame] | 44 | std::mutex lock; |
| 45 | std::condition_variable cv; |
| 46 | |
| 47 | aiocb(); |
| 48 | ~aiocb(); |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // Submit a request for IO to be completed |
| 52 | int aio_read(struct aiocb *); |
| 53 | int aio_write(struct aiocb *); |
| 54 | |
| 55 | // Suspend current thread until given IO is complete, at which point |
| 56 | // its return value and any errors can be accessed |
| 57 | // All submitted requests must have a corresponding suspend. |
| 58 | // aiocb->aio_buf must refer to valid memory until after the suspend call |
| 59 | int aio_suspend(struct aiocb *[], int, const struct timespec *); |
| 60 | int aio_error(const struct aiocb *); |
| 61 | ssize_t aio_return(struct aiocb *); |
| 62 | |
| 63 | // Helper method for setting aiocb members |
sekaiacg | cf8c128 | 2021-12-23 18:57:05 +0800 | [diff] [blame] | 64 | void aio_prepare(struct aiocb *, void*, size_t, off64_t); |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 65 | |
| 66 | #endif // POSIXASYNCIO_H |
| 67 | |