1 /*
2 * Copyright 2019 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 #include "os/handler.h"
18
19 #include <sys/eventfd.h>
20 #include <unistd.h>
21
22 #include <cstring>
23
24 #include "common/bind.h"
25 #include "common/callback.h"
26 #include "os/log.h"
27 #include "os/reactor.h"
28 #include "os/utils.h"
29
30 #ifndef EFD_SEMAPHORE
31 #define EFD_SEMAPHORE 1
32 #endif
33
34 namespace bluetooth {
35 namespace os {
36 using common::OnceClosure;
37
Handler(Thread * thread)38 Handler::Handler(Thread* thread)
39 : tasks_(new std::queue<OnceClosure>()), thread_(thread), fd_(eventfd(0, EFD_SEMAPHORE | EFD_NONBLOCK)) {
40 ASSERT(fd_ != -1);
41 reactable_ = thread_->GetReactor()->Register(
42 fd_, common::Bind(&Handler::handle_next_event, common::Unretained(this)), common::Closure());
43 }
44
~Handler()45 Handler::~Handler() {
46 {
47 std::lock_guard<std::mutex> lock(mutex_);
48 ASSERT_LOG(was_cleared(), "Handlers must be cleared before they are destroyed");
49 }
50
51 int close_status;
52 RUN_NO_INTR(close_status = close(fd_));
53 ASSERT(close_status != -1);
54 }
55
Post(OnceClosure closure)56 void Handler::Post(OnceClosure closure) {
57 {
58 std::lock_guard<std::mutex> lock(mutex_);
59 if (was_cleared()) {
60 LOG_WARN("Posting to a handler which has been cleared");
61 return;
62 }
63 tasks_->emplace(std::move(closure));
64 }
65 uint64_t val = 1;
66 auto write_result = eventfd_write(fd_, val);
67 ASSERT(write_result != -1);
68 }
69
Clear()70 void Handler::Clear() {
71 std::queue<OnceClosure>* tmp = nullptr;
72 {
73 std::lock_guard<std::mutex> lock(mutex_);
74 ASSERT_LOG(!was_cleared(), "Handlers must only be cleared once");
75 std::swap(tasks_, tmp);
76 }
77 delete tmp;
78
79 uint64_t val;
80 while (eventfd_read(fd_, &val) == 0) {
81 }
82
83 thread_->GetReactor()->Unregister(reactable_);
84 reactable_ = nullptr;
85 }
86
WaitUntilStopped(std::chrono::milliseconds timeout)87 void Handler::WaitUntilStopped(std::chrono::milliseconds timeout) {
88 ASSERT(reactable_ == nullptr);
89 ASSERT(thread_->GetReactor()->WaitForUnregisteredReactable(timeout));
90 }
91
handle_next_event()92 void Handler::handle_next_event() {
93 common::OnceClosure closure;
94 {
95 std::lock_guard<std::mutex> lock(mutex_);
96 uint64_t val = 0;
97 auto read_result = eventfd_read(fd_, &val);
98
99 if (was_cleared()) {
100 return;
101 }
102 ASSERT_LOG(read_result != -1, "eventfd read error %d %s", errno, strerror(errno));
103
104 closure = std::move(tasks_->front());
105 tasks_->pop();
106 }
107 std::move(closure).Run();
108 }
109
110 } // namespace os
111 } // namespace bluetooth
112