Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // WARNING: You should *NOT* be using this class directly. PlatformThread is |
michael@0 | 6 | // the low-level platform-specific abstraction to the OS's threading interface. |
michael@0 | 7 | // You should instead be using a message-loop driven Thread, see thread.h. |
michael@0 | 8 | |
michael@0 | 9 | #ifndef BASE_THREADING_PLATFORM_THREAD_H_ |
michael@0 | 10 | #define BASE_THREADING_PLATFORM_THREAD_H_ |
michael@0 | 11 | |
michael@0 | 12 | #include "base/base_export.h" |
michael@0 | 13 | #include "base/basictypes.h" |
michael@0 | 14 | #include "base/time/time.h" |
michael@0 | 15 | #include "build/build_config.h" |
michael@0 | 16 | |
michael@0 | 17 | #if defined(OS_WIN) |
michael@0 | 18 | #include <windows.h> |
michael@0 | 19 | #elif defined(OS_POSIX) |
michael@0 | 20 | #include <pthread.h> |
michael@0 | 21 | #include <unistd.h> |
michael@0 | 22 | #endif |
michael@0 | 23 | |
michael@0 | 24 | namespace base { |
michael@0 | 25 | |
michael@0 | 26 | #if defined(OS_WIN) |
michael@0 | 27 | typedef DWORD PlatformThreadId; |
michael@0 | 28 | #elif defined(OS_POSIX) |
michael@0 | 29 | typedef pid_t PlatformThreadId; |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | class PlatformThreadHandle { |
michael@0 | 33 | public: |
michael@0 | 34 | #if defined(OS_WIN) |
michael@0 | 35 | typedef void* Handle; |
michael@0 | 36 | #elif defined(OS_POSIX) |
michael@0 | 37 | typedef pthread_t Handle; |
michael@0 | 38 | #endif |
michael@0 | 39 | |
michael@0 | 40 | PlatformThreadHandle() |
michael@0 | 41 | : handle_(0), |
michael@0 | 42 | id_(0) { |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | explicit PlatformThreadHandle(Handle handle) |
michael@0 | 46 | : handle_(handle), |
michael@0 | 47 | id_(0) { |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | PlatformThreadHandle(Handle handle, |
michael@0 | 51 | PlatformThreadId id) |
michael@0 | 52 | : handle_(handle), |
michael@0 | 53 | id_(id) { |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | bool is_equal(const PlatformThreadHandle& other) { |
michael@0 | 57 | return handle_ == other.handle_; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | bool is_null() { |
michael@0 | 61 | return !handle_; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | Handle platform_handle() { |
michael@0 | 65 | return handle_; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | private: |
michael@0 | 69 | friend class PlatformThread; |
michael@0 | 70 | |
michael@0 | 71 | Handle handle_; |
michael@0 | 72 | PlatformThreadId id_; |
michael@0 | 73 | }; |
michael@0 | 74 | |
michael@0 | 75 | const PlatformThreadId kInvalidThreadId(0); |
michael@0 | 76 | |
michael@0 | 77 | // Valid values for SetThreadPriority() |
michael@0 | 78 | enum ThreadPriority{ |
michael@0 | 79 | kThreadPriority_Normal, |
michael@0 | 80 | // Suitable for low-latency, glitch-resistant audio. |
michael@0 | 81 | kThreadPriority_RealtimeAudio, |
michael@0 | 82 | // Suitable for threads which generate data for the display (at ~60Hz). |
michael@0 | 83 | kThreadPriority_Display, |
michael@0 | 84 | // Suitable for threads that shouldn't disrupt high priority work. |
michael@0 | 85 | kThreadPriority_Background |
michael@0 | 86 | }; |
michael@0 | 87 | |
michael@0 | 88 | // A namespace for low-level thread functions. |
michael@0 | 89 | class BASE_EXPORT PlatformThread { |
michael@0 | 90 | public: |
michael@0 | 91 | // Implement this interface to run code on a background thread. Your |
michael@0 | 92 | // ThreadMain method will be called on the newly created thread. |
michael@0 | 93 | class BASE_EXPORT Delegate { |
michael@0 | 94 | public: |
michael@0 | 95 | virtual void ThreadMain() = 0; |
michael@0 | 96 | |
michael@0 | 97 | protected: |
michael@0 | 98 | virtual ~Delegate() {} |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | // Gets the current thread id, which may be useful for logging purposes. |
michael@0 | 102 | static PlatformThreadId CurrentId(); |
michael@0 | 103 | |
michael@0 | 104 | // Get the current handle. |
michael@0 | 105 | static PlatformThreadHandle CurrentHandle(); |
michael@0 | 106 | |
michael@0 | 107 | // Yield the current thread so another thread can be scheduled. |
michael@0 | 108 | static void YieldCurrentThread(); |
michael@0 | 109 | |
michael@0 | 110 | // Sleeps for the specified duration. |
michael@0 | 111 | static void Sleep(base::TimeDelta duration); |
michael@0 | 112 | |
michael@0 | 113 | // Sets the thread name visible to debuggers/tools. This has no effect |
michael@0 | 114 | // otherwise. This name pointer is not copied internally. Thus, it must stay |
michael@0 | 115 | // valid until the thread ends. |
michael@0 | 116 | static void SetName(const char* name); |
michael@0 | 117 | |
michael@0 | 118 | // Gets the thread name, if previously set by SetName. |
michael@0 | 119 | static const char* GetName(); |
michael@0 | 120 | |
michael@0 | 121 | // Creates a new thread. The |stack_size| parameter can be 0 to indicate |
michael@0 | 122 | // that the default stack size should be used. Upon success, |
michael@0 | 123 | // |*thread_handle| will be assigned a handle to the newly created thread, |
michael@0 | 124 | // and |delegate|'s ThreadMain method will be executed on the newly created |
michael@0 | 125 | // thread. |
michael@0 | 126 | // NOTE: When you are done with the thread handle, you must call Join to |
michael@0 | 127 | // release system resources associated with the thread. You must ensure that |
michael@0 | 128 | // the Delegate object outlives the thread. |
michael@0 | 129 | static bool Create(size_t stack_size, Delegate* delegate, |
michael@0 | 130 | PlatformThreadHandle* thread_handle); |
michael@0 | 131 | |
michael@0 | 132 | // CreateWithPriority() does the same thing as Create() except the priority of |
michael@0 | 133 | // the thread is set based on |priority|. Can be used in place of Create() |
michael@0 | 134 | // followed by SetThreadPriority(). SetThreadPriority() has not been |
michael@0 | 135 | // implemented on the Linux platform yet, this is the only way to get a high |
michael@0 | 136 | // priority thread on Linux. |
michael@0 | 137 | static bool CreateWithPriority(size_t stack_size, Delegate* delegate, |
michael@0 | 138 | PlatformThreadHandle* thread_handle, |
michael@0 | 139 | ThreadPriority priority); |
michael@0 | 140 | |
michael@0 | 141 | // CreateNonJoinable() does the same thing as Create() except the thread |
michael@0 | 142 | // cannot be Join()'d. Therefore, it also does not output a |
michael@0 | 143 | // PlatformThreadHandle. |
michael@0 | 144 | static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); |
michael@0 | 145 | |
michael@0 | 146 | // Joins with a thread created via the Create function. This function blocks |
michael@0 | 147 | // the caller until the designated thread exits. This will invalidate |
michael@0 | 148 | // |thread_handle|. |
michael@0 | 149 | static void Join(PlatformThreadHandle thread_handle); |
michael@0 | 150 | |
michael@0 | 151 | static void SetThreadPriority(PlatformThreadHandle handle, |
michael@0 | 152 | ThreadPriority priority); |
michael@0 | 153 | |
michael@0 | 154 | private: |
michael@0 | 155 | DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
michael@0 | 156 | }; |
michael@0 | 157 | |
michael@0 | 158 | } // namespace base |
michael@0 | 159 | |
michael@0 | 160 | #endif // BASE_THREADING_PLATFORM_THREAD_H_ |