diff options
author | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-21 09:46:32 +0000 |
---|---|---|
committer | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-21 09:46:32 +0000 |
commit | e9ba26d8303272f6a4a75b53ebf4b96ae8df05f6 (patch) | |
tree | 6d3fee93c5775d976bc2d345e955bc395241d8fe /base/platform_thread_posix.cc | |
parent | 1a3ebe10ead2ca99d2beb45b9ec65a2863406184 (diff) | |
download | chromium_src-e9ba26d8303272f6a4a75b53ebf4b96ae8df05f6.zip chromium_src-e9ba26d8303272f6a4a75b53ebf4b96ae8df05f6.tar.gz chromium_src-e9ba26d8303272f6a4a75b53ebf4b96ae8df05f6.tar.bz2 |
Port the Thread class properly to Mac and Linux. This involved porting
PlatformThread and creating an API on PlatformThread to create a native thread
and later join with it. I also made a few other tweaks to PlatformThread, such
as eliminating the Current() method which appears to be unused. The
operator==() is also unused, so I killed it as well.
I made a few other miscellaneous cleanups and adjustments.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1146 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/platform_thread_posix.cc')
-rw-r--r-- | base/platform_thread_posix.cc | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/base/platform_thread_posix.cc b/base/platform_thread_posix.cc new file mode 100644 index 0000000..00d76dd --- /dev/null +++ b/base/platform_thread_posix.cc @@ -0,0 +1,107 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "base/platform_thread.h" + +#include <errno.h> +#include <sched.h> + +#if defined(OS_MACOSX) +#include <mach/mach.h> +#elif defined(OS_LINUX) +#include <sys/syscall.h> +#include <unistd.h> +#endif + +static void* ThreadFunc(void* closure) { + PlatformThread::Delegate* delegate = + static_cast<PlatformThread::Delegate*>(closure); + delegate->ThreadMain(); + return NULL; +} + +// static +int 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) + return mach_thread_self(); +#elif defined(OS_LINUX) + return syscall(__NR_gettid); +#endif +} + +// static +void PlatformThread::YieldCurrentThread() { + sched_yield(); +} + +// static +void PlatformThread::Sleep(int duration_ms) { + struct timespec sleep_time, remaining; + + // Contains the portion of duration_ms >= 1 sec. + sleep_time.tv_sec = duration_ms / 1000; + duration_ms -= sleep_time.tv_sec * 1000; + + // Contains the portion of duration_ms < 1 sec. + sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. + + while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) + sleep_time = remaining; +} + +// static +void PlatformThread::SetName(int thread_id, const char* name) { + // TODO(darin): implement me! +} + +// static +bool PlatformThread::Create(size_t stack_size, Delegate* delegate, + PlatformThreadHandle* thread_handle) { + bool success = false; + pthread_attr_t attributes; + pthread_attr_init(&attributes); + + // Pthreads are joinable by default, so we don't need to specify any special + // attributes to be able to call pthread_join later. + + if (stack_size > 0) + pthread_attr_setstacksize(&attributes, stack_size); + + success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate); + + pthread_attr_destroy(&attributes); + return success; +} + +// static +void PlatformThread::Join(PlatformThreadHandle thread_handle) { + pthread_join(thread_handle, NULL); +} |