license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // 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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
brettw@google.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 5 | #include "base/platform_thread.h" |
| 6 | |
paulg@google.com | c8eeb75 | 2008-08-13 10:20:26 +0900 | [diff] [blame] | 7 | #include <errno.h> |
deanm@google.com | c1c5cf5 | 2008-08-06 19:06:59 +0900 | [diff] [blame] | 8 | #include <sched.h> |
deanm@google.com | c1c5cf5 | 2008-08-06 19:06:59 +0900 | [diff] [blame] | 9 | |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 10 | #if defined(OS_MACOSX) |
| 11 | #include <mach/mach.h> |
| 12 | #elif defined(OS_LINUX) |
tc@google.com | 0e5744d | 2008-08-14 08:55:02 +0900 | [diff] [blame] | 13 | #include <sys/syscall.h> |
pinkerton@google.com | 9d2fd8a | 2008-08-14 08:34:26 +0900 | [diff] [blame] | 14 | #include <unistd.h> |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 15 | #endif |
| 16 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 17 | static void* ThreadFunc(void* closure) { |
| 18 | PlatformThread::Delegate* delegate = |
| 19 | static_cast<PlatformThread::Delegate*>(closure); |
| 20 | delegate->ThreadMain(); |
| 21 | return NULL; |
paulg@google.com | c8eeb75 | 2008-08-13 10:20:26 +0900 | [diff] [blame] | 22 | } |
| 23 | |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 24 | // static |
| 25 | int PlatformThread::CurrentId() { |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 26 | // 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.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 29 | return mach_thread_self(); |
| 30 | #elif defined(OS_LINUX) |
tc@google.com | 0e5744d | 2008-08-14 08:55:02 +0900 | [diff] [blame] | 31 | return syscall(__NR_gettid); |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 32 | #endif |
| 33 | } |
| 34 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 35 | // static |
| 36 | void PlatformThread::YieldCurrentThread() { |
| 37 | sched_yield(); |
| 38 | } |
| 39 | |
| 40 | // static |
| 41 | void 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 |
| 56 | void PlatformThread::SetName(int thread_id, const char* name) { |
| 57 | // TODO(darin): implement me! |
| 58 | } |
| 59 | |
| 60 | // static |
| 61 | bool 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 |
| 80 | void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 81 | pthread_join(thread_handle, NULL); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 82 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 83 | |