Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: base/threading/platform_thread.h

Issue 12741012: base: Support setting thread priorities generically. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add id to handle. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 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. 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. 7 // You should instead be using a message-loop driven Thread, see thread.h.
8 8
9 #ifndef BASE_THREADING_PLATFORM_THREAD_H_ 9 #ifndef BASE_THREADING_PLATFORM_THREAD_H_
10 #define BASE_THREADING_PLATFORM_THREAD_H_ 10 #define BASE_THREADING_PLATFORM_THREAD_H_
11 11
12 #include "base/base_export.h" 12 #include "base/base_export.h"
13 #include "base/basictypes.h" 13 #include "base/basictypes.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 16
17 #if defined(OS_WIN) 17 #if defined(OS_WIN)
18 #include <windows.h> 18 #include <windows.h>
19 #elif defined(OS_POSIX) 19 #elif defined(OS_POSIX)
20 #include <pthread.h> 20 #include <pthread.h>
21 #include <unistd.h> 21 #include <unistd.h>
22 #include <ostream>
jar (doing other things) 2013/05/15 02:18:36 nit: probably alphabetize by placing before line 2
epennerAtGoogle 2013/05/15 05:09:08 Done. I actually removed this.
epenner 2013/05/17 19:18:13 I went ahead and removed this.
22 #endif 23 #endif
23 24
24 namespace base { 25 namespace base {
25 26
26 // PlatformThreadHandle should not be assumed to be a numeric type, since the 27 // PlatformThreadHandle should not be assumed to be a numeric type, since the
27 // standard intends to allow pthread_t to be a structure. This means you 28 // standard intends to allow pthread_t to be a structure. This means you
28 // should not initialize it to a value, like 0. If it's a member variable, the 29 // should not initialize it to a value, like 0. If it's a member variable, the
29 // constructor can safely "value initialize" using () in the initializer list. 30 // constructor can safely "value initialize" using () in the initializer list.
30 #if defined(OS_WIN) 31 #if defined(OS_WIN)
31 typedef DWORD PlatformThreadId; 32 typedef DWORD PlatformThreadId;
32 typedef void* PlatformThreadHandle; // HANDLE 33 typedef void* PlatformThreadHandle; // HANDLE
33 const PlatformThreadHandle kNullThreadHandle = NULL; 34 const PlatformThreadHandle kNullThreadHandle = NULL;
34 #elif defined(OS_POSIX) 35 #elif defined(OS_POSIX)
35 typedef pthread_t PlatformThreadHandle;
36 const PlatformThreadHandle kNullThreadHandle = 0;
37 typedef pid_t PlatformThreadId; 36 typedef pid_t PlatformThreadId;
37 struct PlatformThreadHandle {
jar (doing other things) 2013/05/15 02:18:36 nit: this should probably be a class. You'll prob
epennerAtGoogle 2013/05/15 05:09:08 I made it a class to protect members, but default
epenner 2013/05/17 19:18:13 I made it into a class to protect private members,
38 PlatformThreadHandle()
39 : handle(),
40 id(0) {
41 }
42
43 PlatformThreadHandle(pthread_t handle)
jar (doing other things) 2013/05/15 02:18:36 nit: explicit.
epennerAtGoogle 2013/05/15 05:09:08 Done.
epenner 2013/05/17 19:18:13 Done.
44 : handle(handle),
45 id(0) {
46 }
47
48 PlatformThreadHandle(pthread_t handle,
49 PlatformThreadId id)
50 : handle(handle),
51 id(id) {
52 }
53
54 operator bool() const {
55 return !!handle;
56 }
57
58 bool operator!() {
jar (doing other things) 2013/05/15 02:18:36 Instead of operator overload, use method names, li
epennerAtGoogle 2013/05/15 05:09:08 I couldn't do this because the handle on windows w
59 return !handle;
60 }
61
62 bool operator==(const PlatformThreadHandle& other) {
jar (doing other things) 2013/05/15 02:18:36 nit: again, just define a method name.
epennerAtGoogle 2013/05/15 05:09:08 Done.
63 return handle == other.handle;
64 }
65
66 bool operator!=(const PlatformThreadHandle& other) {
67 return handle != other.handle;
68 }
69
70 pthread_t handle;
71 PlatformThreadId id;
72 };
73
74 const PlatformThreadHandle kNullThreadHandle(0);
75 std::ostream& operator<< (std::ostream &out,
jar (doing other things) 2013/05/15 02:18:36 This is pretty uncommon, as use of streams is not
epennerAtGoogle 2013/05/15 05:09:08 Yes, I'm trying to remove it by changing a few cal
76 const PlatformThreadHandle& handle);
77
38 #endif 78 #endif
39 79
40 const PlatformThreadId kInvalidThreadId = 0; 80 const PlatformThreadId kInvalidThreadId = 0;
41 81
42 // Valid values for SetThreadPriority() 82 // Valid values for SetThreadPriority()
43 enum ThreadPriority{ 83 enum ThreadPriority{
44 kThreadPriority_Normal, 84 kThreadPriority_Normal,
45 // Suitable for low-latency, glitch-resistant audio. 85 // Suitable for low-latency, glitch-resistant audio.
46 kThreadPriority_RealtimeAudio 86 kThreadPriority_RealtimeAudio,
87 // Suitable for threads which generate data for the display (at ~60Hz).
88 kThreadPriority_Display,
89 // Suitable for threads that shouldn't disrupt high priority work.
90 kThreadPriority_Background
47 }; 91 };
48 92
49 // A namespace for low-level thread functions. 93 // A namespace for low-level thread functions.
50 class BASE_EXPORT PlatformThread { 94 class BASE_EXPORT PlatformThread {
51 public: 95 public:
52 // Implement this interface to run code on a background thread. Your 96 // Implement this interface to run code on a background thread. Your
53 // ThreadMain method will be called on the newly created thread. 97 // ThreadMain method will be called on the newly created thread.
54 class BASE_EXPORT Delegate { 98 class BASE_EXPORT Delegate {
55 public: 99 public:
56 virtual void ThreadMain() = 0; 100 virtual void ThreadMain() = 0;
57 101
58 protected: 102 protected:
59 virtual ~Delegate() {} 103 virtual ~Delegate() {}
60 }; 104 };
61 105
62 // Gets the current thread id, which may be useful for logging purposes. 106 // Gets the current thread id, which may be useful for logging purposes.
63 static PlatformThreadId CurrentId(); 107 static PlatformThreadId CurrentId();
64 108
109 // Get the current handle.
110 static PlatformThreadHandle CurrentHandle();
111
65 // Yield the current thread so another thread can be scheduled. 112 // Yield the current thread so another thread can be scheduled.
66 static void YieldCurrentThread(); 113 static void YieldCurrentThread();
67 114
68 // Sleeps for the specified duration. 115 // Sleeps for the specified duration.
69 static void Sleep(base::TimeDelta duration); 116 static void Sleep(base::TimeDelta duration);
70 117
71 // Sets the thread name visible to debuggers/tools. This has no effect 118 // Sets the thread name visible to debuggers/tools. This has no effect
72 // otherwise. This name pointer is not copied internally. Thus, it must stay 119 // otherwise. This name pointer is not copied internally. Thus, it must stay
73 // valid until the thread ends. 120 // valid until the thread ends.
74 static void SetName(const char* name); 121 static void SetName(const char* name);
(...skipping 24 matching lines...) Expand all
99 // CreateNonJoinable() does the same thing as Create() except the thread 146 // CreateNonJoinable() does the same thing as Create() except the thread
100 // cannot be Join()'d. Therefore, it also does not output a 147 // cannot be Join()'d. Therefore, it also does not output a
101 // PlatformThreadHandle. 148 // PlatformThreadHandle.
102 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); 149 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
103 150
104 // Joins with a thread created via the Create function. This function blocks 151 // Joins with a thread created via the Create function. This function blocks
105 // the caller until the designated thread exits. This will invalidate 152 // the caller until the designated thread exits. This will invalidate
106 // |thread_handle|. 153 // |thread_handle|.
107 static void Join(PlatformThreadHandle thread_handle); 154 static void Join(PlatformThreadHandle thread_handle);
108 155
109 // Sets the priority of the thread specified in |handle| to |priority|.
110 // This does not work on Linux, use CreateWithPriority() instead.
111 static void SetThreadPriority(PlatformThreadHandle handle, 156 static void SetThreadPriority(PlatformThreadHandle handle,
112 ThreadPriority priority); 157 ThreadPriority priority);
113 158
114 private: 159 private:
115 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); 160 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
116 }; 161 };
117 162
118 } // namespace base 163 } // namespace base
119 164
120 #endif // BASE_THREADING_PLATFORM_THREAD_H_ 165 #endif // BASE_THREADING_PLATFORM_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698