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