Chromium Code Reviews| 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 15 matching lines...) Expand all Loading... | |
| 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.cc | 31 * This file is a Linux-specific part of spinlock.cc |
| 32 */ | 32 */ |
| 33 | 33 |
| 34 #include <sched.h> | 34 #include <sched.h> |
| 35 #include <time.h> | 35 #include <time.h> |
| 36 #include "base/linux_syscall_support.h" | |
| 37 | 36 |
| 38 #define FUTEX_WAIT 0 | 37 #define FUTEX_WAIT 0 |
| 39 #define FUTEX_WAKE 1 | 38 #define FUTEX_WAKE 1 |
| 40 #define FUTEX_PRIVATE_FLAG 128 | 39 #define FUTEX_PRIVATE_FLAG 128 |
| 41 | 40 |
| 41 // Note: Instead of making direct system calls that are inlined, we rely | |
| 42 // on the syscall() function in glibc to do the right thing. This | |
| 43 // is necessary to make the code compatible with the seccomp sandbox, | |
| 44 // which needs to be able to find and patch all places where system | |
| 45 // calls are made. Scanning through and patching glibc is fast, but | |
| 46 // doing so on the entire Chrome binary would be prohibitively | |
| 47 // expensive. | |
| 48 // This is a notable change from the upstream version of tcmalloc, | |
| 49 // which prefers direct system calls in order to improve compatibility | |
| 50 // with older toolchains and runtime libraries. | |
| 51 | |
| 42 static bool have_futex; | 52 static bool have_futex; |
| 43 static int futex_private_flag = FUTEX_PRIVATE_FLAG; | 53 static int futex_private_flag = FUTEX_PRIVATE_FLAG; |
| 44 | 54 |
| 45 namespace { | 55 namespace { |
| 46 static struct InitModule { | 56 static struct InitModule { |
| 47 InitModule() { | 57 InitModule() { |
| 48 int x = 0; | 58 int x = 0; |
| 49 // futexes are ints, so we can use them only when | 59 // futexes are ints, so we can use them only when |
| 50 // that's the same size as the lockword_ in SpinLock. | 60 // that's the same size as the lockword_ in SpinLock. |
| 51 have_futex = (sizeof (Atomic32) == sizeof (int) && | 61 have_futex = (sizeof (Atomic32) == sizeof (int) && |
| 52 sys_futex(&x, FUTEX_WAKE, 1, 0) >= 0); | 62 syscall(__NR_futex, &x, FUTEX_WAKE, 1, 0) >= 0); |
| 53 if (have_futex && | 63 if (have_futex && |
| 54 sys_futex(&x, FUTEX_WAKE | futex_private_flag, 1, 0) < 0) { | 64 syscall(__NR_futex, &x, FUTEX_WAKE | futex_private_flag, 1, 0) < 0) { |
| 55 futex_private_flag = 0; | 65 futex_private_flag = 0; |
| 56 } | 66 } |
| 57 } | 67 } |
| 58 } init_module; | 68 } init_module; |
| 59 } // anonymous namespace | 69 } // anonymous namespace |
| 60 | 70 |
| 61 static void SpinLockWait(volatile Atomic32 *w) { | 71 static void SpinLockWait(volatile Atomic32 *w) { |
| 62 int save_errno = errno; | 72 int save_errno = errno; |
| 63 struct timespec tm; | 73 struct timespec tm; |
| 64 tm.tv_sec = 0; | 74 tm.tv_sec = 0; |
| 65 if (have_futex) { | 75 if (have_futex) { |
| 66 int value; | 76 int value; |
| 67 tm.tv_nsec = 1000000; // 1ms; really we're trying to sleep for one kernel | 77 tm.tv_nsec = 1000000; // 1ms; really we're trying to sleep for one kernel |
| 68 // clock tick | 78 // clock tick |
| 69 while ((value = base::subtle::Acquire_CompareAndSwap(w, 0, 1)) != 0) { | 79 while ((value = base::subtle::Acquire_CompareAndSwap(w, 0, 1)) != 0) { |
| 70 sys_futex(reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), | 80 syscall(__NR_futex, reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), |
| 71 FUTEX_WAIT | futex_private_flag, | 81 FUTEX_WAIT | futex_private_flag, |
| 72 value, reinterpret_cast<struct kernel_timespec *>(&tm)); | 82 value, reinterpret_cast<struct kernel_timespec *>(&tm)); |
| 73 } | 83 } |
| 74 } else { | 84 } else { |
| 75 tm.tv_nsec = 2000001; // above 2ms so linux 2.4 doesn't spin | 85 tm.tv_nsec = 2000001; // above 2ms so linux 2.4 doesn't spin |
| 76 if (base::subtle::NoBarrier_Load(w) != 0) { | 86 if (base::subtle::NoBarrier_Load(w) != 0) { |
| 77 sched_yield(); | 87 sched_yield(); |
| 78 } | 88 } |
| 79 while (base::subtle::Acquire_CompareAndSwap(w, 0, 1) != 0) { | 89 while (base::subtle::Acquire_CompareAndSwap(w, 0, 1) != 0) { |
| 80 nanosleep(&tm, NULL); | 90 nanosleep(&tm, NULL); |
|
Markus (顧孟勤)
2013/01/25 22:24:00
I was researching some unrelated problem, and came
| |
| 81 } | 91 } |
| 82 } | 92 } |
| 83 errno = save_errno; | 93 errno = save_errno; |
| 84 } | 94 } |
| 85 | 95 |
| 86 static void SpinLockWake(volatile Atomic32 *w) { | 96 static void SpinLockWake(volatile Atomic32 *w) { |
| 87 if (have_futex) { | 97 if (have_futex) { |
| 88 sys_futex(reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), | 98 syscall(__NR_futex, reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), |
| 89 FUTEX_WAKE | futex_private_flag, 1, 0); | 99 FUTEX_WAKE | futex_private_flag, 1, 0); |
| 90 } | 100 } |
| 91 } | 101 } |
| OLD | NEW |