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 10 matching lines...) Expand all Loading... | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 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.cc | 31 * This file is a Linux-specific part of spinlock_internal.cc |
|
antonm
2011/06/02 13:37:35
which parts were conflicts on merge?
Alexander Potapenko
2011/06/09 09:35:24
Well, these weren't conflicts, but some lines had
| |
| 32 */ | 32 */ |
| 33 | 33 |
| 34 #include <sched.h> | 34 #include <sched.h> |
| 35 #include <time.h> | 35 #include <time.h> |
| 36 #include <limits.h> | |
| 37 #include "base/linux_syscall_support.h" | |
| 36 | 38 |
| 37 #define FUTEX_WAIT 0 | 39 #define FUTEX_WAIT 0 |
| 38 #define FUTEX_WAKE 1 | 40 #define FUTEX_WAKE 1 |
| 39 #define FUTEX_PRIVATE_FLAG 128 | 41 #define FUTEX_PRIVATE_FLAG 128 |
| 40 | 42 |
| 41 // Note: Instead of making direct system calls that are inlined, we rely | 43 // 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 | 44 // on the syscall() function in glibc to do the right thing. This |
| 43 // is necessary to make the code compatible with the seccomp sandbox, | 45 // 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 | 46 // 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 | 47 // calls are made. Scanning through and patching glibc is fast, but |
| 46 // doing so on the entire Chrome binary would be prohibitively | 48 // doing so on the entire Chrome binary would be prohibitively |
| 47 // expensive. | 49 // expensive. |
| 48 // This is a notable change from the upstream version of tcmalloc, | 50 // This is a notable change from the upstream version of tcmalloc, |
| 49 // which prefers direct system calls in order to improve compatibility | 51 // which prefers direct system calls in order to improve compatibility |
| 50 // with older toolchains and runtime libraries. | 52 // with older toolchains and runtime libraries. |
| 51 | 53 |
| 52 static bool have_futex; | 54 static bool have_futex; |
| 53 static int futex_private_flag = FUTEX_PRIVATE_FLAG; | 55 static int futex_private_flag = FUTEX_PRIVATE_FLAG; |
| 54 | 56 |
| 55 namespace { | 57 namespace { |
| 56 static struct InitModule { | 58 static struct InitModule { |
| 57 InitModule() { | 59 InitModule() { |
| 58 int x = 0; | 60 int x = 0; |
| 59 // futexes are ints, so we can use them only when | 61 // futexes are ints, so we can use them only when |
| 60 // that's the same size as the lockword_ in SpinLock. | 62 // that's the same size as the lockword_ in SpinLock. |
| 63 #ifdef __arm__ | |
| 64 // ARM linux doesn't support sys_futex1(void*, int, int, struct timespec*); | |
| 65 have_futex = 0; | |
| 66 #else | |
| 61 have_futex = (sizeof (Atomic32) == sizeof (int) && | 67 have_futex = (sizeof (Atomic32) == sizeof (int) && |
| 62 syscall(__NR_futex, &x, FUTEX_WAKE, 1, 0) >= 0); | 68 syscall(__NR_futex, &x, FUTEX_WAKE, 1, 0) >= 0); |
| 69 #endif | |
| 63 if (have_futex && | 70 if (have_futex && |
| 64 syscall(__NR_futex, &x, FUTEX_WAKE | futex_private_flag, 1, 0) < 0) { | 71 syscall(__NR_futex, &x, FUTEX_WAKE | futex_private_flag, 1, 0) < 0) { |
| 65 futex_private_flag = 0; | 72 futex_private_flag = 0; |
| 66 } | 73 } |
| 67 } | 74 } |
| 68 } init_module; | 75 } init_module; |
| 76 | |
| 69 } // anonymous namespace | 77 } // anonymous namespace |
| 70 | 78 |
| 71 static void SpinLockWait(volatile Atomic32 *w) { | 79 |
| 72 int save_errno = errno; | 80 namespace base { |
| 73 struct timespec tm; | 81 namespace internal { |
| 74 tm.tv_sec = 0; | 82 |
| 75 if (have_futex) { | 83 void SpinLockDelay(volatile Atomic32 *w, int32 value, int loop) { |
| 76 int value; | 84 if (loop != 0) { |
| 77 tm.tv_nsec = 1000000; // 1ms; really we're trying to sleep for one kernel | 85 int save_errno = errno; |
| 78 // clock tick | 86 struct timespec tm; |
| 79 while ((value = base::subtle::Acquire_CompareAndSwap(w, 0, 1)) != 0) { | 87 tm.tv_sec = 0; |
| 88 if (have_futex) { | |
| 89 tm.tv_nsec = 1000000; // 1ms; really we're trying to sleep for one | |
| 90 // kernel clock tick | |
| 91 } else { | |
| 92 tm.tv_nsec = 2000001; // above 2ms so linux 2.4 doesn't spin | |
| 93 } | |
| 94 if (have_futex) { | |
| 80 syscall(__NR_futex, reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), | 95 syscall(__NR_futex, reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), |
| 81 FUTEX_WAIT | futex_private_flag, | 96 FUTEX_WAIT | futex_private_flag, |
|
Alexander Potapenko
2011/06/09 09:35:24
Here.
| |
| 82 value, reinterpret_cast<struct kernel_timespec *>(&tm)); | 97 value, reinterpret_cast<struct kernel_timespec *>(&tm)); |
| 83 } | 98 } else { |
| 84 } else { | |
| 85 tm.tv_nsec = 2000001; // above 2ms so linux 2.4 doesn't spin | |
| 86 if (base::subtle::NoBarrier_Load(w) != 0) { | |
| 87 sched_yield(); | |
| 88 } | |
| 89 while (base::subtle::Acquire_CompareAndSwap(w, 0, 1) != 0) { | |
| 90 nanosleep(&tm, NULL); | 99 nanosleep(&tm, NULL); |
| 91 } | 100 } |
| 101 errno = save_errno; | |
| 92 } | 102 } |
| 93 errno = save_errno; | |
| 94 } | 103 } |
| 95 | 104 |
| 96 static void SpinLockWake(volatile Atomic32 *w) { | 105 void SpinLockWake(volatile Atomic32 *w, bool all) { |
| 97 if (have_futex) { | 106 if (have_futex) { |
| 98 syscall(__NR_futex, reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), | 107 syscall(__NR_futex, reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), |
| 99 FUTEX_WAKE | futex_private_flag, 1, 0); | 108 FUTEX_WAKE | futex_private_flag, 1, 0); |
| 100 } | 109 } |
| 101 } | 110 } |
| 111 | |
| 112 } // namespace internal | |
| 113 } // namespace base | |
| OLD | NEW |