• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
27  * We use an intrusive doubly-linked list to keep track of blocked threads.
28  * This gives us O(1) insertion and removal, and means we don't need to do any allocation.
29  * (The objects themselves are stack-allocated.)
30  * Waking potentially-blocked threads when a file descriptor is closed is O(n) in the total number
31  * of blocked threads (not the number of threads actually blocked on the file descriptor in
32  * question). For now at least, this seems like a good compromise for Android.
33  */
34 static pthread_mutex_t blockedThreadListMutex = PTHREAD_MUTEX_INITIALIZER;
35 static AsynchronousCloseMonitor* blockedThreadList = NULL;
36 
37 /**
38  * The specific signal chosen here is arbitrary, but bionic needs to know so that SIGRTMIN
39  * starts at a higher value.
40  */
41 static const int BLOCKED_THREAD_SIGNAL = __SIGRTMIN + 2;
42 
blockedThreadSignalHandler(int)43 static void blockedThreadSignalHandler(int /*signal*/) {
44     // Do nothing. We only sent this signal for its side-effect of interrupting syscalls.
45 }
46 
init()47 void AsynchronousCloseMonitor::init() {
48     // Ensure that the signal we send interrupts system calls but doesn't kill threads.
49     // Using sigaction(2) lets us ensure that the SA_RESTART flag is not set.
50     // (The whole reason we're sending this signal is to unblock system calls!)
51     struct sigaction sa;
52     memset(&sa, 0, sizeof(sa));
53     sa.sa_handler = blockedThreadSignalHandler;
54     sa.sa_flags = 0;
55     int rc = sigaction(BLOCKED_THREAD_SIGNAL, &sa, NULL);
56     if (rc == -1) {
57         ALOGE("setting blocked thread signal handler failed: %s", strerror(errno));
58     }
59 }
60 
signalBlockedThreads(int fd)61 void AsynchronousCloseMonitor::signalBlockedThreads(int fd) {
62     ScopedPthreadMutexLock lock(&blockedThreadListMutex);
63     for (AsynchronousCloseMonitor* it = blockedThreadList; it != NULL; it = it->mNext) {
64         if (it->mFd == fd) {
65             it->mSignaled = true;
66             pthread_kill(it->mThread, BLOCKED_THREAD_SIGNAL);
67             // Keep going, because there may be more than one thread...
68         }
69     }
70 }
71 
wasSignaled() const72 bool AsynchronousCloseMonitor::wasSignaled() const {
73     return mSignaled;
74 }
75 
AsynchronousCloseMonitor(int fd)76 AsynchronousCloseMonitor::AsynchronousCloseMonitor(int fd) {
77     ScopedPthreadMutexLock lock(&blockedThreadListMutex);
78     // Who are we, and what are we waiting for?
79     mThread = pthread_self();
80     mFd = fd;
81     mSignaled = false;
82     // Insert ourselves at the head of the intrusive doubly-linked list...
83     mPrev = NULL;
84     mNext = blockedThreadList;
85     if (mNext != NULL) {
86         mNext->mPrev = this;
87     }
88     blockedThreadList = this;
89 }
90 
~AsynchronousCloseMonitor()91 AsynchronousCloseMonitor::~AsynchronousCloseMonitor() {
92     ScopedPthreadMutexLock lock(&blockedThreadListMutex);
93     // Unlink ourselves from the intrusive doubly-linked list...
94     if (mNext != NULL) {
95         mNext->mPrev = mPrev;
96     }
97     if (mPrev == NULL) {
98         blockedThreadList = mNext;
99     } else {
100         mPrev->mNext = mNext;
101     }
102 }
103