summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2010-08-06 18:12:14 -0700
committerElliott Hughes <enh@google.com>2010-08-09 18:37:07 -0700
commit9f2b1b1c3bf9de560f29f257c855c7c85b405c0f (patch)
treea1e985fdf3152530c1255246b5b168525009f29d /include
parent22dda9e0ecfe848aa870474da50a02246ca6f376 (diff)
downloadlibcore-9f2b1b1c3bf9de560f29f257c855c7c85b405c0f.zip
libcore-9f2b1b1c3bf9de560f29f257c855c7c85b405c0f.tar.gz
libcore-9f2b1b1c3bf9de560f29f257c855c7c85b405c0f.tar.bz2
Make network I/O interruptible.
Every thread about to block on network I/O registers its thread id and the fd it's going to block on. In close, we scan the list and signal every thread that's blocked on the fd we're closing. They wake up with EINTR, see that their java.io.FileDescriptor has been invalidated (by the close code), and infer that this EINTR is not to be retried: this EINTR implies that they should throw. This patch also fixes a couple of bugs in accept. We were trying (and, obviously, failing) to reset SO_RCVTIMEO on fd -1 if the accept failed, and then throwing an exception relating to that rather than the failed accept(2). We were also not treating timeouts as a special case of failure and throwing the appropriate SocketTimeoutException. (One has to suspect that there's an errno-to-Exception function that we could write that would work for all this native code.) This patch also cleans up connect a little more. I've inlined doConnect into its single caller, I've removed the bogus use of 100ms polling, and I've rewritten the checking for success/failure to be based on the advice in Stevens' "Unix Network Programming". Bug: 2823977 Change-Id: I4f0cbd95be9ba25368be166008855a80c5d30845
Diffstat (limited to 'include')
-rw-r--r--include/ScopedPthreadMutexLock.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/include/ScopedPthreadMutexLock.h b/include/ScopedPthreadMutexLock.h
new file mode 100644
index 0000000..90f4596
--- /dev/null
+++ b/include/ScopedPthreadMutexLock.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SCOPED_PTHREAD_MUTEX_LOCK_H_included
+#define SCOPED_PTHREAD_MUTEX_LOCK_H_included
+
+#include <pthread.h>
+
+/**
+ * Locks and unlocks a pthread_mutex_t as it goes in and out of scope.
+ */
+class ScopedPthreadMutexLock {
+public:
+ explicit ScopedPthreadMutexLock(pthread_mutex_t* mutex) : mMutexPtr(mutex) {
+ pthread_mutex_lock(mMutexPtr);
+ }
+
+ ~ScopedPthreadMutexLock() {
+ pthread_mutex_unlock(mMutexPtr);
+ }
+
+private:
+ pthread_mutex_t* mMutexPtr;
+
+ // Disallow copy and assignment.
+ ScopedPthreadMutexLock(const ScopedPthreadMutexLock&);
+ void operator=(const ScopedPthreadMutexLock&);
+};
+
+#endif // SCOPED_PTHREAD_MUTEX_LOCK_H_included