OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/threading/platform_thread.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <sched.h> |
| 9 |
| 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/safe_strerror_posix.h" |
| 14 #include "base/threading/thread_id_name_manager.h" |
| 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/tracked_objects.h" |
| 17 |
| 18 #if defined(OS_LINUX) |
| 19 #include <sys/prctl.h> |
| 20 #include <sys/resource.h> |
| 21 #include <sys/syscall.h> |
| 22 #include <sys/time.h> |
| 23 #include <unistd.h> |
| 24 #endif |
| 25 |
| 26 namespace base { |
| 27 |
| 28 namespace { |
| 29 int ThreadNiceValue(ThreadPriority priority) { |
| 30 static const int threadPriorityAudio = -10; |
| 31 static const int threadPriorityBackground = 10; |
| 32 static const int threadPriorityDefault = 0; |
| 33 static const int threadPriorityDisplay = -6; |
| 34 switch (priority) { |
| 35 case kThreadPriority_RealtimeAudio: |
| 36 return threadPriorityAudio; |
| 37 case kThreadPriority_Background: |
| 38 return threadPriorityBackground; |
| 39 case kThreadPriority_Normal: |
| 40 return threadPriorityDefault; |
| 41 case kThreadPriority_Display: |
| 42 return threadPriorityDisplay; |
| 43 default: |
| 44 NOTREACHED() << "Unknown priority."; |
| 45 return 0; |
| 46 } |
| 47 } |
| 48 } // namespace |
| 49 |
| 50 // static |
| 51 void PlatformThread::SetName(const char* name) { |
| 52 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
| 53 tracked_objects::ThreadData::InitializeThreadContext(name); |
| 54 |
| 55 #ifndef OS_NACL |
| 56 // On linux we can get the thread names to show up in the debugger by setting |
| 57 // the process name for the LWP. We don't want to do this for the main |
| 58 // thread because that would rename the process, causing tools like killall |
| 59 // to stop working. |
| 60 if (PlatformThread::CurrentId() == getpid()) |
| 61 return; |
| 62 |
| 63 // http://0pointer.de/blog/projects/name-your-threads.html |
| 64 // Set the name for the LWP (which gets truncated to 15 characters). |
| 65 // Note that glibc also has a 'pthread_setname_np' api, but it may not be |
| 66 // available everywhere and it's only benefit over using prctl directly is |
| 67 // that it can set the name of threads other than the current thread. |
| 68 int err = prctl(PR_SET_NAME, name); |
| 69 // We expect EPERM failures in sandboxed processes, just ignore those. |
| 70 if (err < 0 && errno != EPERM) |
| 71 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; |
| 72 #endif |
| 73 } |
| 74 |
| 75 // static |
| 76 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
| 77 ThreadPriority priority) { |
| 78 #ifndef OS_NACL |
| 79 // setpriority(2) will set a thread's priority if it is passed a tid as |
| 80 // the 'process identifier', not affecting the rest of the threads in the |
| 81 // process. Setting this priority will only succeed if the user has been |
| 82 // granted permission to adjust nice values on the system. |
| 83 DCHECK_NE(handle.id_, kInvalidThreadId); |
| 84 int kNiceSetting = ThreadNiceValue(priority); |
| 85 if (setpriority(PRIO_PROCESS, handle.id_, kNiceSetting)) |
| 86 LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting; |
| 87 #endif |
| 88 } |
| 89 |
| 90 void InitThreading() { |
| 91 } |
| 92 |
| 93 void InitOnThread() { |
| 94 } |
| 95 |
| 96 void TerminateOnThread() { |
| 97 } |
| 98 |
| 99 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { |
| 100 return 0; |
| 101 } |
| 102 |
| 103 } // namespace base |
OLD | NEW |