| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef StackUtil_h | 5 #ifndef StackUtil_h |
| 6 #define StackUtil_h | 6 #define StackUtil_h |
| 7 | 7 |
| 8 #include "wtf/Compiler.h" | 8 #include "wtf/Compiler.h" |
| 9 #include "wtf/WTFExport.h" | 9 #include "wtf/WTFExport.h" |
| 10 #include "wtf/build_config.h" | 10 #include "wtf/build_config.h" |
| 11 #include <stddef.h> | 11 #include <stddef.h> |
| 12 #include <stdint.h> | 12 #include <stdint.h> |
| 13 | 13 |
| 14 namespace WTF { | 14 namespace WTF { |
| 15 | 15 |
| 16 WTF_EXPORT size_t getUnderestimatedStackSize(); | 16 WTF_EXPORT size_t getUnderestimatedStackSize(); |
| 17 WTF_EXPORT void* getStackStart(); | 17 WTF_EXPORT void* getStackStart(); |
| 18 | 18 |
| 19 namespace internal { | 19 namespace internal { |
| 20 | 20 |
| 21 WTF_EXPORT uintptr_t mainThreadUnderestimatedStackSize(); | 21 WTF_EXPORT extern uintptr_t s_mainThreadStackStart; |
| 22 WTF_EXPORT extern uintptr_t s_mainThreadUnderestimatedStackSize; |
| 23 |
| 24 WTF_EXPORT void initializeMainThreadStackEstimate(); |
| 22 | 25 |
| 23 #if OS(WIN) && COMPILER(MSVC) | 26 #if OS(WIN) && COMPILER(MSVC) |
| 24 size_t threadStackSize(); | 27 size_t threadStackSize(); |
| 25 #endif | 28 #endif |
| 26 | 29 |
| 30 } // namespace internal |
| 31 |
| 27 // Returns true if the function is not called on the main thread. Note carefully | 32 // Returns true if the function is not called on the main thread. Note carefully |
| 28 // that this function may have false positives, i.e. it can return true even if | 33 // that this function may have false positives, i.e. it can return true even if |
| 29 // we are on the main thread. If the function returns false, we are certainly | 34 // we are on the main thread. If the function returns false, we are certainly |
| 30 // on the main thread. Must be WTF_EXPORT due to template inlining in | 35 // on the main thread. |
| 31 // ThreadSpecific. | 36 inline bool mayNotBeMainThread() { |
| 32 inline WTF_EXPORT bool mayNotBeMainThread() { | |
| 33 // getStackStart is exclusive, not inclusive (i.e. it points past the last | |
| 34 // page of the stack in linear order). So, to ensure an inclusive comparison, | |
| 35 // subtract here and in mainThreadUnderestimatedStackSize(). | |
| 36 static uintptr_t s_mainThreadStackStart = | |
| 37 reinterpret_cast<uintptr_t>(getStackStart()) - sizeof(void*); | |
| 38 static uintptr_t s_mainThreadUnderestimatedStackSize = | |
| 39 mainThreadUnderestimatedStackSize(); | |
| 40 uintptr_t dummy; | 37 uintptr_t dummy; |
| 41 uintptr_t addressDiff = | 38 uintptr_t addressDiff = |
| 42 s_mainThreadStackStart - reinterpret_cast<uintptr_t>(&dummy); | 39 internal::s_mainThreadStackStart - reinterpret_cast<uintptr_t>(&dummy); |
| 43 // This is a fast way to judge if we are in the main thread. | 40 // This is a fast way to judge if we are in the main thread. |
| 44 // If |&dummy| is within |s_mainThreadUnderestimatedStackSize| byte from | 41 // If |&dummy| is within |s_mainThreadUnderestimatedStackSize| byte from |
| 45 // the stack start of the main thread, we judge that we are in | 42 // the stack start of the main thread, we judge that we are in |
| 46 // the main thread. | 43 // the main thread. |
| 47 return addressDiff >= s_mainThreadUnderestimatedStackSize; | 44 return addressDiff >= internal::s_mainThreadUnderestimatedStackSize; |
| 48 } | 45 } |
| 49 | 46 |
| 50 } // namespace internal | |
| 51 | |
| 52 } // namespace WTF | 47 } // namespace WTF |
| 53 | 48 |
| 54 #endif // StackUtil_h | 49 #endif // StackUtil_h |
| OLD | NEW |