OLD | NEW |
1 // Copyright (c) 2010 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 #include "base/threading/simple_thread.h" | 5 #include "base/threading/simple_thread.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 SimpleThread::SimpleThread(const std::string& name_prefix) | 14 SimpleThread::SimpleThread(const std::string& name_prefix) |
15 : name_prefix_(name_prefix), name_(name_prefix), | 15 : name_prefix_(name_prefix), |
16 thread_(), event_(true, false), tid_(0), joined_(false) { | 16 name_(name_prefix), |
17 } | 17 thread_(), |
| 18 event_(true, false), |
| 19 tid_(0), |
| 20 joined_(false), |
| 21 priority_(options_.priority()) {} |
18 | 22 |
19 SimpleThread::SimpleThread(const std::string& name_prefix, | 23 SimpleThread::SimpleThread(const std::string& name_prefix, |
20 const Options& options) | 24 const Options& options) |
21 : name_prefix_(name_prefix), name_(name_prefix), options_(options), | 25 : name_prefix_(name_prefix), |
22 thread_(), event_(true, false), tid_(0), joined_(false) { | 26 name_(name_prefix), |
23 } | 27 options_(options), |
| 28 thread_(), |
| 29 event_(true, false), |
| 30 tid_(0), |
| 31 joined_(false), |
| 32 priority_(options_.priority()) {} |
24 | 33 |
25 SimpleThread::~SimpleThread() { | 34 SimpleThread::~SimpleThread() { |
26 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; | 35 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; |
27 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; | 36 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; |
28 } | 37 } |
29 | 38 |
30 void SimpleThread::Start() { | 39 void SimpleThread::Start() { |
31 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; | 40 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; |
32 bool success; | 41 bool success; |
33 if (options_.priority() == ThreadPriority::NORMAL) { | 42 if (options_.priority() == ThreadPriority::NORMAL) { |
(...skipping 25 matching lines...) Expand all Loading... |
59 name_.push_back('/'); | 68 name_.push_back('/'); |
60 name_.append(IntToString(tid_)); | 69 name_.append(IntToString(tid_)); |
61 PlatformThread::SetName(name_); | 70 PlatformThread::SetName(name_); |
62 | 71 |
63 // We've initialized our new thread, signal that we're done to Start(). | 72 // We've initialized our new thread, signal that we're done to Start(). |
64 event_.Signal(); | 73 event_.Signal(); |
65 | 74 |
66 Run(); | 75 Run(); |
67 } | 76 } |
68 | 77 |
| 78 bool SimpleThread::SetThreadPriority(ThreadPriority priority) { |
| 79 DCHECK(HasBeenStarted()) |
| 80 << "Tried to set a priority of never-started thread."; |
| 81 |
| 82 if (PlatformThread::SetChildThreadPriorityInCurrentProcess(thread_, tid_, |
| 83 priority)) { |
| 84 priority_ = priority; |
| 85 return true; |
| 86 } |
| 87 return false; |
| 88 } |
| 89 |
| 90 // Must have default priority either equal to higher or lower priority. |
| 91 DynamicPriorityThread::DynamicPriorityThread(const PrioritySet& priorities, |
| 92 const std::string& name_prefix, |
| 93 const Options& options) |
| 94 : SimpleThread(name_prefix, options), |
| 95 priorities_(priorities), |
| 96 dynamic_priorities_(false), |
| 97 priority_changed_(false) { |
| 98 CHECK(priorities_.higher_priority == priorities_.default_priority || |
| 99 priorities_.lower_priority == priorities_.default_priority); |
| 100 dynamic_priorities_ = |
| 101 priorities_.higher_priority != priorities_.lower_priority; |
| 102 } |
| 103 |
| 104 bool DynamicPriorityThread::Speedup() { |
| 105 if (dynamic_priorities_ && |
| 106 (GetThreadPriority() != priorities_.higher_priority)) { |
| 107 SetThreadPriority(priorities_.higher_priority); |
| 108 priority_changed_ = true; |
| 109 return true; |
| 110 } |
| 111 return false; |
| 112 } |
| 113 |
| 114 bool DynamicPriorityThread::Slowdown() { |
| 115 if (dynamic_priorities_ && |
| 116 (GetThreadPriority() != priorities_.lower_priority)) { |
| 117 SetThreadPriority(priorities_.lower_priority); |
| 118 priority_changed_ = true; |
| 119 return true; |
| 120 } |
| 121 return false; |
| 122 } |
| 123 |
| 124 bool DynamicPriorityThread::RestoreDefaultPriority() { |
| 125 if (dynamic_priorities_ && priority_changed_) { |
| 126 priority_changed_ = false; |
| 127 SetThreadPriority(priorities_.default_priority); |
| 128 // LOG(ERROR) << "\nPRAS:: [" << std::hex << this << "] " |
| 129 // << GetPrioritySetForDebugging() << ". Restored to default."; |
| 130 return true; |
| 131 } |
| 132 return false; |
| 133 } |
| 134 |
69 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, | 135 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, |
70 const std::string& name_prefix) | 136 const std::string& name_prefix) |
71 : SimpleThread(name_prefix), | 137 : SimpleThread(name_prefix), |
72 delegate_(delegate) { | 138 delegate_(delegate) { |
73 } | 139 } |
74 | 140 |
75 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, | 141 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, |
76 const std::string& name_prefix, | 142 const std::string& name_prefix, |
77 const Options& options) | 143 const Options& options) |
78 : SimpleThread(name_prefix, options), | 144 : SimpleThread(name_prefix, options), |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 222 |
157 // A NULL delegate pointer signals us to quit. | 223 // A NULL delegate pointer signals us to quit. |
158 if (!work) | 224 if (!work) |
159 break; | 225 break; |
160 | 226 |
161 work->Run(); | 227 work->Run(); |
162 } | 228 } |
163 } | 229 } |
164 | 230 |
165 } // namespace base | 231 } // namespace base |
OLD | NEW |