OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sched.h> | 8 #include <sched.h> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 #endif | 145 #endif |
146 } | 146 } |
147 | 147 |
148 // static | 148 // static |
149 void PlatformThread::YieldCurrentThread() { | 149 void PlatformThread::YieldCurrentThread() { |
150 sched_yield(); | 150 sched_yield(); |
151 } | 151 } |
152 | 152 |
153 // static | 153 // static |
154 void PlatformThread::Sleep(int duration_ms) { | 154 void PlatformThread::Sleep(int duration_ms) { |
| 155 // NOTE: This function will be supplanted by the other version of Sleep |
| 156 // in the future. See issue 108171 for more information. |
| 157 Sleep(TimeDelta::FromMilliseconds(duration_ms)); |
| 158 } |
| 159 |
| 160 // static |
| 161 void PlatformThread::Sleep(TimeDelta duration) { |
155 struct timespec sleep_time, remaining; | 162 struct timespec sleep_time, remaining; |
156 | 163 |
157 // Contains the portion of duration_ms >= 1 sec. | 164 // Break the duration into seconds and nanoseconds. |
158 sleep_time.tv_sec = duration_ms / 1000; | 165 // NOTE: TimeDelta's microseconds are int64s while timespec's |
159 duration_ms -= sleep_time.tv_sec * 1000; | 166 // nanoseconds are longs, so this unpacking must prevent overflow. |
160 | 167 sleep_time.tv_sec = duration.InSeconds(); |
161 // Contains the portion of duration_ms < 1 sec. | 168 duration -= TimeDelta::FromSeconds(sleep_time.tv_sec); |
162 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. | 169 sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds |
163 | 170 |
164 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) | 171 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) |
165 sleep_time = remaining; | 172 sleep_time = remaining; |
166 } | 173 } |
167 | 174 |
168 #if defined(OS_LINUX) | 175 #if defined(OS_LINUX) |
169 // static | 176 // static |
170 void PlatformThread::SetName(const char* name) { | 177 void PlatformThread::SetName(const char* name) { |
171 // have to cast away const because ThreadLocalPointer does not support const | 178 // have to cast away const because ThreadLocalPointer does not support const |
172 // void* | 179 // void* |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 // Mac OS X uses lower-level mach APIs | 249 // Mac OS X uses lower-level mach APIs |
243 | 250 |
244 // static | 251 // static |
245 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { | 252 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { |
246 // TODO(crogers): implement | 253 // TODO(crogers): implement |
247 NOTIMPLEMENTED(); | 254 NOTIMPLEMENTED(); |
248 } | 255 } |
249 #endif | 256 #endif |
250 | 257 |
251 } // namespace base | 258 } // namespace base |
OLD | NEW |