• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2016 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 "async_fd_watcher.h"
18 
19 #include <algorithm>
20 #include <atomic>
21 #include <condition_variable>
22 #include <log/log.h>
23 #include <map>
24 #include <mutex>
25 #include <thread>
26 #include <vector>
27 #include "fcntl.h"
28 #include "sys/select.h"
29 #include "unistd.h"
30 
31 static const int INVALID_FD = -1;
32 static const int BT_RT_PRIORITY = 1;
33 
34 namespace android {
35 namespace hardware {
36 namespace bluetooth {
37 namespace async {
38 
WatchFdForNonBlockingReads(int file_descriptor,const ReadCallback & on_read_fd_ready_callback)39 int AsyncFdWatcher::WatchFdForNonBlockingReads(
40     int file_descriptor, const ReadCallback& on_read_fd_ready_callback) {
41   // Add file descriptor and callback
42   {
43     std::unique_lock<std::mutex> guard(internal_mutex_);
44     watched_fds_[file_descriptor] = on_read_fd_ready_callback;
45   }
46 
47   // Start the thread if not started yet
48   return tryStartThread();
49 }
50 
ConfigureTimeout(const std::chrono::milliseconds timeout,const TimeoutCallback & on_timeout_callback)51 int AsyncFdWatcher::ConfigureTimeout(
52     const std::chrono::milliseconds timeout,
53     const TimeoutCallback& on_timeout_callback) {
54   // Add timeout and callback
55   {
56     std::unique_lock<std::mutex> guard(timeout_mutex_);
57     timeout_cb_ = on_timeout_callback;
58     timeout_ms_ = timeout;
59   }
60 
61   notifyThread();
62   return 0;
63 }
64 
StopWatchingFileDescriptors()65 void AsyncFdWatcher::StopWatchingFileDescriptors() { stopThread(); }
66 
~AsyncFdWatcher()67 AsyncFdWatcher::~AsyncFdWatcher() {}
68 
69 // Make sure to call this with at least one file descriptor ready to be
70 // watched upon or the thread routine will return immediately
tryStartThread()71 int AsyncFdWatcher::tryStartThread() {
72   if (std::atomic_exchange(&running_, true)) return 0;
73 
74   // Set up the communication channel
75   int pipe_fds[2];
76   if (pipe2(pipe_fds, O_NONBLOCK)) return -1;
77 
78   notification_listen_fd_ = pipe_fds[0];
79   notification_write_fd_ = pipe_fds[1];
80 
81   thread_ = std::thread([this]() { ThreadRoutine(); });
82   if (!thread_.joinable()) return -1;
83 
84   return 0;
85 }
86 
stopThread()87 int AsyncFdWatcher::stopThread() {
88   if (!std::atomic_exchange(&running_, false)) return 0;
89 
90   notifyThread();
91   if (std::this_thread::get_id() != thread_.get_id()) {
92     thread_.join();
93   }
94 
95   {
96     std::unique_lock<std::mutex> guard(internal_mutex_);
97     watched_fds_.clear();
98   }
99 
100   {
101     std::unique_lock<std::mutex> guard(timeout_mutex_);
102     timeout_cb_ = nullptr;
103   }
104 
105   close(notification_listen_fd_);
106   close(notification_write_fd_);
107 
108   return 0;
109 }
110 
notifyThread()111 int AsyncFdWatcher::notifyThread() {
112   uint8_t buffer[] = {0};
113   if (TEMP_FAILURE_RETRY(write(notification_write_fd_, &buffer, 1)) < 0) {
114     return -1;
115   }
116   return 0;
117 }
118 
ThreadRoutine()119 void AsyncFdWatcher::ThreadRoutine() {
120 
121   // Make watching thread RT.
122   struct sched_param rt_params;
123   rt_params.sched_priority = BT_RT_PRIORITY;
124   if (sched_setscheduler(gettid(), SCHED_FIFO, &rt_params)) {
125     ALOGE("%s unable to set SCHED_FIFO for pid %d, tid %d, error %s", __func__,
126           getpid(), gettid(), strerror(errno));
127   }
128 
129   while (running_) {
130     fd_set read_fds;
131     FD_ZERO(&read_fds);
132     FD_SET(notification_listen_fd_, &read_fds);
133     int max_read_fd = INVALID_FD;
134     for (auto& it : watched_fds_) {
135       FD_SET(it.first, &read_fds);
136       max_read_fd = std::max(max_read_fd, it.first);
137     }
138 
139     struct timeval timeout;
140     struct timeval* timeout_ptr = NULL;
141     if (timeout_ms_ > std::chrono::milliseconds(0)) {
142       timeout.tv_sec = timeout_ms_.count() / 1000;
143       timeout.tv_usec = (timeout_ms_.count() % 1000) * 1000;
144       timeout_ptr = &timeout;
145     }
146 
147     // Wait until there is data available to read on some FD.
148     int nfds = std::max(notification_listen_fd_, max_read_fd);
149     int retval = select(nfds + 1, &read_fds, NULL, NULL, timeout_ptr);
150 
151     // There was some error.
152     if (retval < 0) continue;
153 
154     // Timeout.
155     if (retval == 0) {
156       // Allow the timeout callback to modify the timeout.
157       TimeoutCallback saved_cb;
158       {
159         std::unique_lock<std::mutex> guard(timeout_mutex_);
160         if (timeout_ms_ > std::chrono::milliseconds(0))
161           saved_cb = timeout_cb_;
162       }
163       if (saved_cb != nullptr)
164         saved_cb();
165       continue;
166     }
167 
168     // Read data from the notification FD.
169     if (FD_ISSET(notification_listen_fd_, &read_fds)) {
170       char buffer[] = {0};
171       TEMP_FAILURE_RETRY(read(notification_listen_fd_, buffer, 1));
172       continue;
173     }
174 
175     // Invoke the data ready callbacks if appropriate.
176     {
177       // Hold the mutex to make sure that the callbacks are still valid.
178       std::unique_lock<std::mutex> guard(internal_mutex_);
179       for (auto& it : watched_fds_) {
180         if (FD_ISSET(it.first, &read_fds)) {
181         it.second(it.first);
182         }
183       }
184     }
185   }
186 }
187 
188 } // namespace async
189 } // namespace bluetooth
190 } // namespace hardware
191 } // namespace android
192