blob: 79e17f3d94625928b31945363d000144b4014b4c [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
brettw@google.come3c034a2008-08-08 03:31:40 +09005#include "base/platform_thread.h"
6
paulg@google.comc8eeb752008-08-13 10:20:26 +09007#include <errno.h>
deanm@google.comc1c5cf52008-08-06 19:06:59 +09008#include <sched.h>
deanm@google.comc1c5cf52008-08-06 19:06:59 +09009
pinkerton@google.com44159e42008-08-14 08:20:03 +090010#if defined(OS_MACOSX)
11#include <mach/mach.h>
12#elif defined(OS_LINUX)
tc@google.com0e5744d2008-08-14 08:55:02 +090013#include <sys/syscall.h>
pinkerton@google.com9d2fd8a2008-08-14 08:34:26 +090014#include <unistd.h>
pinkerton@google.com44159e42008-08-14 08:20:03 +090015#endif
16
darin@google.comc18d7ae2008-08-21 18:46:32 +090017static void* ThreadFunc(void* closure) {
18 PlatformThread::Delegate* delegate =
19 static_cast<PlatformThread::Delegate*>(closure);
20 delegate->ThreadMain();
21 return NULL;
paulg@google.comc8eeb752008-08-13 10:20:26 +090022}
23
pinkerton@google.com44159e42008-08-14 08:20:03 +090024// static
25int PlatformThread::CurrentId() {
darin@google.comc18d7ae2008-08-21 18:46:32 +090026 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
27 // into the kernel.
28#if defined(OS_MACOSX)
pinkerton@google.com44159e42008-08-14 08:20:03 +090029 return mach_thread_self();
30#elif defined(OS_LINUX)
tc@google.com0e5744d2008-08-14 08:55:02 +090031 return syscall(__NR_gettid);
pinkerton@google.com44159e42008-08-14 08:20:03 +090032#endif
33}
34
darin@google.comc18d7ae2008-08-21 18:46:32 +090035// static
36void PlatformThread::YieldCurrentThread() {
37 sched_yield();
38}
39
40// static
41void PlatformThread::Sleep(int duration_ms) {
42 struct timespec sleep_time, remaining;
43
44 // Contains the portion of duration_ms >= 1 sec.
45 sleep_time.tv_sec = duration_ms / 1000;
46 duration_ms -= sleep_time.tv_sec * 1000;
47
48 // Contains the portion of duration_ms < 1 sec.
49 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds.
50
51 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR)
52 sleep_time = remaining;
53}
54
55// static
56void PlatformThread::SetName(int thread_id, const char* name) {
57 // TODO(darin): implement me!
58}
59
60// static
61bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
62 PlatformThreadHandle* thread_handle) {
63 bool success = false;
64 pthread_attr_t attributes;
65 pthread_attr_init(&attributes);
66
67 // Pthreads are joinable by default, so we don't need to specify any special
68 // attributes to be able to call pthread_join later.
69
70 if (stack_size > 0)
71 pthread_attr_setstacksize(&attributes, stack_size);
72
73 success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate);
74
75 pthread_attr_destroy(&attributes);
76 return success;
77}
78
79// static
80void PlatformThread::Join(PlatformThreadHandle thread_handle) {
81 pthread_join(thread_handle, NULL);
initial.commit3f4a7322008-07-27 06:49:38 +090082}
license.botf003cfe2008-08-24 09:55:55 +090083