OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef StackUtil_h |
| 6 #define StackUtil_h |
| 7 |
| 8 #include "wtf/Compiler.h" |
| 9 #include "wtf/WTFExport.h" |
| 10 #include "wtf/build_config.h" |
| 11 #include <stddef.h> |
| 12 #include <stdint.h> |
| 13 |
| 14 namespace WTF { |
| 15 |
| 16 WTF_EXPORT size_t getUnderestimatedStackSize(); |
| 17 WTF_EXPORT void* getStackStart(); |
| 18 |
| 19 namespace internal { |
| 20 |
| 21 WTF_EXPORT uintptr_t mainThreadUnderestimatedStackSize(); |
| 22 |
| 23 #if OS(WIN) && COMPILER(MSVC) |
| 24 size_t threadStackSize(); |
| 25 #endif |
| 26 |
| 27 // 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 |
| 29 // we are on the main thread. If the function returns false, we are certainly |
| 30 // not on the main thread. Must be WTF_EXPORT due to template inlining in |
| 31 // ThreadSpecific. |
| 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; |
| 41 uintptr_t addressDiff = |
| 42 s_mainThreadStackStart - reinterpret_cast<uintptr_t>(&dummy); |
| 43 // This is a fast way to judge if we are in the main thread. |
| 44 // If |&dummy| is within |s_mainThreadUnderestimatedStackSize| byte from |
| 45 // the stack start of the main thread, we judge that we are in |
| 46 // the main thread. |
| 47 return addressDiff >= s_mainThreadUnderestimatedStackSize; |
| 48 } |
| 49 |
| 50 } // namespace internal |
| 51 |
| 52 } // namespace WTF |
| 53 |
| 54 #endif // StackUtil_h |
OLD | NEW |