blob: eb97a98a2d2e93e75eefaa7cb875171a5a4855bf [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 *
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#include <android-base/logging.h>
18#include <condition_variable>
19#include <memory>
20#include <mutex>
21#include <queue>
22
23#include "AsyncIO.h"
24
25void read_func(struct aiocb *aiocbp) {
26 aiocbp->ret = TEMP_FAILURE_RETRY(pread(aiocbp->aio_fildes,
27 aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
28 if (aiocbp->ret == -1) aiocbp->error = errno;
29}
30
31void write_func(struct aiocb *aiocbp) {
32 aiocbp->ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
33 aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
34 if (aiocbp->ret == -1) aiocbp->error = errno;
35}
36
37void splice_read_func(struct aiocb *aiocbp) {
38 loff_t long_offset = aiocbp->aio_offset;
39 aiocbp->ret = TEMP_FAILURE_RETRY(splice(aiocbp->aio_fildes,
40 &long_offset, aiocbp->aio_sink,
41 NULL, aiocbp->aio_nbytes, 0));
42 if (aiocbp->ret == -1) aiocbp->error = errno;
43}
44
45void splice_write_func(struct aiocb *aiocbp) {
46 loff_t long_offset = aiocbp->aio_offset;
47 aiocbp->ret = TEMP_FAILURE_RETRY(splice(aiocbp->aio_fildes, NULL,
48 aiocbp->aio_sink, &long_offset,
49 aiocbp->aio_nbytes, 0));
50 if (aiocbp->ret == -1) aiocbp->error = errno;
51}
52
53std::queue<std::unique_ptr<struct aiocb>> queue;
54std::mutex queue_lock;
55std::condition_variable queue_cond;
56std::condition_variable write_cond;
57int done = 1;
58void splice_write_pool_func(int) {
59 while(1) {
60 std::unique_lock<std::mutex> lk(queue_lock);
61 queue_cond.wait(lk, []{return !queue.empty() || done;});
62 if (queue.empty() && done) {
63 return;
64 }
65 std::unique_ptr<struct aiocb> aiocbp = std::move(queue.front());
66 queue.pop();
67 lk.unlock();
68 write_cond.notify_one();
69 splice_write_func(aiocbp.get());
70 close(aiocbp->aio_fildes);
71 }
72}
73
74void write_pool_func(int) {
75 while(1) {
76 std::unique_lock<std::mutex> lk(queue_lock);
77 queue_cond.wait(lk, []{return !queue.empty() || done;});
78 if (queue.empty() && done) {
79 return;
80 }
81 std::unique_ptr<struct aiocb> aiocbp = std::move(queue.front());
82 queue.pop();
83 lk.unlock();
84 write_cond.notify_one();
85 aiocbp->ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
86 aiocbp->aio_pool_buf.get(), aiocbp->aio_nbytes, aiocbp->aio_offset));
87 if (aiocbp->ret == -1) aiocbp->error = errno;
88 }
89}
90
91constexpr int NUM_THREADS = 1;
92constexpr int MAX_QUEUE_SIZE = 10;
93std::thread pool[NUM_THREADS];
94
95aiocb::~aiocb() {
96 CHECK(!thread.joinable());
97}
98
99void aio_pool_init(void(f)(int)) {
100 CHECK(done == 1);
101 done = 0;
102 for (int i = 0; i < NUM_THREADS; i++) {
103 pool[i] = std::thread(f, i);
104 }
105}
106
107void aio_pool_splice_init() {
108 aio_pool_init(splice_write_pool_func);
109}
110
111void aio_pool_write_init() {
112 aio_pool_init(write_pool_func);
113}
114
115void aio_pool_end() {
116 done = 1;
117 for (int i = 0; i < NUM_THREADS; i++) {
118 std::unique_lock<std::mutex> lk(queue_lock);
119 lk.unlock();
120 queue_cond.notify_one();
121 }
122
123 for (int i = 0; i < NUM_THREADS; i++) {
124 pool[i].join();
125 }
126}
127
128// used for both writes and splices depending on which init was used before.
129int aio_pool_write(struct aiocb *aiocbp) {
130 std::unique_lock<std::mutex> lk(queue_lock);
131 write_cond.wait(lk, []{return queue.size() < MAX_QUEUE_SIZE;});
132 queue.push(std::unique_ptr<struct aiocb>(aiocbp));
133 lk.unlock();
134 queue_cond.notify_one();
135 return 0;
136}
137
138int aio_read(struct aiocb *aiocbp) {
139 aiocbp->thread = std::thread(read_func, aiocbp);
140 return 0;
141}
142
143int aio_write(struct aiocb *aiocbp) {
144 aiocbp->thread = std::thread(write_func, aiocbp);
145 return 0;
146}
147
148int aio_splice_read(struct aiocb *aiocbp) {
149 aiocbp->thread = std::thread(splice_read_func, aiocbp);
150 return 0;
151}
152
153int aio_splice_write(struct aiocb *aiocbp) {
154 aiocbp->thread = std::thread(splice_write_func, aiocbp);
155 return 0;
156}
157
158int aio_error(const struct aiocb *aiocbp) {
159 return aiocbp->error;
160}
161
162ssize_t aio_return(struct aiocb *aiocbp) {
163 return aiocbp->ret;
164}
165
166int aio_suspend(struct aiocb *aiocbp[], int n,
167 const struct timespec *) {
168 for (int i = 0; i < n; i++) {
169 aiocbp[i]->thread.join();
170 }
171 return 0;
172}
173
174int aio_cancel(int, struct aiocb *) {
175 // Not implemented
176 return -1;
177}
178