2015-10-25 01:20:18 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-08-30 23:12:56 +02:00
|
|
|
#pragma once
|
2015-10-25 01:20:18 +02:00
|
|
|
|
2019-03-13 15:25:52 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
2015-10-25 01:20:18 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
2021-05-14 05:18:14 +02:00
|
|
|
#include <memory>
|
2015-10-25 01:20:18 +02:00
|
|
|
#include <optional>
|
2022-10-14 18:13:19 +02:00
|
|
|
#include <unordered_set>
|
2019-08-30 23:12:56 +02:00
|
|
|
#include <vector>
|
2015-10-25 01:20:18 +02:00
|
|
|
|
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
|
|
|
|
#include "result.h"
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
|
|
|
class Epoll {
|
|
|
|
public:
|
|
|
|
Epoll();
|
|
|
|
|
2021-05-14 05:18:14 +02:00
|
|
|
typedef std::function<void()> Handler;
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> Open();
|
2021-05-14 05:18:14 +02:00
|
|
|
Result<void> RegisterHandler(int fd, Handler handler, uint32_t events = EPOLLIN);
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> UnregisterHandler(int fd);
|
2022-10-14 18:35:35 +02:00
|
|
|
void SetFirstCallback(std::function<void()> first_callback);
|
2022-10-14 18:13:19 +02:00
|
|
|
Result<int> Wait(std::optional<std::chrono::milliseconds> timeout);
|
2015-10-25 01:20:18 +02:00
|
|
|
|
|
|
|
private:
|
2022-03-08 04:10:57 +01:00
|
|
|
struct Info {
|
|
|
|
std::shared_ptr<Handler> handler;
|
|
|
|
uint32_t events;
|
|
|
|
};
|
|
|
|
|
2015-10-25 01:20:18 +02:00
|
|
|
android::base::unique_fd epoll_fd_;
|
2022-03-08 04:10:57 +01:00
|
|
|
std::map<int, Info> epoll_handlers_;
|
2022-10-14 18:35:35 +02:00
|
|
|
std::function<void()> first_callback_;
|
2022-10-14 18:13:19 +02:00
|
|
|
std::unordered_set<int> to_remove_;
|
2015-10-25 01:20:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|