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