summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-24 02:04:37 +0000
committerrsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-24 02:04:37 +0000
commita7979fe0f9963a24c6546874f0ad5024c5cc12f9 (patch)
treea90a132efceea9957478d7f3a1bde91536a79a37
parenta8e5b70fce9edfee935215237540e36ba5d8faf1 (diff)
downloadchromium_src-a7979fe0f9963a24c6546874f0ad5024c5cc12f9.zip
chromium_src-a7979fe0f9963a24c6546874f0ad5024c5cc12f9.tar.gz
chromium_src-a7979fe0f9963a24c6546874f0ad5024c5cc12f9.tar.bz2
[Mac] In PlatformThread::CurrentId(), use pthread_self() instead of mach_thread_self().
BUG=105513 TEST=none Review URL: http://codereview.chromium.org/9281011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118780 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/threading/platform_thread.h10
-rw-r--r--base/threading/platform_thread_posix.cc7
-rw-r--r--base/threading/worker_pool_posix_unittest.cc19
3 files changed, 19 insertions, 17 deletions
diff --git a/base/threading/platform_thread.h b/base/threading/platform_thread.h
index f958ef6..3aa3879 100644
--- a/base/threading/platform_thread.h
+++ b/base/threading/platform_thread.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -19,12 +19,8 @@
#include <windows.h>
#elif defined(OS_POSIX)
#include <pthread.h>
-#if defined(OS_MACOSX)
-#include <mach/mach.h>
-#else // OS_POSIX && !OS_MACOSX
#include <unistd.h>
#endif
-#endif
namespace base {
@@ -39,12 +35,8 @@ const PlatformThreadHandle kNullThreadHandle = NULL;
#elif defined(OS_POSIX)
typedef pthread_t PlatformThreadHandle;
const PlatformThreadHandle kNullThreadHandle = 0;
-#if defined(OS_MACOSX)
-typedef mach_port_t PlatformThreadId;
-#else // OS_POSIX && !OS_MACOSX
typedef pid_t PlatformThreadId;
#endif
-#endif
const PlatformThreadId kInvalidThreadId = 0;
diff --git a/base/threading/platform_thread_posix.cc b/base/threading/platform_thread_posix.cc
index 7eda4e9..27e666a 100644
--- a/base/threading/platform_thread_posix.cc
+++ b/base/threading/platform_thread_posix.cc
@@ -16,7 +16,6 @@
#include "base/tracked_objects.h"
#if defined(OS_MACOSX)
-#include <mach/mach.h>
#include <sys/resource.h>
#include <algorithm>
#endif
@@ -132,11 +131,7 @@ bool CreateThread(size_t stack_size, bool joinable,
PlatformThreadId PlatformThread::CurrentId() {
// Pthreads doesn't have the concept of a thread ID, so we have to reach down
// into the kernel.
-#if defined(OS_MACOSX)
- mach_port_t port = mach_thread_self();
- mach_port_deallocate(mach_task_self(), port);
- return port;
-#elif defined(OS_LINUX)
+#if defined(OS_LINUX)
return syscall(__NR_gettid);
#elif defined(OS_ANDROID)
return gettid();
diff --git a/base/threading/worker_pool_posix_unittest.cc b/base/threading/worker_pool_posix_unittest.cc
index 1b700e1..a8d9218 100644
--- a/base/threading/worker_pool_posix_unittest.cc
+++ b/base/threading/worker_pool_posix_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -231,7 +231,22 @@ TEST_F(PosixDynamicThreadPoolTest, Complex) {
pool_->PostTask(FROM_HERE, CreateNewIncrementingTaskCallback());
WaitForIdleThreads(1);
- EXPECT_EQ(3U, unique_threads_.size());
+ // The POSIX implementation of PlatformThread::CurrentId() uses pthread_self()
+ // which is not guaranteed to be unique after a thread joins. The OS X
+ // implemntation of pthread_self() returns the address of the pthread_t, which
+ // is merely a malloc()ed pointer stored in the first TLS slot. When a thread
+ // joins and that structure is freed, the block of memory can be put on the
+ // OS free list, meaning the same address could be reused in a subsequent
+ // allocation. This in fact happens when allocating in a loop as this test
+ // does.
+ //
+ // Because there are two concurrent threads, there's at least the guarantee
+ // of having two unique thread IDs in the set. But after those two threads are
+ // joined, the next-created thread can get a re-used ID if the allocation of
+ // the pthread_t structure is taken from the free list. Therefore, there can
+ // be either 2 or 3 unique thread IDs in the set at this stage in the test.
+ EXPECT_TRUE(unique_threads_.size() >= 2 && unique_threads_.size() <= 3)
+ << "unique_threads_.size() = " << unique_threads_.size();
EXPECT_EQ(1, peer_.num_idle_threads());
EXPECT_EQ(4, counter_);
}