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