1 /*
2 * Copyright (C) 2010 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 #define LOG_TAG "AsynchronousCloseMonitor"
18
19 #include <log/log.h>
20
21 #include <errno.h>
22 #include <signal.h>
23 #include <string.h>
24
25 #include <mutex>
26
27 #include "AsynchronousCloseMonitor.h"
28
29 namespace {
30
31 class AsynchronousCloseMonitorImpl {
32 public:
33 explicit AsynchronousCloseMonitorImpl(int fd);
34 ~AsynchronousCloseMonitorImpl();
35 bool wasSignaled() const;
36
37 static void init();
38
39 static void signalBlockedThreads(int fd);
40
41 private:
42 AsynchronousCloseMonitorImpl(const AsynchronousCloseMonitorImpl&) = delete;
43 AsynchronousCloseMonitorImpl& operator=(const AsynchronousCloseMonitorImpl&) = delete;
44
45 AsynchronousCloseMonitorImpl* mPrev;
46 AsynchronousCloseMonitorImpl* mNext;
47 pthread_t mThread;
48 int mFd;
49 bool mSignaled;
50 };
51
52 /**
53 * We use an intrusive doubly-linked list to keep track of blocked threads.
54 * This gives us O(1) insertion and removal, and means we don't need to do any allocation.
55 * (The objects themselves are stack-allocated.)
56 * Waking potentially-blocked threads when a file descriptor is closed is O(n) in the total number
57 * of blocked threads (not the number of threads actually blocked on the file descriptor in
58 * question). For now at least, this seems like a good compromise for Android.
59 */
60 static std::mutex blockedThreadListMutex;
61 static AsynchronousCloseMonitorImpl* blockedThreadList = NULL;
62
63 /**
64 * The specific signal chosen here is arbitrary, but bionic needs to know so that SIGRTMIN
65 * starts at a higher value.
66 */
67 #if defined(__Fuchsia__)
68 static const int BLOCKED_THREAD_SIGNAL = SIGRTMIN + 2;
69 #else
70 static const int BLOCKED_THREAD_SIGNAL = __SIGRTMIN + 2;
71 #endif
72
blockedThreadSignalHandler(int)73 static void blockedThreadSignalHandler(int /*signal*/) {
74 // Do nothing. We only sent this signal for its side-effect of interrupting syscalls.
75 }
76
init()77 void AsynchronousCloseMonitorImpl::init() {
78 // Ensure that the signal we send interrupts system calls but doesn't kill threads.
79 // Using sigaction(2) lets us ensure that the SA_RESTART flag is not set.
80 // (The whole reason we're sending this signal is to unblock system calls!)
81 struct sigaction sa;
82 memset(&sa, 0, sizeof(sa));
83 sa.sa_handler = blockedThreadSignalHandler;
84 sa.sa_flags = 0;
85 int rc = sigaction(BLOCKED_THREAD_SIGNAL, &sa, NULL);
86 if (rc == -1) {
87 ALOGE("setting blocked thread signal handler failed: %s", strerror(errno));
88 }
89 }
90
signalBlockedThreads(int fd)91 void AsynchronousCloseMonitorImpl::signalBlockedThreads(int fd) {
92 std::lock_guard<std::mutex> lock(blockedThreadListMutex);
93 for (AsynchronousCloseMonitorImpl* it = blockedThreadList; it != NULL; it = it->mNext) {
94 if (it->mFd == fd) {
95 it->mSignaled = true;
96 pthread_kill(it->mThread, BLOCKED_THREAD_SIGNAL);
97 // Keep going, because there may be more than one thread...
98 }
99 }
100 }
101
wasSignaled() const102 bool AsynchronousCloseMonitorImpl::wasSignaled() const {
103 return mSignaled;
104 }
105
AsynchronousCloseMonitorImpl(int fd)106 AsynchronousCloseMonitorImpl::AsynchronousCloseMonitorImpl(int fd) {
107 std::lock_guard<std::mutex> lock(blockedThreadListMutex);
108 // Who are we, and what are we waiting for?
109 mThread = pthread_self();
110 mFd = fd;
111 mSignaled = false;
112 // Insert ourselves at the head of the intrusive doubly-linked list...
113 mPrev = NULL;
114 mNext = blockedThreadList;
115 if (mNext != NULL) {
116 mNext->mPrev = this;
117 }
118 blockedThreadList = this;
119 }
120
~AsynchronousCloseMonitorImpl()121 AsynchronousCloseMonitorImpl::~AsynchronousCloseMonitorImpl() {
122 std::lock_guard<std::mutex> lock(blockedThreadListMutex);
123 // Unlink ourselves from the intrusive doubly-linked list...
124 if (mNext != NULL) {
125 mNext->mPrev = mPrev;
126 }
127 if (mPrev == NULL) {
128 blockedThreadList = mNext;
129 } else {
130 mPrev->mNext = mNext;
131 }
132 }
133
134 } // namespace
135
136 //
137 // C ABI and API boundary
138 //
139
140 extern "C" {
async_close_monitor_static_init()141 void async_close_monitor_static_init() {
142 AsynchronousCloseMonitorImpl::init();
143 }
144
async_close_monitor_signal_blocked_threads(int fd)145 void async_close_monitor_signal_blocked_threads(int fd) {
146 AsynchronousCloseMonitorImpl::signalBlockedThreads(fd);
147 }
148
async_close_monitor_create(int fd)149 void* async_close_monitor_create(int fd) {
150 return new AsynchronousCloseMonitorImpl(fd);
151 }
152
async_close_monitor_destroy(void * instance)153 void async_close_monitor_destroy(void* instance) {
154 auto monitor = reinterpret_cast<AsynchronousCloseMonitorImpl*>(instance);
155 delete monitor;
156 }
157
async_close_monitor_was_signalled(const void * instance)158 int async_close_monitor_was_signalled(const void* instance) {
159 auto monitor = reinterpret_cast<const AsynchronousCloseMonitorImpl*>(instance);
160 return monitor->wasSignaled() ? 1 : 0;
161 }
162 }
163