AsynchronousSocketCloseMonitor.cpp revision 679cf68b607e9b4a3beb8bcdee06868ae583386f
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 "AsynchronousSocketCloseMonitor"
18
19#include "AsynchronousSocketCloseMonitor.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 socket is closed is O(n) in the total number of
31 * blocked threads (not the number of threads actually blocked on the socket in question).
32 * For now at least, this seems like a good compromise for Android.
33 */
34static pthread_mutex_t blockedThreadListMutex = PTHREAD_MUTEX_INITIALIZER;
35static AsynchronousSocketCloseMonitor* blockedThreadList = NULL;
36
37/**
38 * The specific signal chosen here is arbitrary.
39 */
40static const int BLOCKED_THREAD_SIGNAL = SIGRTMIN + 2;
41
42static void blockedThreadSignalHandler(int /*signal*/) {
43    // Do nothing. We only sent this signal for its side-effect of interrupting syscalls.
44}
45
46void AsynchronousSocketCloseMonitor::init() {
47    // Ensure that the signal we send interrupts system calls but doesn't kill threads.
48    // Using sigaction(2) lets us ensure that the SA_RESTART flag is not set.
49    // (The whole reason we're sending this signal is to unblock system calls!)
50    struct sigaction sa;
51    memset(&sa, 0, sizeof(sa));
52    sa.sa_handler = blockedThreadSignalHandler;
53    sa.sa_flags = 0;
54    int rc = sigaction(BLOCKED_THREAD_SIGNAL, &sa, NULL);
55    if (rc == -1) {
56        ALOGE("setting blocked thread signal handler failed: %s", strerror(errno));
57    }
58}
59
60void AsynchronousSocketCloseMonitor::signalBlockedThreads(int fd) {
61    ScopedPthreadMutexLock lock(&blockedThreadListMutex);
62    for (AsynchronousSocketCloseMonitor* it = blockedThreadList; it != NULL; it = it->mNext) {
63        if (it->mFd == fd) {
64            pthread_kill(it->mThread, BLOCKED_THREAD_SIGNAL);
65            // Keep going, because there may be more than one thread...
66        }
67    }
68}
69
70AsynchronousSocketCloseMonitor::AsynchronousSocketCloseMonitor(int fd) {
71    ScopedPthreadMutexLock lock(&blockedThreadListMutex);
72    // Who are we, and what are we waiting for?
73    mThread = pthread_self();
74    mFd = fd;
75    // Insert ourselves at the head of the intrusive doubly-linked list...
76    mPrev = NULL;
77    mNext = blockedThreadList;
78    if (mNext != NULL) {
79        mNext->mPrev = this;
80    }
81    blockedThreadList = this;
82}
83
84AsynchronousSocketCloseMonitor::~AsynchronousSocketCloseMonitor() {
85    ScopedPthreadMutexLock lock(&blockedThreadListMutex);
86    // Unlink ourselves from the intrusive doubly-linked list...
87    if (mNext != NULL) {
88        mNext->mPrev = mPrev;
89    }
90    if (mPrev == NULL) {
91        blockedThreadList = mNext;
92    } else {
93        mPrev->mNext = mNext;
94    }
95}
96