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

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: Rebase. 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 #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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 if (result != KERN_SUCCESS) 156 if (result != KERN_SUCCESS)
155 DVLOG(1) << "thread_policy_set() failure: " << result; 157 DVLOG(1) << "thread_policy_set() failure: " << result;
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,
166 PlatformThreadId,
164 ThreadPriority priority) { 167 ThreadPriority priority) {
165 // Convert from pthread_t to mach thread identifier. 168 // Convert from pthread_t to mach thread identifier.
166 mach_port_t mach_thread_id = pthread_mach_thread_np(handle); 169 mach_port_t mach_thread_id = pthread_mach_thread_np(handle);
167 170
168 switch (priority) { 171 switch (priority) {
169 case kThreadPriority_Normal: 172 case kThreadPriority_Normal:
170 SetPriorityNormal(mach_thread_id); 173 SetPriorityNormal(mach_thread_id);
171 break; 174 break;
172 case kThreadPriority_RealtimeAudio: 175 case kThreadPriority_RealtimeAudio:
173 SetPriorityRealtimeAudio(mach_thread_id); 176 SetPriorityRealtimeAudio(mach_thread_id);
174 break; 177 break;
178 default:
179 NOTREACHED() << "Unknown priority.";
180 break;
175 } 181 }
176 } 182 }
177 183
184 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
185 #if defined(OS_IOS)
186 return 0;
187 #else
188 // The Mac OS X default for a pthread stack size is 512kB.
189 // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses
190 // DEFAULT_STACK_SIZE for this purpose.
191 //
192 // 512kB isn't quite generous enough for some deeply recursive threads that
193 // otherwise request the default stack size by specifying 0. Here, adopt
194 // glibc's behavior as on Linux, which is to use the current stack size
195 // limit (ulimit -s) as the default stack size. See
196 // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To
197 // avoid setting the limit below the Mac OS X default or the minimum usable
198 // stack size, these values are also considered. If any of these values
199 // can't be determined, or if stack size is unlimited (ulimit -s unlimited),
200 // stack_size is left at 0 to get the system default.
201 //
202 // Mac OS X normally only applies ulimit -s to the main thread stack. On
203 // contemporary OS X and Linux systems alike, this value is generally 8MB
204 // or in that neighborhood.
205 size_t default_stack_size = 0;
206 struct rlimit stack_rlimit;
207 if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 &&
208 getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 &&
209 stack_rlimit.rlim_cur != RLIM_INFINITY) {
210 default_stack_size =
211 std::max(std::max(default_stack_size,
212 static_cast<size_t>(PTHREAD_STACK_MIN)),
213 static_cast<size_t>(stack_rlimit.rlim_cur));
214 }
215 return default_stack_size;
216 #endif
217 }
218
178 } // namespace base 219 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698