blob: 95de317e6f7961ce8ff9f87c197347b5358f6308 [file] [log] [blame]
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +00001/*
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
Steven Moreland3544a932017-07-19 10:26:05 -070019#include <nativehelper/AsynchronousCloseMonitor.h>
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +000020#include "cutils/log.h"
21
22#include <errno.h>
23#include <signal.h>
24#include <string.h>
25
Colin Crossca2077d2016-07-27 12:30:45 -070026#include <mutex>
27
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +000028/**
29 * We use an intrusive doubly-linked list to keep track of blocked threads.
30 * This gives us O(1) insertion and removal, and means we don't need to do any allocation.
31 * (The objects themselves are stack-allocated.)
32 * Waking potentially-blocked threads when a file descriptor is closed is O(n) in the total number
33 * of blocked threads (not the number of threads actually blocked on the file descriptor in
34 * question). For now at least, this seems like a good compromise for Android.
35 */
Colin Crossca2077d2016-07-27 12:30:45 -070036static std::mutex blockedThreadListMutex;
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +000037static AsynchronousCloseMonitor* blockedThreadList = NULL;
38
39/**
40 * The specific signal chosen here is arbitrary, but bionic needs to know so that SIGRTMIN
41 * starts at a higher value.
42 */
43static const int BLOCKED_THREAD_SIGNAL = __SIGRTMIN + 2;
44
45static void blockedThreadSignalHandler(int /*signal*/) {
46 // Do nothing. We only sent this signal for its side-effect of interrupting syscalls.
47}
48
49void AsynchronousCloseMonitor::init() {
50 // Ensure that the signal we send interrupts system calls but doesn't kill threads.
51 // Using sigaction(2) lets us ensure that the SA_RESTART flag is not set.
52 // (The whole reason we're sending this signal is to unblock system calls!)
53 struct sigaction sa;
54 memset(&sa, 0, sizeof(sa));
55 sa.sa_handler = blockedThreadSignalHandler;
56 sa.sa_flags = 0;
57 int rc = sigaction(BLOCKED_THREAD_SIGNAL, &sa, NULL);
58 if (rc == -1) {
59 ALOGE("setting blocked thread signal handler failed: %s", strerror(errno));
60 }
61}
62
63void AsynchronousCloseMonitor::signalBlockedThreads(int fd) {
Colin Crossca2077d2016-07-27 12:30:45 -070064 std::lock_guard<std::mutex> lock(blockedThreadListMutex);
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +000065 for (AsynchronousCloseMonitor* it = blockedThreadList; it != NULL; it = it->mNext) {
66 if (it->mFd == fd) {
67 it->mSignaled = true;
68 pthread_kill(it->mThread, BLOCKED_THREAD_SIGNAL);
69 // Keep going, because there may be more than one thread...
70 }
71 }
72}
73
74bool AsynchronousCloseMonitor::wasSignaled() const {
75 return mSignaled;
76}
77
78AsynchronousCloseMonitor::AsynchronousCloseMonitor(int fd) {
Colin Crossca2077d2016-07-27 12:30:45 -070079 std::lock_guard<std::mutex> lock(blockedThreadListMutex);
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +000080 // Who are we, and what are we waiting for?
81 mThread = pthread_self();
82 mFd = fd;
83 mSignaled = false;
84 // Insert ourselves at the head of the intrusive doubly-linked list...
85 mPrev = NULL;
86 mNext = blockedThreadList;
87 if (mNext != NULL) {
88 mNext->mPrev = this;
89 }
90 blockedThreadList = this;
91}
92
93AsynchronousCloseMonitor::~AsynchronousCloseMonitor() {
Colin Crossca2077d2016-07-27 12:30:45 -070094 std::lock_guard<std::mutex> lock(blockedThreadListMutex);
Przemyslaw Szczepaniake03afaa2015-12-16 14:15:51 +000095 // Unlink ourselves from the intrusive doubly-linked list...
96 if (mNext != NULL) {
97 mNext->mPrev = mPrev;
98 }
99 if (mPrev == NULL) {
100 blockedThreadList = mNext;
101 } else {
102 mPrev->mNext = mNext;
103 }
104}