PosixAsyncIO.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef _POSIXASYNCIO_H
  17. #define _POSIXASYNCIO_H
  18. #include <condition_variable>
  19. #include <mutex>
  20. #include <sys/cdefs.h>
  21. #include <sys/types.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. /**
  25. * Provides a subset of POSIX aio operations.
  26. */
  27. struct aiocb {
  28. int aio_fildes;
  29. void *aio_buf;
  30. off_t aio_offset;
  31. size_t aio_nbytes;
  32. // Used internally
  33. bool read;
  34. bool queued;
  35. ssize_t ret;
  36. int error;
  37. std::mutex lock;
  38. std::condition_variable cv;
  39. aiocb();
  40. ~aiocb();
  41. };
  42. // Submit a request for IO to be completed
  43. int aio_read(struct aiocb *);
  44. int aio_write(struct aiocb *);
  45. // Suspend current thread until given IO is complete, at which point
  46. // its return value and any errors can be accessed
  47. // All submitted requests must have a corresponding suspend.
  48. // aiocb->aio_buf must refer to valid memory until after the suspend call
  49. int aio_suspend(struct aiocb *[], int, const struct timespec *);
  50. int aio_error(const struct aiocb *);
  51. ssize_t aio_return(struct aiocb *);
  52. // Helper method for setting aiocb members
  53. void aio_prepare(struct aiocb *, void*, size_t, off_t);
  54. #endif // POSIXASYNCIO_H