summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-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_);
}