michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // WARNING: You should *NOT* be using this class directly. PlatformThread is michael@0: // the low-level platform-specific abstraction to the OS's threading interface. michael@0: // You should instead be using a message-loop driven Thread, see thread.h. michael@0: michael@0: #ifndef BASE_THREADING_PLATFORM_THREAD_H_ michael@0: #define BASE_THREADING_PLATFORM_THREAD_H_ michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: #include "base/time/time.h" michael@0: #include "build/build_config.h" michael@0: michael@0: #if defined(OS_WIN) michael@0: #include michael@0: #elif defined(OS_POSIX) michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: namespace base { michael@0: michael@0: #if defined(OS_WIN) michael@0: typedef DWORD PlatformThreadId; michael@0: #elif defined(OS_POSIX) michael@0: typedef pid_t PlatformThreadId; michael@0: #endif michael@0: michael@0: class PlatformThreadHandle { michael@0: public: michael@0: #if defined(OS_WIN) michael@0: typedef void* Handle; michael@0: #elif defined(OS_POSIX) michael@0: typedef pthread_t Handle; michael@0: #endif michael@0: michael@0: PlatformThreadHandle() michael@0: : handle_(0), michael@0: id_(0) { michael@0: } michael@0: michael@0: explicit PlatformThreadHandle(Handle handle) michael@0: : handle_(handle), michael@0: id_(0) { michael@0: } michael@0: michael@0: PlatformThreadHandle(Handle handle, michael@0: PlatformThreadId id) michael@0: : handle_(handle), michael@0: id_(id) { michael@0: } michael@0: michael@0: bool is_equal(const PlatformThreadHandle& other) { michael@0: return handle_ == other.handle_; michael@0: } michael@0: michael@0: bool is_null() { michael@0: return !handle_; michael@0: } michael@0: michael@0: Handle platform_handle() { michael@0: return handle_; michael@0: } michael@0: michael@0: private: michael@0: friend class PlatformThread; michael@0: michael@0: Handle handle_; michael@0: PlatformThreadId id_; michael@0: }; michael@0: michael@0: const PlatformThreadId kInvalidThreadId(0); michael@0: michael@0: // Valid values for SetThreadPriority() michael@0: enum ThreadPriority{ michael@0: kThreadPriority_Normal, michael@0: // Suitable for low-latency, glitch-resistant audio. michael@0: kThreadPriority_RealtimeAudio, michael@0: // Suitable for threads which generate data for the display (at ~60Hz). michael@0: kThreadPriority_Display, michael@0: // Suitable for threads that shouldn't disrupt high priority work. michael@0: kThreadPriority_Background michael@0: }; michael@0: michael@0: // A namespace for low-level thread functions. michael@0: class BASE_EXPORT PlatformThread { michael@0: public: michael@0: // Implement this interface to run code on a background thread. Your michael@0: // ThreadMain method will be called on the newly created thread. michael@0: class BASE_EXPORT Delegate { michael@0: public: michael@0: virtual void ThreadMain() = 0; michael@0: michael@0: protected: michael@0: virtual ~Delegate() {} michael@0: }; michael@0: michael@0: // Gets the current thread id, which may be useful for logging purposes. michael@0: static PlatformThreadId CurrentId(); michael@0: michael@0: // Get the current handle. michael@0: static PlatformThreadHandle CurrentHandle(); michael@0: michael@0: // Yield the current thread so another thread can be scheduled. michael@0: static void YieldCurrentThread(); michael@0: michael@0: // Sleeps for the specified duration. michael@0: static void Sleep(base::TimeDelta duration); michael@0: michael@0: // Sets the thread name visible to debuggers/tools. This has no effect michael@0: // otherwise. This name pointer is not copied internally. Thus, it must stay michael@0: // valid until the thread ends. michael@0: static void SetName(const char* name); michael@0: michael@0: // Gets the thread name, if previously set by SetName. michael@0: static const char* GetName(); michael@0: michael@0: // Creates a new thread. The |stack_size| parameter can be 0 to indicate michael@0: // that the default stack size should be used. Upon success, michael@0: // |*thread_handle| will be assigned a handle to the newly created thread, michael@0: // and |delegate|'s ThreadMain method will be executed on the newly created michael@0: // thread. michael@0: // NOTE: When you are done with the thread handle, you must call Join to michael@0: // release system resources associated with the thread. You must ensure that michael@0: // the Delegate object outlives the thread. michael@0: static bool Create(size_t stack_size, Delegate* delegate, michael@0: PlatformThreadHandle* thread_handle); michael@0: michael@0: // CreateWithPriority() does the same thing as Create() except the priority of michael@0: // the thread is set based on |priority|. Can be used in place of Create() michael@0: // followed by SetThreadPriority(). SetThreadPriority() has not been michael@0: // implemented on the Linux platform yet, this is the only way to get a high michael@0: // priority thread on Linux. michael@0: static bool CreateWithPriority(size_t stack_size, Delegate* delegate, michael@0: PlatformThreadHandle* thread_handle, michael@0: ThreadPriority priority); michael@0: michael@0: // CreateNonJoinable() does the same thing as Create() except the thread michael@0: // cannot be Join()'d. Therefore, it also does not output a michael@0: // PlatformThreadHandle. michael@0: static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); michael@0: michael@0: // Joins with a thread created via the Create function. This function blocks michael@0: // the caller until the designated thread exits. This will invalidate michael@0: // |thread_handle|. michael@0: static void Join(PlatformThreadHandle thread_handle); michael@0: michael@0: static void SetThreadPriority(PlatformThreadHandle handle, michael@0: ThreadPriority priority); michael@0: michael@0: private: michael@0: DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_THREADING_PLATFORM_THREAD_H_