2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef _LIBS_UTILS_ANDROID_THREADS_H
18 #define _LIBS_UTILS_ANDROID_THREADS_H
21 #include <sys/types.h>
23 #if defined(HAVE_PTHREADS)
27 #include <utils/ThreadDefs.h>
29 // ---------------------------------------------------------------------------
36 // Create and run a new thread.
37 extern int androidCreateThread(android_thread_func_t, void *);
39 // Create thread with lots of parameters
40 extern int androidCreateThreadEtc(android_thread_func_t entryFunction,
42 const char* threadName,
43 int32_t threadPriority,
44 size_t threadStackSize,
45 android_thread_id_t *threadId);
47 // Get some sort of unique identifier for the current thread.
48 extern android_thread_id_t androidGetThreadId();
50 // Low-level thread creation -- never creates threads that can
51 // interact with the Java VM.
52 extern int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
54 const char* threadName,
55 int32_t threadPriority,
56 size_t threadStackSize,
57 android_thread_id_t *threadId);
59 // set the same of the running thread
60 extern void androidSetThreadName(const char* name);
62 // Used by the Java Runtime to control how threads are created, so that
63 // they can be proper and lovely Java threads.
64 typedef int (*android_create_thread_fn)(android_thread_func_t entryFunction,
66 const char* threadName,
67 int32_t threadPriority,
68 size_t threadStackSize,
69 android_thread_id_t *threadId);
71 extern void androidSetCreateThreadFunc(android_create_thread_fn func);
73 // ------------------------------------------------------------------
74 // Extra functions working with raw pids.
76 // Get pid for the current thread.
77 extern pid_t androidGetTid();
79 #ifdef HAVE_ANDROID_OS
80 // Change the priority AND scheduling group of a particular thread. The priority
81 // should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION
82 // if the priority set failed, else another value if just the group set failed;
83 // in either case errno is set. Thread ID zero means current thread.
84 extern int androidSetThreadPriority(pid_t tid, int prio);
86 // Get the current priority of a particular thread. Returns one of the
87 // ANDROID_PRIORITY constants or a negative result in case of error.
88 extern int androidGetThreadPriority(pid_t tid);
95 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // Create and run a new thread.
102 inline bool createThread(thread_func_t f, void *a) {
103 return androidCreateThread(f, a) ? true : false;
106 // Create thread with lots of parameters
107 inline bool createThreadEtc(thread_func_t entryFunction,
109 const char* threadName = "android:unnamed_thread",
110 int32_t threadPriority = PRIORITY_DEFAULT,
111 size_t threadStackSize = 0,
112 thread_id_t *threadId = 0)
114 return androidCreateThreadEtc(entryFunction, userData, threadName,
115 threadPriority, threadStackSize, threadId) ? true : false;
118 // Get some sort of unique identifier for the current thread.
119 inline thread_id_t getThreadId() {
120 return androidGetThreadId();
123 // ----------------------------------------------------------------------------
124 }; // namespace android
125 #endif // __cplusplus
126 // ----------------------------------------------------------------------------
128 #endif // _LIBS_UTILS_ANDROID_THREADS_H