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 * 8 * http://www.apache.org/licenses/LICENSE-2.0 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 _ASYNCIO_H 18 #define _ASYNCIO_H 19 20 #include <fcntl.h> 21 #include <linux/aio_abi.h> 22 #include <memory> 23 #include <signal.h> 24 #include <sys/cdefs.h> 25 #include <sys/types.h> 26 #include <time.h> 27 #include <thread> 28 #include <unistd.h> 29 30 /** 31 * Provides a subset of POSIX aio operations, as well 32 * as similar operations with splice and threadpools. 33 */ 34 35 struct aiocb { 36 int aio_fildes; // Assumed to be the source for splices 37 void *aio_buf; // Unused for splices 38 39 // Used for threadpool operations only, freed automatically 40 std::unique_ptr<char[]> aio_pool_buf; 41 42 off_t aio_offset; 43 size_t aio_nbytes; 44 45 int aio_sink; // Unused for non splice r/w 46 47 // Used internally 48 std::thread thread; 49 ssize_t ret; 50 int error; 51 52 ~aiocb(); 53 }; 54 55 // Submit a request for IO to be completed 56 int aio_read(struct aiocb *); 57 int aio_write(struct aiocb *); 58 int aio_splice_read(struct aiocb *); 59 int aio_splice_write(struct aiocb *); 60 61 // Suspend current thread until given IO is complete, at which point 62 // its return value and any errors can be accessed 63 // All submitted requests must have a corresponding suspend. 64 // aiocb->aio_buf must refer to valid memory until after the suspend call 65 int aio_suspend(struct aiocb *[], int, const struct timespec *); 66 int aio_error(const struct aiocb *); 67 ssize_t aio_return(struct aiocb *); 68 69 // (Currently unimplemented) 70 int aio_cancel(int, struct aiocb *); 71 72 // Initialize a threadpool to perform IO. Only one pool can be 73 // running at a time. 74 void aio_pool_write_init(); 75 void aio_pool_splice_init(); 76 // Suspend current thread until all queued work is complete, then ends the threadpool 77 void aio_pool_end(); 78 // Submit IO work for the threadpool to complete. Memory associated with the work is 79 // freed automatically when the work is complete. 80 int aio_pool_write(struct aiocb *); 81 82 #endif // ASYNCIO_H 83 84