blob: f7515a2ed9487e41e29f81c44953711374b47866 [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#ifndef _ASYNCIO_H
18#define _ASYNCIO_H
19
20#include <fcntl.h>
21#include <linux/aio_abi.h>
22#include <memory>
23#include <signal.h>
24#include <sys/cdefs.h>
25#include <sys/types.h>
26#include <time.h>
27#include <thread>
28#include <unistd.h>
29
30/**
31 * Provides a subset of POSIX aio operations, as well
32 * as similar operations with splice and threadpools.
33 */
34
35struct aiocb {
36 int aio_fildes; // Assumed to be the source for splices
37 void *aio_buf; // Unused for splices
38
39 // Used for threadpool operations only, freed automatically
40 std::unique_ptr<char[]> aio_pool_buf;
41
42 off_t aio_offset;
43 size_t aio_nbytes;
44
45 int aio_sink; // Unused for non splice r/w
46
47 // Used internally
48 std::thread thread;
49 ssize_t ret;
50 int error;
51};
52
53// Submit a request for IO to be completed
54int aio_read(struct aiocb *);
55int aio_write(struct aiocb *);
56int aio_splice_read(struct aiocb *);
57int aio_splice_write(struct aiocb *);
58
59// Suspend current thread until given IO is complete, at which point
60// its return value and any errors can be accessed
61int aio_suspend(struct aiocb *[], int, const struct timespec *);
62int aio_error(const struct aiocb *);
63ssize_t aio_return(struct aiocb *);
64int aio_cancel(int, struct aiocb *);
65
66// Initialize a threadpool to perform IO. Only one pool can be
67// running at a time.
68void aio_pool_write_init();
69void aio_pool_splice_init();
70// Suspend current thread until all queued work is complete, then ends the threadpool
71void aio_pool_end();
72// Submit IO work for the threadpool to complete. Memory associated with the work is
73// freed automatically when the work is complete.
74int aio_pool_write(struct aiocb *);
75
76#endif // ASYNCIO_H
77