- add sources.
[platform/framework/web/crosswalk.git] / src / base / threading / platform_thread_android.cc
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 <sys/resource.h>
9
10 #include "base/android/jni_android.h"
11 #include "base/android/thread_utils.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/threading/thread_id_name_manager.h"
15 #include "base/tracked_objects.h"
16 #include "jni/ThreadUtils_jni.h"
17
18 namespace base {
19
20 namespace {
21 int ThreadNiceValue(ThreadPriority priority) {
22   // These nice values are taken from Android, which uses nice
23   // values like linux, but defines some preset nice values.
24   //   Process.THREAD_PRIORITY_AUDIO = -16
25   //   Process.THREAD_PRIORITY_BACKGROUND = 10
26   //   Process.THREAD_PRIORITY_DEFAULT = 0;
27   //   Process.THREAD_PRIORITY_DISPLAY = -4;
28   //   Process.THREAD_PRIORITY_FOREGROUND = -2;
29   //   Process.THREAD_PRIORITY_LESS_FAVORABLE = 1;
30   //   Process.THREAD_PRIORITY_LOWEST = 19;
31   //   Process.THREAD_PRIORITY_MORE_FAVORABLE = -1;
32   //   Process.THREAD_PRIORITY_URGENT_AUDIO = -19;
33   //   Process.THREAD_PRIORITY_URGENT_DISPLAY = -8;
34   // We use -6 for display, but we may want to split this
35   // into urgent (-8) and non-urgent (-4).
36   static const int threadPriorityAudio = -16;
37   static const int threadPriorityBackground = 10;
38   static const int threadPriorityDefault = 0;
39   static const int threadPriorityDisplay = -6;
40   switch (priority) {
41     case kThreadPriority_RealtimeAudio:
42       return threadPriorityAudio;
43     case kThreadPriority_Background:
44       return threadPriorityBackground;
45     case kThreadPriority_Normal:
46       return threadPriorityDefault;
47     case kThreadPriority_Display:
48       return threadPriorityDisplay;
49     default:
50       NOTREACHED() << "Unknown priority.";
51       return 0;
52   }
53 }
54 } // namespace
55
56 //static
57 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
58                                        ThreadPriority priority) {
59   // On Android, we set the Audio priority through JNI as Audio priority
60   // will also allow the process to run while it is backgrounded.
61   if (priority == kThreadPriority_RealtimeAudio) {
62     JNIEnv* env = base::android::AttachCurrentThread();
63     Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
64     return;
65   }
66
67   // setpriority(2) will set a thread's priority if it is passed a tid as
68   // the 'process identifier', not affecting the rest of the threads in the
69   // process. Setting this priority will only succeed if the user has been
70   // granted permission to adjust nice values on the system.
71   DCHECK_NE(handle.id_, kInvalidThreadId);
72   int kNiceSetting = ThreadNiceValue(priority);
73   if (setpriority(PRIO_PROCESS, handle.id_, kNiceSetting))
74     LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting;
75 }
76
77 void PlatformThread::SetName(const char* name) {
78   ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
79   tracked_objects::ThreadData::InitializeThreadContext(name);
80 }
81
82
83 void InitThreading() {
84 }
85
86 void InitOnThread() {
87   // Threads on linux/android may inherit their priority from the thread
88   // where they were created. This sets all new threads to the default.
89   PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(),
90                                     kThreadPriority_Normal);
91 }
92
93 void TerminateOnThread() {
94   base::android::DetachFromVM();
95 }
96
97 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
98   return 0;
99 }
100
101 bool RegisterThreadUtils(JNIEnv* env) {
102   return RegisterNativesImpl(env);
103 }
104
105 }  // namespace base