Home | History | Annotate | Download | only in native
      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 #if defined(__APPLE__)
     42 static const int BLOCKED_THREAD_SIGNAL = SIGUSR2;
     43 #else
     44 static const int BLOCKED_THREAD_SIGNAL = __SIGRTMIN + 2;
     45 #endif
     46 
     47 static void blockedThreadSignalHandler(int /*signal*/) {
     48     // Do nothing. We only sent this signal for its side-effect of interrupting syscalls.
     49 }
     50 
     51 void AsynchronousCloseMonitor::init() {
     52     // Ensure that the signal we send interrupts system calls but doesn't kill threads.
     53     // Using sigaction(2) lets us ensure that the SA_RESTART flag is not set.
     54     // (The whole reason we're sending this signal is to unblock system calls!)
     55     struct sigaction sa;
     56     memset(&sa, 0, sizeof(sa));
     57     sa.sa_handler = blockedThreadSignalHandler;
     58     sa.sa_flags = 0;
     59     int rc = sigaction(BLOCKED_THREAD_SIGNAL, &sa, NULL);
     60     if (rc == -1) {
     61         ALOGE("setting blocked thread signal handler failed: %s", strerror(errno));
     62     }
     63 }
     64 
     65 void AsynchronousCloseMonitor::signalBlockedThreads(int fd) {
     66     ScopedPthreadMutexLock lock(&blockedThreadListMutex);
     67     for (AsynchronousCloseMonitor* it = blockedThreadList; it != NULL; it = it->mNext) {
     68         if (it->mFd == fd) {
     69             it->mSignaled = true;
     70             pthread_kill(it->mThread, BLOCKED_THREAD_SIGNAL);
     71             // Keep going, because there may be more than one thread...
     72         }
     73     }
     74 }
     75 
     76 bool AsynchronousCloseMonitor::wasSignaled() const {
     77     return mSignaled;
     78 }
     79 
     80 AsynchronousCloseMonitor::AsynchronousCloseMonitor(int fd) {
     81     ScopedPthreadMutexLock lock(&blockedThreadListMutex);
     82     // Who are we, and what are we waiting for?
     83     mThread = pthread_self();
     84     mFd = fd;
     85     mSignaled = false;
     86     // Insert ourselves at the head of the intrusive doubly-linked list...
     87     mPrev = NULL;
     88     mNext = blockedThreadList;
     89     if (mNext != NULL) {
     90         mNext->mPrev = this;
     91     }
     92     blockedThreadList = this;
     93 }
     94 
     95 AsynchronousCloseMonitor::~AsynchronousCloseMonitor() {
     96     ScopedPthreadMutexLock lock(&blockedThreadListMutex);
     97     // Unlink ourselves from the intrusive doubly-linked list...
     98     if (mNext != NULL) {
     99         mNext->mPrev = mPrev;
    100     }
    101     if (mPrev == NULL) {
    102         blockedThreadList = mNext;
    103     } else {
    104         mPrev->mNext = mNext;
    105     }
    106 }
    107