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