blob: 2bcae4c2eb490b456368d87241b1ef0038f8faaf [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 *
sekaiacgcf8c1282021-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#ifndef _POSIXASYNCIO_H
18#define _POSIXASYNCIO_H
19
sekaiacgcf8c1282021-12-23 18:57:05 +080020#include <condition_variable>
21#include <mutex>
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050022#include <sys/cdefs.h>
23#include <sys/types.h>
24#include <time.h>
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050025#include <unistd.h>
26
27/**
28 * Provides a subset of POSIX aio operations.
29 */
30
31struct aiocb {
sekaiacgcf8c1282021-12-23 18:57:05 +080032 int aio_fildes;
33 void *aio_buf;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050034
sekaiacgcf8c1282021-12-23 18:57:05 +080035 off64_t aio_offset;
36 size_t aio_nbytes;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050037
sekaiacgcf8c1282021-12-23 18:57:05 +080038 // Used internally
39 bool read;
40 bool queued;
41 ssize_t ret;
42 int error;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050043
sekaiacgcf8c1282021-12-23 18:57:05 +080044 std::mutex lock;
45 std::condition_variable cv;
46
47 aiocb();
48 ~aiocb();
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050049};
50
51// Submit a request for IO to be completed
52int aio_read(struct aiocb *);
53int aio_write(struct aiocb *);
54
55// Suspend current thread until given IO is complete, at which point
56// its return value and any errors can be accessed
57// All submitted requests must have a corresponding suspend.
58// aiocb->aio_buf must refer to valid memory until after the suspend call
59int aio_suspend(struct aiocb *[], int, const struct timespec *);
60int aio_error(const struct aiocb *);
61ssize_t aio_return(struct aiocb *);
62
63// Helper method for setting aiocb members
sekaiacgcf8c1282021-12-23 18:57:05 +080064void aio_prepare(struct aiocb *, void*, size_t, off64_t);
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050065
66#endif // POSIXASYNCIO_H
67