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