12bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian/*
22bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * Copyright (C) 2007 The Android Open Source Project
32bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian *
42bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
52bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * you may not use this file except in compliance with the License.
62bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * You may obtain a copy of the License at
72bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian *
82bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
92bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian *
102bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * Unless required by applicable law or agreed to in writing, software
112bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
122bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * See the License for the specific language governing permissions and
142bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian * limitations under the License.
152bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian */
162bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
172bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#ifndef _LIBS_UTILS_ANDROID_THREADS_H
182bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#define _LIBS_UTILS_ANDROID_THREADS_H
192bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
202bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <stdint.h>
212bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <sys/types.h>
222bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
232bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#if defined(HAVE_PTHREADS)
242bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian# include <pthread.h>
252bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif
262bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
272bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <utils/ThreadDefs.h>
282bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
292bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// ---------------------------------------------------------------------------
302bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// C API
312bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
322bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#ifdef __cplusplus
332bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern "C" {
342bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif
352bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
362bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Create and run a new thread.
372bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern int androidCreateThread(android_thread_func_t, void *);
382bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
392bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Create thread with lots of parameters
402bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern int androidCreateThreadEtc(android_thread_func_t entryFunction,
412bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                  void *userData,
422bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                  const char* threadName,
432bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                  int32_t threadPriority,
442bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                  size_t threadStackSize,
452bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                  android_thread_id_t *threadId);
462bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
472bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Get some sort of unique identifier for the current thread.
482bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern android_thread_id_t androidGetThreadId();
492bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
502bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Low-level thread creation -- never creates threads that can
512bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// interact with the Java VM.
522bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
532bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                     void *userData,
542bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                     const char* threadName,
552bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                     int32_t threadPriority,
562bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                     size_t threadStackSize,
572bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                     android_thread_id_t *threadId);
582bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
596090df85a8a227db0bf407b7877b2777937e6427Mathias Agopian// set the same of the running thread
606090df85a8a227db0bf407b7877b2777937e6427Mathias Agopianextern void androidSetThreadName(const char* name);
616090df85a8a227db0bf407b7877b2777937e6427Mathias Agopian
622bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Used by the Java Runtime to control how threads are created, so that
632bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// they can be proper and lovely Java threads.
642bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopiantypedef int (*android_create_thread_fn)(android_thread_func_t entryFunction,
652bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                        void *userData,
662bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                        const char* threadName,
672bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                        int32_t threadPriority,
682bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                        size_t threadStackSize,
692bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                                        android_thread_id_t *threadId);
702bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
712bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern void androidSetCreateThreadFunc(android_create_thread_fn func);
722bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
732bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// ------------------------------------------------------------------
742bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Extra functions working with raw pids.
752bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
762bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Get pid for the current thread.
772bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern pid_t androidGetTid();
782bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
7927e6eaae879309d6bbfb708e1e1aa75d7431a3a4Jeff Brown#ifdef HAVE_ANDROID_OS
802bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Change the priority AND scheduling group of a particular thread.  The priority
812bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// should be one of the ANDROID_PRIORITY constants.  Returns INVALID_OPERATION
822bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// if the priority set failed, else another value if just the group set failed;
832bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// in either case errno is set.  Thread ID zero means current thread.
842bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern int androidSetThreadPriority(pid_t tid, int prio);
852bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
862bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Get the current priority of a particular thread. Returns one of the
872bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// ANDROID_PRIORITY constants or a negative result in case of error.
882bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianextern int androidGetThreadPriority(pid_t tid);
8927e6eaae879309d6bbfb708e1e1aa75d7431a3a4Jeff Brown#endif
902bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
912bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#ifdef __cplusplus
922bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian} // extern "C"
932bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif
942bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
952bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// ----------------------------------------------------------------------------
962bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// C++ API
972bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#ifdef __cplusplus
982bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopiannamespace android {
992bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// ----------------------------------------------------------------------------
1002bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
1012bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Create and run a new thread.
1022bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianinline bool createThread(thread_func_t f, void *a) {
1032bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian    return androidCreateThread(f, a) ? true : false;
1042bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian}
1052bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
1062bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Create thread with lots of parameters
1072bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianinline bool createThreadEtc(thread_func_t entryFunction,
1082bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                            void *userData,
1092bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                            const char* threadName = "android:unnamed_thread",
1102bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                            int32_t threadPriority = PRIORITY_DEFAULT,
1112bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                            size_t threadStackSize = 0,
1122bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian                            thread_id_t *threadId = 0)
1132bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian{
1142bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian    return androidCreateThreadEtc(entryFunction, userData, threadName,
1152bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian        threadPriority, threadStackSize, threadId) ? true : false;
1162bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian}
1172bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
1182bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// Get some sort of unique identifier for the current thread.
1192bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianinline thread_id_t getThreadId() {
1202bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian    return androidGetThreadId();
1212bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian}
1222bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
1232bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// ----------------------------------------------------------------------------
1242bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian}; // namespace android
1252bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif  // __cplusplus
1262bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// ----------------------------------------------------------------------------
1272bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian
1282bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif // _LIBS_UTILS_ANDROID_THREADS_H
129