You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.9 KiB
86 lines
2.9 KiB
/*
|
|
* Copyright 2019 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <atomic>
|
|
#include <functional>
|
|
#include <future>
|
|
#include <list>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
#include "common/callback.h"
|
|
#include "os/utils.h"
|
|
|
|
namespace bluetooth {
|
|
namespace os {
|
|
|
|
// A simple implementation of reactor-style looper.
|
|
// When a reactor is running, the main loop is polling and blocked until at least one registered reactable is ready to
|
|
// read or write. It will invoke on_read_ready() or on_write_ready(), which is registered with the reactor. Then, it
|
|
// blocks again until ready event.
|
|
class Reactor {
|
|
public:
|
|
// An object used for Unregister() and ModifyRegistration()
|
|
class Reactable;
|
|
|
|
// Construct a reactor on the current thread
|
|
Reactor();
|
|
|
|
// Destruct this reactor and release its resources
|
|
~Reactor();
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Reactor);
|
|
|
|
// Start the reactor. The current thread will be blocked until Stop() is invoked and handled.
|
|
void Run();
|
|
|
|
// Stop the reactor. Must be invoked from a different thread. Note: all registered reactables will not be unregistered
|
|
// by Stop(). If the reactor is not running, it will be stopped once it's started.
|
|
void Stop();
|
|
|
|
// Register a reactable fd to this reactor. Returns a pointer to a Reactable. Caller must use this object to
|
|
// unregister or modify registration. Ownership of the memory space is NOT transferred to user.
|
|
Reactable* Register(int fd, common::Closure on_read_ready, common::Closure on_write_ready);
|
|
|
|
// Unregister a reactable from this reactor
|
|
void Unregister(Reactable* reactable);
|
|
|
|
// Wait for up to timeout milliseconds, and return true if the reactable finished executing.
|
|
bool WaitForUnregisteredReactable(std::chrono::milliseconds timeout);
|
|
|
|
// Wait for up to timeout milliseconds, and return true if we reached idle.
|
|
bool WaitForIdle(std::chrono::milliseconds timeout);
|
|
|
|
// Modify the registration for a reactable with given reactable
|
|
void ModifyRegistration(Reactable* reactable, common::Closure on_read_ready, common::Closure on_write_ready);
|
|
|
|
private:
|
|
mutable std::mutex mutex_;
|
|
int epoll_fd_;
|
|
int control_fd_;
|
|
std::atomic<bool> is_running_;
|
|
std::list<Reactable*> invalidation_list_;
|
|
std::shared_ptr<std::future<void>> executing_reactable_finished_;
|
|
std::shared_ptr<std::promise<void>> idle_promise_;
|
|
};
|
|
|
|
} // namespace os
|
|
} // namespace bluetooth
|