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

Side by Side Diff: base/threading/platform_thread_mac.mm

Issue 12741012: base: Support setting thread priorities generically. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove static initializers. 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
« no previous file with comments | « base/threading/platform_thread_linux.cc ('k') | base/threading/platform_thread_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/threading/platform_thread.h" 5 #include "base/threading/platform_thread.h"
6 6
7 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 #include <algorithm>
8 #include <dlfcn.h> 9 #include <dlfcn.h>
9 #include <mach/mach.h> 10 #include <mach/mach.h>
10 #include <mach/mach_time.h> 11 #include <mach/mach_time.h>
11 #include <mach/thread_policy.h> 12 #include <mach/thread_policy.h>
13 #include <sys/resource.h>
12 14
13 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
14 #include "base/logging.h" 16 #include "base/logging.h"
15 #include "base/threading/thread_id_name_manager.h" 17 #include "base/threading/thread_id_name_manager.h"
16 #include "base/tracked_objects.h" 18 #include "base/tracked_objects.h"
17 19
18 namespace base { 20 namespace base {
19 21
20 // If Cocoa is to be used on more than one thread, it must know that the 22 // If Cocoa is to be used on more than one thread, it must know that the
21 // application is multithreaded. Since it's possible to enter Cocoa code 23 // application is multithreaded. Since it's possible to enter Cocoa code
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 158
157 return; 159 return;
158 } 160 }
159 161
160 } // anonymous namespace 162 } // anonymous namespace
161 163
162 // static 164 // static
163 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, 165 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
164 ThreadPriority priority) { 166 ThreadPriority priority) {
165 // Convert from pthread_t to mach thread identifier. 167 // Convert from pthread_t to mach thread identifier.
166 mach_port_t mach_thread_id = pthread_mach_thread_np(handle); 168 mach_port_t mach_thread_id = pthread_mach_thread_np(handle.handle_);
167 169
168 switch (priority) { 170 switch (priority) {
169 case kThreadPriority_Normal: 171 case kThreadPriority_Normal:
170 SetPriorityNormal(mach_thread_id); 172 SetPriorityNormal(mach_thread_id);
171 break; 173 break;
172 case kThreadPriority_RealtimeAudio: 174 case kThreadPriority_RealtimeAudio:
173 SetPriorityRealtimeAudio(mach_thread_id); 175 SetPriorityRealtimeAudio(mach_thread_id);
174 break; 176 break;
177 default:
178 NOTREACHED() << "Unknown priority.";
179 break;
175 } 180 }
176 } 181 }
177 182
183 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
184 #if defined(OS_IOS)
185 return 0;
186 #else
187 // The Mac OS X default for a pthread stack size is 512kB.
188 // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses
189 // DEFAULT_STACK_SIZE for this purpose.
190 //
191 // 512kB isn't quite generous enough for some deeply recursive threads that
192 // otherwise request the default stack size by specifying 0. Here, adopt
193 // glibc's behavior as on Linux, which is to use the current stack size
194 // limit (ulimit -s) as the default stack size. See
195 // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To
196 // avoid setting the limit below the Mac OS X default or the minimum usable
197 // stack size, these values are also considered. If any of these values
198 // can't be determined, or if stack size is unlimited (ulimit -s unlimited),
199 // stack_size is left at 0 to get the system default.
200 //
201 // Mac OS X normally only applies ulimit -s to the main thread stack. On
202 // contemporary OS X and Linux systems alike, this value is generally 8MB
203 // or in that neighborhood.
204 size_t default_stack_size = 0;
205 struct rlimit stack_rlimit;
206 if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 &&
207 getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 &&
208 stack_rlimit.rlim_cur != RLIM_INFINITY) {
209 default_stack_size =
210 std::max(std::max(default_stack_size,
211 static_cast<size_t>(PTHREAD_STACK_MIN)),
212 static_cast<size_t>(stack_rlimit.rlim_cur));
213 }
214 return default_stack_size;
215 #endif
216 }
217
218 void InitOnThread() {
219 }
220
221 void TerminateOnThread() {
222 }
223
178 } // namespace base 224 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/platform_thread_linux.cc ('k') | base/threading/platform_thread_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698