Mathias Agopian | f91bb05 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _LIBS_UTILS_ANDROID_THREADS_H |
| 18 | #define _LIBS_UTILS_ANDROID_THREADS_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #if defined(HAVE_PTHREADS) |
| 24 | # include <pthread.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <utils/ThreadDefs.h> |
| 28 | |
| 29 | // --------------------------------------------------------------------------- |
| 30 | // C API |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | // Create and run a new thread. |
| 37 | extern int androidCreateThread(android_thread_func_t, void *); |
| 38 | |
| 39 | // Create thread with lots of parameters |
| 40 | extern int androidCreateThreadEtc(android_thread_func_t entryFunction, |
| 41 | void *userData, |
| 42 | const char* threadName, |
| 43 | int32_t threadPriority, |
| 44 | size_t threadStackSize, |
| 45 | android_thread_id_t *threadId); |
| 46 | |
| 47 | // Get some sort of unique identifier for the current thread. |
| 48 | extern android_thread_id_t androidGetThreadId(); |
| 49 | |
| 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, |
| 53 | void *userData, |
| 54 | const char* threadName, |
| 55 | int32_t threadPriority, |
| 56 | size_t threadStackSize, |
| 57 | android_thread_id_t *threadId); |
| 58 | |
| 59 | // Used by the Java Runtime to control how threads are created, so that |
| 60 | // they can be proper and lovely Java threads. |
| 61 | typedef int (*android_create_thread_fn)(android_thread_func_t entryFunction, |
| 62 | void *userData, |
| 63 | const char* threadName, |
| 64 | int32_t threadPriority, |
| 65 | size_t threadStackSize, |
| 66 | android_thread_id_t *threadId); |
| 67 | |
| 68 | extern void androidSetCreateThreadFunc(android_create_thread_fn func); |
| 69 | |
| 70 | // ------------------------------------------------------------------ |
| 71 | // Extra functions working with raw pids. |
| 72 | |
| 73 | // Get pid for the current thread. |
| 74 | extern pid_t androidGetTid(); |
| 75 | |
Jeff Brown | 0818b09 | 2012-03-16 22:18:39 -0700 | [diff] [blame^] | 76 | #ifdef HAVE_ANDROID_OS |
Mathias Agopian | f91bb05 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 77 | // Change the scheduling group of a particular thread. The group |
| 78 | // should be one of the ANDROID_TGROUP constants. Returns BAD_VALUE if |
| 79 | // grp is out of range, else another non-zero value with errno set if |
| 80 | // the operation failed. Thread ID zero means current thread. |
| 81 | extern int androidSetThreadSchedulingGroup(pid_t tid, int grp); |
| 82 | |
| 83 | // Change the priority AND scheduling group of a particular thread. The priority |
| 84 | // should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION |
| 85 | // if the priority set failed, else another value if just the group set failed; |
| 86 | // in either case errno is set. Thread ID zero means current thread. |
| 87 | extern int androidSetThreadPriority(pid_t tid, int prio); |
| 88 | |
| 89 | // Get the current priority of a particular thread. Returns one of the |
| 90 | // ANDROID_PRIORITY constants or a negative result in case of error. |
| 91 | extern int androidGetThreadPriority(pid_t tid); |
| 92 | |
| 93 | // Get the current scheduling group of a particular thread. Normally returns |
| 94 | // one of the ANDROID_TGROUP constants other than ANDROID_TGROUP_DEFAULT. |
| 95 | // Returns ANDROID_TGROUP_DEFAULT if no pthread support (e.g. on host) or if |
| 96 | // scheduling groups are disabled. Returns INVALID_OPERATION if unexpected error. |
| 97 | // Thread ID zero means current thread. |
| 98 | extern int androidGetThreadSchedulingGroup(pid_t tid); |
Jeff Brown | 0818b09 | 2012-03-16 22:18:39 -0700 | [diff] [blame^] | 99 | #endif |
Mathias Agopian | f91bb05 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 100 | |
| 101 | #ifdef __cplusplus |
| 102 | } // extern "C" |
| 103 | #endif |
| 104 | |
| 105 | // ---------------------------------------------------------------------------- |
| 106 | // C++ API |
| 107 | #ifdef __cplusplus |
| 108 | namespace android { |
| 109 | // ---------------------------------------------------------------------------- |
| 110 | |
| 111 | // Create and run a new thread. |
| 112 | inline bool createThread(thread_func_t f, void *a) { |
| 113 | return androidCreateThread(f, a) ? true : false; |
| 114 | } |
| 115 | |
| 116 | // Create thread with lots of parameters |
| 117 | inline bool createThreadEtc(thread_func_t entryFunction, |
| 118 | void *userData, |
| 119 | const char* threadName = "android:unnamed_thread", |
| 120 | int32_t threadPriority = PRIORITY_DEFAULT, |
| 121 | size_t threadStackSize = 0, |
| 122 | thread_id_t *threadId = 0) |
| 123 | { |
| 124 | return androidCreateThreadEtc(entryFunction, userData, threadName, |
| 125 | threadPriority, threadStackSize, threadId) ? true : false; |
| 126 | } |
| 127 | |
| 128 | // Get some sort of unique identifier for the current thread. |
| 129 | inline thread_id_t getThreadId() { |
| 130 | return androidGetThreadId(); |
| 131 | } |
| 132 | |
| 133 | // ---------------------------------------------------------------------------- |
| 134 | }; // namespace android |
| 135 | #endif // __cplusplus |
| 136 | // ---------------------------------------------------------------------------- |
| 137 | |
| 138 | #endif // _LIBS_UTILS_ANDROID_THREADS_H |