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 _POSIXASYNCIO_H 18 #define _POSIXASYNCIO_H 19 20 #include <condition_variable> 21 #include <mutex> 22 #include <sys/cdefs.h> 23 #include <sys/types.h> 24 #include <time.h> 25 #include <unistd.h> 26 27 /** 28 * Provides a subset of POSIX aio operations. 29 */ 30 31 struct aiocb { 32 int aio_fildes; 33 void *aio_buf; 34 35 off64_t aio_offset; 36 size_t aio_nbytes; 37 38 // Used internally 39 bool read; 40 bool queued; 41 ssize_t ret; 42 int error; 43 44 std::mutex lock; 45 std::condition_variable cv; 46 47 aiocb(); 48 ~aiocb(); 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 64 void aio_prepare(struct aiocb *, void*, size_t, off64_t); 65 66 #endif // POSIXASYNCIO_H 67 68