1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // WARNING: You should *NOT* be using this class directly. PlatformThread is 6 // the low-level platform-specific abstraction to the OS's threading interface. 7 // You should instead be using a message-loop driven Thread, see thread.h. 8 9 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_THREADING_PLATFORM_THREAD_H_ 10 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_THREADING_PLATFORM_THREAD_H_ 11 12 #include <stddef.h> 13 14 #include <iosfwd> 15 16 #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h" 17 #include "base/allocator/partition_allocator/partition_alloc_base/threading/platform_thread_ref.h" 18 #include "base/allocator/partition_allocator/partition_alloc_base/time/time.h" 19 #include "build/build_config.h" 20 21 #if BUILDFLAG(IS_WIN) 22 #include "base/allocator/partition_allocator/partition_alloc_base/win/windows_types.h" 23 #elif BUILDFLAG(IS_FUCHSIA) 24 #include <zircon/types.h> 25 #elif BUILDFLAG(IS_APPLE) 26 #include <mach/mach_types.h> 27 #elif BUILDFLAG(IS_POSIX) 28 #include <pthread.h> 29 #include <unistd.h> 30 #endif 31 32 namespace partition_alloc::internal::base { 33 34 // Used for logging. Always an integer value. 35 #if BUILDFLAG(IS_WIN) 36 typedef DWORD PlatformThreadId; 37 #elif BUILDFLAG(IS_FUCHSIA) 38 typedef zx_handle_t PlatformThreadId; 39 #elif BUILDFLAG(IS_APPLE) 40 typedef mach_port_t PlatformThreadId; 41 #elif BUILDFLAG(IS_POSIX) 42 typedef pid_t PlatformThreadId; 43 #endif 44 45 // Used to operate on threads. 46 class PlatformThreadHandle { 47 public: 48 #if BUILDFLAG(IS_WIN) 49 typedef void* Handle; 50 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 51 typedef pthread_t Handle; 52 #endif 53 PlatformThreadHandle()54 constexpr PlatformThreadHandle() : handle_(0) {} 55 PlatformThreadHandle(Handle handle)56 explicit constexpr PlatformThreadHandle(Handle handle) : handle_(handle) {} 57 is_equal(const PlatformThreadHandle & other)58 bool is_equal(const PlatformThreadHandle& other) const { 59 return handle_ == other.handle_; 60 } 61 is_null()62 bool is_null() const { return !handle_; } 63 platform_handle()64 Handle platform_handle() const { return handle_; } 65 66 private: 67 Handle handle_; 68 }; 69 70 const PlatformThreadId kInvalidThreadId(0); 71 72 typedef void (*SetThreadNameProc)(const std::string&); 73 74 // A namespace for low-level thread functions. PA_COMPONENT_EXPORT(PARTITION_ALLOC)75class PA_COMPONENT_EXPORT(PARTITION_ALLOC) PlatformThread { 76 public: 77 PlatformThread() = delete; 78 PlatformThread(const PlatformThread&) = delete; 79 PlatformThread& operator=(const PlatformThread&) = delete; 80 81 // Gets the current thread id, which may be useful for logging purposes. 82 static PlatformThreadId CurrentId(); 83 84 // Gets the current thread reference, which can be used to check if 85 // we're on the right thread quickly. 86 static PlatformThreadRef CurrentRef(); 87 88 // Get the handle representing the current thread. On Windows, this is a 89 // pseudo handle constant which will always represent the thread using it and 90 // hence should not be shared with other threads nor be used to differentiate 91 // the current thread from another. 92 static PlatformThreadHandle CurrentHandle(); 93 94 // Sleeps for the specified duration (real-time; ignores time overrides). 95 // Note: The sleep duration may be in base::Time or base::TimeTicks, depending 96 // on platform. If you're looking to use this in unit tests testing delayed 97 // tasks, this will be unreliable - instead, use 98 // base::test::TaskEnvironment with MOCK_TIME mode. 99 static void Sleep(TimeDelta duration); 100 101 // Sets the thread name visible to debuggers/tools. This will try to 102 // initialize the context for current thread unless it's a WorkerThread. 103 static void SetName(const std::string& name); 104 105 static void SetThreadNameHook(SetThreadNameProc hook); 106 }; 107 108 } // namespace partition_alloc::internal::base 109 110 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_THREADING_PLATFORM_THREAD_H_ 111