Merge "init: Introduce Epoll::SetFirstCallback()"
This commit is contained in:
commit
9457a9ab72
3 changed files with 14 additions and 4 deletions
|
|
@ -75,6 +75,10 @@ Result<void> Epoll::UnregisterHandler(int fd) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Epoll::SetFirstCallback(std::function<void()> first_callback) {
|
||||||
|
first_callback_ = std::move(first_callback);
|
||||||
|
}
|
||||||
|
|
||||||
Result<std::vector<std::shared_ptr<Epoll::Handler>>> Epoll::Wait(
|
Result<std::vector<std::shared_ptr<Epoll::Handler>>> Epoll::Wait(
|
||||||
std::optional<std::chrono::milliseconds> timeout) {
|
std::optional<std::chrono::milliseconds> timeout) {
|
||||||
int timeout_ms = -1;
|
int timeout_ms = -1;
|
||||||
|
|
@ -87,6 +91,9 @@ Result<std::vector<std::shared_ptr<Epoll::Handler>>> Epoll::Wait(
|
||||||
if (num_events == -1) {
|
if (num_events == -1) {
|
||||||
return ErrnoError() << "epoll_wait failed";
|
return ErrnoError() << "epoll_wait failed";
|
||||||
}
|
}
|
||||||
|
if (num_events > 0 && first_callback_) {
|
||||||
|
first_callback_();
|
||||||
|
}
|
||||||
std::vector<std::shared_ptr<Handler>> pending_functions;
|
std::vector<std::shared_ptr<Handler>> pending_functions;
|
||||||
for (int i = 0; i < num_events; ++i) {
|
for (int i = 0; i < num_events; ++i) {
|
||||||
auto& info = *reinterpret_cast<Info*>(ev[i].data.ptr);
|
auto& info = *reinterpret_cast<Info*>(ev[i].data.ptr);
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ class Epoll {
|
||||||
Result<void> Open();
|
Result<void> Open();
|
||||||
Result<void> RegisterHandler(int fd, Handler handler, uint32_t events = EPOLLIN);
|
Result<void> RegisterHandler(int fd, Handler handler, uint32_t events = EPOLLIN);
|
||||||
Result<void> UnregisterHandler(int fd);
|
Result<void> UnregisterHandler(int fd);
|
||||||
|
void SetFirstCallback(std::function<void()> first_callback);
|
||||||
Result<std::vector<std::shared_ptr<Handler>>> Wait(
|
Result<std::vector<std::shared_ptr<Handler>>> Wait(
|
||||||
std::optional<std::chrono::milliseconds> timeout);
|
std::optional<std::chrono::milliseconds> timeout);
|
||||||
|
|
||||||
|
|
@ -53,6 +54,7 @@ class Epoll {
|
||||||
|
|
||||||
android::base::unique_fd epoll_fd_;
|
android::base::unique_fd epoll_fd_;
|
||||||
std::map<int, Info> epoll_handlers_;
|
std::map<int, Info> epoll_handlers_;
|
||||||
|
std::function<void()> first_callback_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace init
|
} // namespace init
|
||||||
|
|
|
||||||
|
|
@ -1061,6 +1061,11 @@ int SecondStageMain(int argc, char** argv) {
|
||||||
PLOG(FATAL) << result.error();
|
PLOG(FATAL) << result.error();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We always reap children before responding to the other pending functions. This is to
|
||||||
|
// prevent a race where other daemons see that a service has exited and ask init to
|
||||||
|
// start it again via ctl.start before init has reaped it.
|
||||||
|
epoll.SetFirstCallback(ReapAnyOutstandingChildren);
|
||||||
|
|
||||||
InstallSignalFdHandler(&epoll);
|
InstallSignalFdHandler(&epoll);
|
||||||
InstallInitNotifier(&epoll);
|
InstallInitNotifier(&epoll);
|
||||||
StartPropertyService(&property_fd);
|
StartPropertyService(&property_fd);
|
||||||
|
|
@ -1176,10 +1181,6 @@ int SecondStageMain(int argc, char** argv) {
|
||||||
if (!pending_functions.ok()) {
|
if (!pending_functions.ok()) {
|
||||||
LOG(ERROR) << pending_functions.error();
|
LOG(ERROR) << pending_functions.error();
|
||||||
} else if (!pending_functions->empty()) {
|
} else if (!pending_functions->empty()) {
|
||||||
// We always reap children before responding to the other pending functions. This is to
|
|
||||||
// prevent a race where other daemons see that a service has exited and ask init to
|
|
||||||
// start it again via ctl.start before init has reaped it.
|
|
||||||
ReapAnyOutstandingChildren();
|
|
||||||
for (const auto& function : *pending_functions) {
|
for (const auto& function : *pending_functions) {
|
||||||
(*function)();
|
(*function)();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue