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 23 matching lines...) Expand all Loading... |
34 #include <errno.h> | 34 #include <errno.h> |
35 #include <sched.h> | 35 #include <sched.h> |
36 #include <time.h> | 36 #include <time.h> |
37 #include <limits.h> | 37 #include <limits.h> |
38 #include "base/linux_syscall_support.h" | 38 #include "base/linux_syscall_support.h" |
39 | 39 |
40 #define FUTEX_WAIT 0 | 40 #define FUTEX_WAIT 0 |
41 #define FUTEX_WAKE 1 | 41 #define FUTEX_WAKE 1 |
42 #define FUTEX_PRIVATE_FLAG 128 | 42 #define FUTEX_PRIVATE_FLAG 128 |
43 | 43 |
| 44 // Note: Instead of making direct system calls that are inlined, we rely |
| 45 // on the syscall() function in glibc to do the right thing. This |
| 46 // is necessary to make the code compatible with the seccomp sandbox, |
| 47 // which needs to be able to find and patch all places where system |
| 48 // calls are made. Scanning through and patching glibc is fast, but |
| 49 // doing so on the entire Chrome binary would be prohibitively |
| 50 // expensive. |
| 51 // This is a notable change from the upstream version of tcmalloc, |
| 52 // which prefers direct system calls in order to improve compatibility |
| 53 // with older toolchains and runtime libraries. |
| 54 |
44 static bool have_futex; | 55 static bool have_futex; |
45 static int futex_private_flag = FUTEX_PRIVATE_FLAG; | 56 static int futex_private_flag = FUTEX_PRIVATE_FLAG; |
46 | 57 |
47 namespace { | 58 namespace { |
48 static struct InitModule { | 59 static struct InitModule { |
49 InitModule() { | 60 InitModule() { |
50 int x = 0; | 61 int x = 0; |
51 // futexes are ints, so we can use them only when | 62 // futexes are ints, so we can use them only when |
52 // that's the same size as the lockword_ in SpinLock. | 63 // that's the same size as the lockword_ in SpinLock. |
53 #ifdef __arm__ | 64 #ifdef __arm__ |
54 // ARM linux doesn't support sys_futex1(void*, int, int, struct timespec*); | 65 // ARM linux doesn't support sys_futex1(void*, int, int, struct timespec*); |
55 have_futex = 0; | 66 have_futex = 0; |
56 #else | 67 #else |
57 have_futex = (sizeof (Atomic32) == sizeof (int) && | 68 have_futex = (sizeof (Atomic32) == sizeof (int) && |
58 sys_futex(&x, FUTEX_WAKE, 1, 0) >= 0); | 69 syscall(__NR_futex, &x, FUTEX_WAKE, 1, 0) >= 0); |
59 #endif | 70 #endif |
60 if (have_futex && | 71 if (have_futex && |
61 sys_futex(&x, FUTEX_WAKE | futex_private_flag, 1, 0) < 0) { | 72 syscall(__NR_futex, &x, FUTEX_WAKE | futex_private_flag, 1, 0) < 0) { |
62 futex_private_flag = 0; | 73 futex_private_flag = 0; |
63 } | 74 } |
64 } | 75 } |
65 } init_module; | 76 } init_module; |
66 | 77 |
67 } // anonymous namespace | 78 } // anonymous namespace |
68 | 79 |
69 | 80 |
70 namespace base { | 81 namespace base { |
71 namespace internal { | 82 namespace internal { |
72 | 83 |
73 void SpinLockDelay(volatile Atomic32 *w, int32 value, int loop) { | 84 void SpinLockDelay(volatile Atomic32 *w, int32 value, int loop) { |
74 if (loop != 0) { | 85 if (loop != 0) { |
75 int save_errno = errno; | 86 int save_errno = errno; |
76 struct timespec tm; | 87 struct timespec tm; |
77 tm.tv_sec = 0; | 88 tm.tv_sec = 0; |
78 if (have_futex) { | 89 if (have_futex) { |
79 tm.tv_nsec = base::internal::SuggestedDelayNS(loop); | 90 tm.tv_nsec = base::internal::SuggestedDelayNS(loop); |
80 } else { | 91 } else { |
81 tm.tv_nsec = 2000001; // above 2ms so linux 2.4 doesn't spin | 92 tm.tv_nsec = 2000001; // above 2ms so linux 2.4 doesn't spin |
82 } | 93 } |
83 if (have_futex) { | 94 if (have_futex) { |
84 tm.tv_nsec *= 16; // increase the delay; we expect explicit wakeups | 95 tm.tv_nsec *= 16; // increase the delay; we expect explicit wakeups |
85 sys_futex(reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), | 96 syscall(__NR_futex, reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), |
86 FUTEX_WAIT | futex_private_flag, | 97 FUTEX_WAIT | futex_private_flag, |
87 value, reinterpret_cast<struct kernel_timespec *>(&tm)); | 98 value, reinterpret_cast<struct kernel_timespec *>(&tm)); |
88 } else { | 99 } else { |
89 nanosleep(&tm, NULL); | 100 nanosleep(&tm, NULL); |
90 } | 101 } |
91 errno = save_errno; | 102 errno = save_errno; |
92 } | 103 } |
93 } | 104 } |
94 | 105 |
95 void SpinLockWake(volatile Atomic32 *w, bool all) { | 106 void SpinLockWake(volatile Atomic32 *w, bool all) { |
96 if (have_futex) { | 107 if (have_futex) { |
97 sys_futex(reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), | 108 syscall(__NR_futex, reinterpret_cast<int *>(const_cast<Atomic32 *>(w)), |
98 FUTEX_WAKE | futex_private_flag, all? INT_MAX : 1, 0); | 109 FUTEX_WAKE | futex_private_flag, 1, 0); |
99 } | 110 } |
100 } | 111 } |
101 | 112 |
102 } // namespace internal | 113 } // namespace internal |
103 } // namespace base | 114 } // namespace base |
OLD | NEW |