michael@0: // Copyright (c) 2006-2008 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_PLATFORM_THREAD_H_ michael@0: #define BASE_PLATFORM_THREAD_H_ michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: // PlatformThreadHandle should not be assumed to be a numeric type, since the michael@0: // standard intends to allow pthread_t to be a structure. This means you michael@0: // should not initialize it to a value, like 0. If it's a member variable, the michael@0: // constructor can safely "value initialize" using () in the initializer list. michael@0: #if defined(OS_WIN) michael@0: #include michael@0: typedef DWORD PlatformThreadId; michael@0: typedef void* PlatformThreadHandle; // HANDLE michael@0: #elif defined(OS_POSIX) michael@0: #include michael@0: typedef pthread_t PlatformThreadHandle; michael@0: #if defined(OS_LINUX) || defined(OS_OPENBSD) || defined(__GLIBC__) michael@0: #include michael@0: typedef pid_t PlatformThreadId; michael@0: #elif defined(OS_BSD) michael@0: #include michael@0: typedef lwpid_t PlatformThreadId; michael@0: #elif defined(OS_MACOSX) michael@0: #include michael@0: typedef mach_port_t PlatformThreadId; michael@0: #endif michael@0: #endif michael@0: michael@0: // A namespace for low-level thread functions. michael@0: class PlatformThread { michael@0: public: michael@0: // Gets the current thread id, which may be useful for logging purposes. michael@0: static PlatformThreadId CurrentId(); 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 (units are milliseconds). michael@0: static void Sleep(int duration_ms); michael@0: michael@0: // Sets the thread name visible to a debugger. This has no effect otherwise. michael@0: static void SetName(const char* name); michael@0: 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 Delegate { michael@0: public: michael@0: virtual ~Delegate() {} michael@0: virtual void ThreadMain() = 0; michael@0: }; 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: // 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: private: michael@0: DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); michael@0: }; michael@0: michael@0: #endif // BASE_PLATFORM_THREAD_H_