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 "AsynchronousCloseMonitor.h"
20#include "cutils/log.h"
21
22#include <errno.h>
23#include <signal.h>
24#include <string.h>
25
26#include <mutex>
27
28/**
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 */
36static std::mutex blockedThreadListMutex;
37static 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) {
64    std::lock_guard<std::mutex> lock(blockedThreadListMutex);
65    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) {
79    std::lock_guard<std::mutex> lock(blockedThreadListMutex);
80    // 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() {
94    std::lock_guard<std::mutex> lock(blockedThreadListMutex);
95    // 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}
105