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