blob: 8205e3bb6343bb88fb1ef27f081073007a89c203 [file] [log] [blame]
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -05001/*
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 *
sekaiacg8cb6d9a2021-12-23 18:57:05 +08008 * http://www.apache.org/licenses/LICENSE-2.0
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -05009 *
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 bigbiffaf32bb92018-12-18 18:39:53 -050018#include <memory>
sekaiacg8cb6d9a2021-12-23 18:57:05 +080019#include <pthread.h>
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050020#include <queue>
sekaiacg8cb6d9a2021-12-23 18:57:05 +080021#include <thread>
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050022#include <unistd.h>
23
24#include "PosixAsyncIO.h"
25
26namespace {
27
sekaiacg8cb6d9a2021-12-23 18:57:05 +080028std::thread gWorkerThread;
29std::deque<struct aiocb*> gWorkQueue;
30bool gSuspended = true;
31int gAiocbRefcount = 0;
32std::mutex gLock;
33std::condition_variable gWait;
34
35void 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 bigbiffaf32bb92018-12-18 18:39:53 -050066}
67
sekaiacg8cb6d9a2021-12-23 18:57:05 +080068int 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 bigbiffaf32bb92018-12-18 18:39:53 -050077}
78
79} // end anonymous namespace
80
sekaiacg8cb6d9a2021-12-23 18:57:05 +080081aiocb::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 bigbiffaf32bb92018-12-18 18:39:53 -050096aiocb::~aiocb() {
sekaiacg8cb6d9a2021-12-23 18:57:05 +080097 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 bigbiffaf32bb92018-12-18 18:39:53 -0500111}
112
113int aio_read(struct aiocb *aiocbp) {
sekaiacg8cb6d9a2021-12-23 18:57:05 +0800114 aiocbp->read = true;
115 return aio_add(aiocbp);
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -0500116}
117
118int aio_write(struct aiocb *aiocbp) {
sekaiacg8cb6d9a2021-12-23 18:57:05 +0800119 aiocbp->read = false;
120 return aio_add(aiocbp);
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -0500121}
122
123int aio_error(const struct aiocb *aiocbp) {
sekaiacg8cb6d9a2021-12-23 18:57:05 +0800124 return aiocbp->error;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -0500125}
126
127ssize_t aio_return(struct aiocb *aiocbp) {
sekaiacg8cb6d9a2021-12-23 18:57:05 +0800128 return aiocbp->ret;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -0500129}
130
131int aio_suspend(struct aiocb *aiocbp[], int n,
sekaiacg8cb6d9a2021-12-23 18:57:05 +0800132 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 bigbiffaf32bb92018-12-18 18:39:53 -0500140}
141
sekaiacg8cb6d9a2021-12-23 18:57:05 +0800142void 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 bigbiffaf32bb92018-12-18 18:39:53 -0500146}