OLD | NEW |
1 /* Copyright (c) 2009, Google Inc. | 1 /* Copyright (c) 2009, Google Inc. |
2 * All rights reserved. | 2 * All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 * | 29 * |
30 * --- | 30 * --- |
31 * This file is a Linux-specific part of spinlock_internal.cc | 31 * This file is a Linux-specific part of spinlock_internal.cc |
32 */ | 32 */ |
33 | 33 |
34 #include <errno.h> | |
35 #include <sched.h> | 34 #include <sched.h> |
36 #include <time.h> | 35 #include <time.h> |
37 #include <limits.h> | 36 #include <limits.h> |
38 #include "base/linux_syscall_support.h" | 37 #include "base/linux_syscall_support.h" |
39 | 38 |
40 #define FUTEX_WAIT 0 | 39 #define FUTEX_WAIT 0 |
41 #define FUTEX_WAKE 1 | 40 #define FUTEX_WAKE 1 |
42 #define FUTEX_PRIVATE_FLAG 128 | 41 #define FUTEX_PRIVATE_FLAG 128 |
43 | 42 |
44 static bool have_futex; | 43 static bool have_futex; |
(...skipping 24 matching lines...) Expand all Loading... |
69 | 68 |
70 namespace base { | 69 namespace base { |
71 namespace internal { | 70 namespace internal { |
72 | 71 |
73 void SpinLockDelay(volatile Atomic32 *w, int32 value, int loop) { | 72 void SpinLockDelay(volatile Atomic32 *w, int32 value, int loop) { |
74 if (loop != 0) { | 73 if (loop != 0) { |
75 int save_errno = errno; | 74 int save_errno = errno; |
76 struct timespec tm; | 75 struct timespec tm; |
77 tm.tv_sec = 0; | 76 tm.tv_sec = 0; |
78 if (have_futex) { | 77 if (have_futex) { |
79 tm.tv_nsec = base::internal::SuggestedDelayNS(loop); | 78 tm.tv_nsec = 1000000; // 1ms; really we're trying to sleep for one |
| 79 // kernel clock tick |
80 } else { | 80 } else { |
81 tm.tv_nsec = 2000001; // above 2ms so linux 2.4 doesn't spin | 81 tm.tv_nsec = 2000001; // above 2ms so linux 2.4 doesn't spin |
82 } | 82 } |
83 if (have_futex) { | 83 if (have_futex) { |
84 tm.tv_nsec *= 16; // increase the delay; we expect explicit wakeups | |
85 sys_futex(reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), | 84 sys_futex(reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), |
86 FUTEX_WAIT | futex_private_flag, | 85 FUTEX_WAIT | futex_private_flag, |
87 value, reinterpret_cast<struct kernel_timespec *>(&tm)); | 86 value, reinterpret_cast<struct kernel_timespec *>(&tm)); |
88 } else { | 87 } else { |
89 nanosleep(&tm, NULL); | 88 nanosleep(&tm, NULL); |
90 } | 89 } |
91 errno = save_errno; | 90 errno = save_errno; |
92 } | 91 } |
93 } | 92 } |
94 | 93 |
95 void SpinLockWake(volatile Atomic32 *w, bool all) { | 94 void SpinLockWake(volatile Atomic32 *w, bool all) { |
96 if (have_futex) { | 95 if (have_futex) { |
97 sys_futex(reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), | 96 sys_futex(reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), |
98 FUTEX_WAKE | futex_private_flag, all? INT_MAX : 1, 0); | 97 FUTEX_WAKE | futex_private_flag, all? INT_MAX : 1, 0); |
99 } | 98 } |
100 } | 99 } |
101 | 100 |
102 } // namespace internal | 101 } // namespace internal |
103 } // namespace base | 102 } // namespace base |
OLD | NEW |