AndroidThreads.h revision 0818b0921ef6cda07f41b56d2ef19b2849dfefd1
1f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian/* 2f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * Copyright (C) 2007 The Android Open Source Project 3f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * 4f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * you may not use this file except in compliance with the License. 6f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * You may obtain a copy of the License at 7f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * 8f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * 10f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * Unless required by applicable law or agreed to in writing, software 11f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * See the License for the specific language governing permissions and 14f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian * limitations under the License. 15f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian */ 16f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 17f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#ifndef _LIBS_UTILS_ANDROID_THREADS_H 18f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#define _LIBS_UTILS_ANDROID_THREADS_H 19f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 20f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#include <stdint.h> 21f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#include <sys/types.h> 22f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 23f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#if defined(HAVE_PTHREADS) 24f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian# include <pthread.h> 25f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#endif 26f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 27f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#include <utils/ThreadDefs.h> 28f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 29f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// --------------------------------------------------------------------------- 30f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// C API 31f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 32f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#ifdef __cplusplus 33f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern "C" { 34f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#endif 35f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 36f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Create and run a new thread. 37f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern int androidCreateThread(android_thread_func_t, void *); 38f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 39f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Create thread with lots of parameters 40f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern int androidCreateThreadEtc(android_thread_func_t entryFunction, 41f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian void *userData, 42f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian const char* threadName, 43f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian int32_t threadPriority, 44f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian size_t threadStackSize, 45f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian android_thread_id_t *threadId); 46f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 47f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Get some sort of unique identifier for the current thread. 48f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern android_thread_id_t androidGetThreadId(); 49f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 50f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Low-level thread creation -- never creates threads that can 51f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// interact with the Java VM. 52f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern int androidCreateRawThreadEtc(android_thread_func_t entryFunction, 53f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian void *userData, 54f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian const char* threadName, 55f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian int32_t threadPriority, 56f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian size_t threadStackSize, 57f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian android_thread_id_t *threadId); 58f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 59f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Used by the Java Runtime to control how threads are created, so that 60f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// they can be proper and lovely Java threads. 61f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopiantypedef int (*android_create_thread_fn)(android_thread_func_t entryFunction, 62f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian void *userData, 63f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian const char* threadName, 64f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian int32_t threadPriority, 65f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian size_t threadStackSize, 66f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian android_thread_id_t *threadId); 67f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 68f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern void androidSetCreateThreadFunc(android_create_thread_fn func); 69f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 70f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// ------------------------------------------------------------------ 71f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Extra functions working with raw pids. 72f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 73f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Get pid for the current thread. 74f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern pid_t androidGetTid(); 75f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 760818b0921ef6cda07f41b56d2ef19b2849dfefd1Jeff Brown#ifdef HAVE_ANDROID_OS 77f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Change the scheduling group of a particular thread. The group 78f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// should be one of the ANDROID_TGROUP constants. Returns BAD_VALUE if 79f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// grp is out of range, else another non-zero value with errno set if 80f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// the operation failed. Thread ID zero means current thread. 81f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern int androidSetThreadSchedulingGroup(pid_t tid, int grp); 82f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 83f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Change the priority AND scheduling group of a particular thread. The priority 84f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION 85f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// if the priority set failed, else another value if just the group set failed; 86f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// in either case errno is set. Thread ID zero means current thread. 87f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern int androidSetThreadPriority(pid_t tid, int prio); 88f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 89f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Get the current priority of a particular thread. Returns one of the 90f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// ANDROID_PRIORITY constants or a negative result in case of error. 91f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern int androidGetThreadPriority(pid_t tid); 92f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 93f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Get the current scheduling group of a particular thread. Normally returns 94f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// one of the ANDROID_TGROUP constants other than ANDROID_TGROUP_DEFAULT. 95f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Returns ANDROID_TGROUP_DEFAULT if no pthread support (e.g. on host) or if 96f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// scheduling groups are disabled. Returns INVALID_OPERATION if unexpected error. 97f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Thread ID zero means current thread. 98f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianextern int androidGetThreadSchedulingGroup(pid_t tid); 990818b0921ef6cda07f41b56d2ef19b2849dfefd1Jeff Brown#endif 100f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 101f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#ifdef __cplusplus 102f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian} // extern "C" 103f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#endif 104f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 105f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// ---------------------------------------------------------------------------- 106f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// C++ API 107f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#ifdef __cplusplus 108f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopiannamespace android { 109f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// ---------------------------------------------------------------------------- 110f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 111f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Create and run a new thread. 112f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianinline bool createThread(thread_func_t f, void *a) { 113f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian return androidCreateThread(f, a) ? true : false; 114f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian} 115f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 116f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Create thread with lots of parameters 117f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianinline bool createThreadEtc(thread_func_t entryFunction, 118f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian void *userData, 119f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian const char* threadName = "android:unnamed_thread", 120f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian int32_t threadPriority = PRIORITY_DEFAULT, 121f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian size_t threadStackSize = 0, 122f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian thread_id_t *threadId = 0) 123f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian{ 124f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian return androidCreateThreadEtc(entryFunction, userData, threadName, 125f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian threadPriority, threadStackSize, threadId) ? true : false; 126f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian} 127f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 128f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// Get some sort of unique identifier for the current thread. 129f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopianinline thread_id_t getThreadId() { 130f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian return androidGetThreadId(); 131f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian} 132f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 133f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// ---------------------------------------------------------------------------- 134f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian}; // namespace android 135f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#endif // __cplusplus 136f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian// ---------------------------------------------------------------------------- 137f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian 138f91bb05132dccbb61a69351752d9ae47cc31f30dMathias Agopian#endif // _LIBS_UTILS_ANDROID_THREADS_H 139