2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "AsynchronousSocketCloseMonitor"
19 #include "AsynchronousSocketCloseMonitor.h"
20 #include "cutils/log.h"
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.
34 static pthread_mutex_t blockedThreadListMutex = PTHREAD_MUTEX_INITIALIZER;
35 static AsynchronousSocketCloseMonitor* blockedThreadList = NULL;
38 * The specific signal chosen here is arbitrary.
40 static const int BLOCKED_THREAD_SIGNAL = SIGRTMIN + 2;
42 static void blockedThreadSignalHandler(int /*signal*/) {
43 // Do nothing. We only sent this signal for its side-effect of interrupting syscalls.
46 void 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!)
51 memset(&sa, 0, sizeof(sa));
52 sa.sa_handler = blockedThreadSignalHandler;
54 int rc = sigaction(BLOCKED_THREAD_SIGNAL, &sa, NULL);
56 LOGE("setting blocked thread signal handler failed: %s", strerror(errno));
60 void AsynchronousSocketCloseMonitor::signalBlockedThreads(int fd) {
61 ScopedPthreadMutexLock lock(&blockedThreadListMutex);
62 for (AsynchronousSocketCloseMonitor* it = blockedThreadList; it != NULL; it = it->mNext) {
64 pthread_kill(it->mThread, BLOCKED_THREAD_SIGNAL);
65 // Keep going, because there may be more than one thread...
70 AsynchronousSocketCloseMonitor::AsynchronousSocketCloseMonitor(int fd) {
71 ScopedPthreadMutexLock lock(&blockedThreadListMutex);
72 // Who are we, and what are we waiting for?
73 mThread = pthread_self();
75 // Insert ourselves at the head of the intrusive doubly-linked list...
77 mNext = blockedThreadList;
81 blockedThreadList = this;
84 AsynchronousSocketCloseMonitor::~AsynchronousSocketCloseMonitor() {
85 ScopedPthreadMutexLock lock(&blockedThreadListMutex);
86 // Unlink ourselves from the intrusive doubly-linked list...
91 blockedThreadList = mNext;