blob: e77ad38b79862fab7df2cbca531c0577f0199e81 [file] [log] [blame]
Jerry Zhang487be612016-10-24 12:10:41 -07001/*
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
25namespace {
26
27void read_func(struct aiocb *aiocbp) {
28 aiocbp->ret = TEMP_FAILURE_RETRY(pread(aiocbp->aio_fildes,
29 aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
30 if (aiocbp->ret == -1) aiocbp->error = errno;
31}
32
33void write_func(struct aiocb *aiocbp) {
34 aiocbp->ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
35 aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
36 if (aiocbp->ret == -1) aiocbp->error = errno;
37}
38
39void splice_read_func(struct aiocb *aiocbp) {
Jerry Zhang8e889ef2017-01-03 14:41:56 -080040 loff_t long_offset = aiocbp->aio_offset;
Jerry Zhang487be612016-10-24 12:10:41 -070041 aiocbp->ret = TEMP_FAILURE_RETRY(splice(aiocbp->aio_fildes,
Jerry Zhang8e889ef2017-01-03 14:41:56 -080042 &long_offset, aiocbp->aio_sink,
Jerry Zhang487be612016-10-24 12:10:41 -070043 NULL, aiocbp->aio_nbytes, 0));
44 if (aiocbp->ret == -1) aiocbp->error = errno;
45}
46
47void splice_write_func(struct aiocb *aiocbp) {
Jerry Zhang8e889ef2017-01-03 14:41:56 -080048 loff_t long_offset = aiocbp->aio_offset;
Jerry Zhang487be612016-10-24 12:10:41 -070049 aiocbp->ret = TEMP_FAILURE_RETRY(splice(aiocbp->aio_fildes, NULL,
Jerry Zhang8e889ef2017-01-03 14:41:56 -080050 aiocbp->aio_sink, &long_offset,
Jerry Zhang487be612016-10-24 12:10:41 -070051 aiocbp->aio_nbytes, 0));
52 if (aiocbp->ret == -1) aiocbp->error = errno;
53}
54
55std::queue<std::unique_ptr<struct aiocb>> queue;
56std::mutex queue_lock;
57std::condition_variable queue_cond;
58std::condition_variable write_cond;
59int done = 1;
60void splice_write_pool_func(int) {
61 while(1) {
62 std::unique_lock<std::mutex> lk(queue_lock);
63 queue_cond.wait(lk, []{return !queue.empty() || done;});
64 if (queue.empty() && done) {
65 return;
66 }
67 std::unique_ptr<struct aiocb> aiocbp = std::move(queue.front());
68 queue.pop();
69 lk.unlock();
70 write_cond.notify_one();
71 splice_write_func(aiocbp.get());
72 close(aiocbp->aio_fildes);
73 }
74}
75
76void write_pool_func(int) {
77 while(1) {
78 std::unique_lock<std::mutex> lk(queue_lock);
79 queue_cond.wait(lk, []{return !queue.empty() || done;});
80 if (queue.empty() && done) {
81 return;
82 }
83 std::unique_ptr<struct aiocb> aiocbp = std::move(queue.front());
84 queue.pop();
85 lk.unlock();
86 write_cond.notify_one();
87 aiocbp->ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
88 aiocbp->aio_pool_buf.get(), aiocbp->aio_nbytes, aiocbp->aio_offset));
89 if (aiocbp->ret == -1) aiocbp->error = errno;
90 }
91}
92
93constexpr int NUM_THREADS = 1;
94constexpr int MAX_QUEUE_SIZE = 10;
95std::thread pool[NUM_THREADS];
96
97} // end anonymous namespace
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