blob: 00d76ddec0cd839be1bcf07565bbc01e7348fa78 [file] [log] [blame]
initial.commit3f4a7322008-07-27 06:49:38 +09001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
brettw@google.come3c034a2008-08-08 03:31:40 +090030#include "base/platform_thread.h"
31
paulg@google.comc8eeb752008-08-13 10:20:26 +090032#include <errno.h>
deanm@google.comc1c5cf52008-08-06 19:06:59 +090033#include <sched.h>
deanm@google.comc1c5cf52008-08-06 19:06:59 +090034
pinkerton@google.com44159e42008-08-14 08:20:03 +090035#if defined(OS_MACOSX)
36#include <mach/mach.h>
37#elif defined(OS_LINUX)
tc@google.com0e5744d2008-08-14 08:55:02 +090038#include <sys/syscall.h>
pinkerton@google.com9d2fd8a2008-08-14 08:34:26 +090039#include <unistd.h>
pinkerton@google.com44159e42008-08-14 08:20:03 +090040#endif
41
darin@google.comc18d7ae2008-08-21 18:46:32 +090042static void* ThreadFunc(void* closure) {
43 PlatformThread::Delegate* delegate =
44 static_cast<PlatformThread::Delegate*>(closure);
45 delegate->ThreadMain();
46 return NULL;
paulg@google.comc8eeb752008-08-13 10:20:26 +090047}
48
pinkerton@google.com44159e42008-08-14 08:20:03 +090049// static
50int PlatformThread::CurrentId() {
darin@google.comc18d7ae2008-08-21 18:46:32 +090051 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
52 // into the kernel.
53#if defined(OS_MACOSX)
pinkerton@google.com44159e42008-08-14 08:20:03 +090054 return mach_thread_self();
55#elif defined(OS_LINUX)
tc@google.com0e5744d2008-08-14 08:55:02 +090056 return syscall(__NR_gettid);
pinkerton@google.com44159e42008-08-14 08:20:03 +090057#endif
58}
59
darin@google.comc18d7ae2008-08-21 18:46:32 +090060// static
61void PlatformThread::YieldCurrentThread() {
62 sched_yield();
63}
64
65// static
66void PlatformThread::Sleep(int duration_ms) {
67 struct timespec sleep_time, remaining;
68
69 // Contains the portion of duration_ms >= 1 sec.
70 sleep_time.tv_sec = duration_ms / 1000;
71 duration_ms -= sleep_time.tv_sec * 1000;
72
73 // Contains the portion of duration_ms < 1 sec.
74 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds.
75
76 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR)
77 sleep_time = remaining;
78}
79
80// static
81void PlatformThread::SetName(int thread_id, const char* name) {
82 // TODO(darin): implement me!
83}
84
85// static
86bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
87 PlatformThreadHandle* thread_handle) {
88 bool success = false;
89 pthread_attr_t attributes;
90 pthread_attr_init(&attributes);
91
92 // Pthreads are joinable by default, so we don't need to specify any special
93 // attributes to be able to call pthread_join later.
94
95 if (stack_size > 0)
96 pthread_attr_setstacksize(&attributes, stack_size);
97
98 success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate);
99
100 pthread_attr_destroy(&attributes);
101 return success;
102}
103
104// static
105void PlatformThread::Join(PlatformThreadHandle thread_handle) {
106 pthread_join(thread_handle, NULL);
initial.commit3f4a7322008-07-27 06:49:38 +0900107}