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