initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1 | // 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.com | e3c034a | 2008-08-08 03:31:40 +0900 | [diff] [blame] | 30 | #include "base/platform_thread.h" |
| 31 | |
paulg@google.com | c8eeb75 | 2008-08-13 10:20:26 +0900 | [diff] [blame] | 32 | #include <errno.h> |
deanm@google.com | c1c5cf5 | 2008-08-06 19:06:59 +0900 | [diff] [blame] | 33 | #include <sched.h> |
deanm@google.com | c1c5cf5 | 2008-08-06 19:06:59 +0900 | [diff] [blame] | 34 | |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 35 | #if defined(OS_MACOSX) |
| 36 | #include <mach/mach.h> |
| 37 | #elif defined(OS_LINUX) |
tc@google.com | 0e5744d | 2008-08-14 08:55:02 +0900 | [diff] [blame] | 38 | #include <sys/syscall.h> |
pinkerton@google.com | 9d2fd8a | 2008-08-14 08:34:26 +0900 | [diff] [blame] | 39 | #include <unistd.h> |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 40 | #endif |
| 41 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 42 | static void* ThreadFunc(void* closure) { |
| 43 | PlatformThread::Delegate* delegate = |
| 44 | static_cast<PlatformThread::Delegate*>(closure); |
| 45 | delegate->ThreadMain(); |
| 46 | return NULL; |
paulg@google.com | c8eeb75 | 2008-08-13 10:20:26 +0900 | [diff] [blame] | 47 | } |
| 48 | |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 49 | // static |
| 50 | int PlatformThread::CurrentId() { |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 51 | // 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.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 54 | return mach_thread_self(); |
| 55 | #elif defined(OS_LINUX) |
tc@google.com | 0e5744d | 2008-08-14 08:55:02 +0900 | [diff] [blame] | 56 | return syscall(__NR_gettid); |
pinkerton@google.com | 44159e4 | 2008-08-14 08:20:03 +0900 | [diff] [blame] | 57 | #endif |
| 58 | } |
| 59 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 60 | // static |
| 61 | void PlatformThread::YieldCurrentThread() { |
| 62 | sched_yield(); |
| 63 | } |
| 64 | |
| 65 | // static |
| 66 | void 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 |
| 81 | void PlatformThread::SetName(int thread_id, const char* name) { |
| 82 | // TODO(darin): implement me! |
| 83 | } |
| 84 | |
| 85 | // static |
| 86 | bool 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 |
| 105 | void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 106 | pthread_join(thread_handle, NULL); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 107 | } |