Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 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_PLATFORM_THREAD_H_ |
michael@0 | 10 | #define BASE_PLATFORM_THREAD_H_ |
michael@0 | 11 | |
michael@0 | 12 | #include "base/basictypes.h" |
michael@0 | 13 | |
michael@0 | 14 | // PlatformThreadHandle should not be assumed to be a numeric type, since the |
michael@0 | 15 | // standard intends to allow pthread_t to be a structure. This means you |
michael@0 | 16 | // should not initialize it to a value, like 0. If it's a member variable, the |
michael@0 | 17 | // constructor can safely "value initialize" using () in the initializer list. |
michael@0 | 18 | #if defined(OS_WIN) |
michael@0 | 19 | #include <windows.h> |
michael@0 | 20 | typedef DWORD PlatformThreadId; |
michael@0 | 21 | typedef void* PlatformThreadHandle; // HANDLE |
michael@0 | 22 | #elif defined(OS_POSIX) |
michael@0 | 23 | #include <pthread.h> |
michael@0 | 24 | typedef pthread_t PlatformThreadHandle; |
michael@0 | 25 | #if defined(OS_LINUX) || defined(OS_OPENBSD) || defined(__GLIBC__) |
michael@0 | 26 | #include <unistd.h> |
michael@0 | 27 | typedef pid_t PlatformThreadId; |
michael@0 | 28 | #elif defined(OS_BSD) |
michael@0 | 29 | #include <sys/types.h> |
michael@0 | 30 | typedef lwpid_t PlatformThreadId; |
michael@0 | 31 | #elif defined(OS_MACOSX) |
michael@0 | 32 | #include <mach/mach.h> |
michael@0 | 33 | typedef mach_port_t PlatformThreadId; |
michael@0 | 34 | #endif |
michael@0 | 35 | #endif |
michael@0 | 36 | |
michael@0 | 37 | // A namespace for low-level thread functions. |
michael@0 | 38 | class PlatformThread { |
michael@0 | 39 | public: |
michael@0 | 40 | // Gets the current thread id, which may be useful for logging purposes. |
michael@0 | 41 | static PlatformThreadId CurrentId(); |
michael@0 | 42 | |
michael@0 | 43 | // Yield the current thread so another thread can be scheduled. |
michael@0 | 44 | static void YieldCurrentThread(); |
michael@0 | 45 | |
michael@0 | 46 | // Sleeps for the specified duration (units are milliseconds). |
michael@0 | 47 | static void Sleep(int duration_ms); |
michael@0 | 48 | |
michael@0 | 49 | // Sets the thread name visible to a debugger. This has no effect otherwise. |
michael@0 | 50 | static void SetName(const char* name); |
michael@0 | 51 | |
michael@0 | 52 | // Implement this interface to run code on a background thread. Your |
michael@0 | 53 | // ThreadMain method will be called on the newly created thread. |
michael@0 | 54 | class Delegate { |
michael@0 | 55 | public: |
michael@0 | 56 | virtual ~Delegate() {} |
michael@0 | 57 | virtual void ThreadMain() = 0; |
michael@0 | 58 | }; |
michael@0 | 59 | |
michael@0 | 60 | // Creates a new thread. The |stack_size| parameter can be 0 to indicate |
michael@0 | 61 | // that the default stack size should be used. Upon success, |
michael@0 | 62 | // |*thread_handle| will be assigned a handle to the newly created thread, |
michael@0 | 63 | // and |delegate|'s ThreadMain method will be executed on the newly created |
michael@0 | 64 | // thread. |
michael@0 | 65 | // NOTE: When you are done with the thread handle, you must call Join to |
michael@0 | 66 | // release system resources associated with the thread. You must ensure that |
michael@0 | 67 | // the Delegate object outlives the thread. |
michael@0 | 68 | static bool Create(size_t stack_size, Delegate* delegate, |
michael@0 | 69 | PlatformThreadHandle* thread_handle); |
michael@0 | 70 | |
michael@0 | 71 | // CreateNonJoinable() does the same thing as Create() except the thread |
michael@0 | 72 | // cannot be Join()'d. Therefore, it also does not output a |
michael@0 | 73 | // PlatformThreadHandle. |
michael@0 | 74 | static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); |
michael@0 | 75 | |
michael@0 | 76 | // Joins with a thread created via the Create function. This function blocks |
michael@0 | 77 | // the caller until the designated thread exits. This will invalidate |
michael@0 | 78 | // |thread_handle|. |
michael@0 | 79 | static void Join(PlatformThreadHandle thread_handle); |
michael@0 | 80 | |
michael@0 | 81 | private: |
michael@0 | 82 | DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | #endif // BASE_PLATFORM_THREAD_H_ |