PosixAsyncIO.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include <android-base/logging.h>
  17. #include <memory>
  18. #include <pthread.h>
  19. #include <queue>
  20. #include <thread>
  21. #include <unistd.h>
  22. #include "PosixAsyncIO.h"
  23. namespace {
  24. std::thread gWorkerThread;
  25. std::deque<struct aiocb*> gWorkQueue;
  26. bool gSuspended = true;
  27. int gAiocbRefcount = 0;
  28. std::mutex gLock;
  29. std::condition_variable gWait;
  30. void work_func(void *) {
  31. pthread_setname_np(pthread_self(), "AsyncIO work");
  32. while (true) {
  33. struct aiocb *aiocbp;
  34. {
  35. std::unique_lock<std::mutex> lk(gLock);
  36. gWait.wait(lk, []{return gWorkQueue.size() > 0 || gSuspended;});
  37. if (gSuspended)
  38. return;
  39. aiocbp = gWorkQueue.back();
  40. gWorkQueue.pop_back();
  41. }
  42. CHECK(aiocbp->queued);
  43. int ret;
  44. if (aiocbp->read) {
  45. ret = TEMP_FAILURE_RETRY(pread(aiocbp->aio_fildes,
  46. aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
  47. } else {
  48. ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
  49. aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
  50. }
  51. {
  52. std::unique_lock<std::mutex> lk(aiocbp->lock);
  53. aiocbp->ret = ret;
  54. if (aiocbp->ret == -1) {
  55. aiocbp->error = errno;
  56. }
  57. aiocbp->queued = false;
  58. }
  59. aiocbp->cv.notify_all();
  60. }
  61. }
  62. int aio_add(struct aiocb *aiocbp) {
  63. CHECK(!aiocbp->queued);
  64. aiocbp->queued = true;
  65. {
  66. std::unique_lock<std::mutex> lk(gLock);
  67. gWorkQueue.push_front(aiocbp);
  68. }
  69. gWait.notify_one();
  70. return 0;
  71. }
  72. } // end anonymous namespace
  73. aiocb::aiocb() {
  74. this->ret = 0;
  75. this->queued = false;
  76. {
  77. std::unique_lock<std::mutex> lk(gLock);
  78. if (gAiocbRefcount == 0) {
  79. CHECK(gWorkQueue.size() == 0);
  80. CHECK(gSuspended);
  81. gSuspended = false;
  82. gWorkerThread = std::thread(work_func, nullptr);
  83. }
  84. gAiocbRefcount++;
  85. }
  86. }
  87. aiocb::~aiocb() {
  88. CHECK(!this->queued);
  89. {
  90. std::unique_lock<std::mutex> lk(gLock);
  91. CHECK(!gSuspended);
  92. if (gAiocbRefcount == 1) {
  93. CHECK(gWorkQueue.size() == 0);
  94. gSuspended = true;
  95. lk.unlock();
  96. gWait.notify_one();
  97. gWorkerThread.join();
  98. lk.lock();
  99. }
  100. gAiocbRefcount--;
  101. }
  102. }
  103. int aio_read(struct aiocb *aiocbp) {
  104. aiocbp->read = true;
  105. return aio_add(aiocbp);
  106. }
  107. int aio_write(struct aiocb *aiocbp) {
  108. aiocbp->read = false;
  109. return aio_add(aiocbp);
  110. }
  111. int aio_error(const struct aiocb *aiocbp) {
  112. return aiocbp->error;
  113. }
  114. ssize_t aio_return(struct aiocb *aiocbp) {
  115. return aiocbp->ret;
  116. }
  117. int aio_suspend(struct aiocb *aiocbp[], int n,
  118. const struct timespec *) {
  119. for (int i = 0; i < n; i++) {
  120. {
  121. std::unique_lock<std::mutex> lk(aiocbp[i]->lock);
  122. aiocbp[i]->cv.wait(lk, [aiocbp, i]{return !aiocbp[i]->queued;});
  123. }
  124. }
  125. return 0;
  126. }
  127. void aio_prepare(struct aiocb *aiocbp, void* buf, size_t count, off_t offset) {
  128. aiocbp->aio_buf = buf;
  129. aiocbp->aio_offset = offset;
  130. aiocbp->aio_nbytes = count;
  131. }