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