blob: a8d7851759d20c472631e65712bea3d7bd0566c6 [file] [log] [blame]
Mathias Agopian2bd99592012-02-25 23:02:14 -08001/*
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
Yabin Cui4a6e5a32015-01-26 19:48:54 -080023#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -080024# include <pthread.h>
25#endif
26
27#include <utils/ThreadDefs.h>
28
29// ---------------------------------------------------------------------------
30// C API
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36// Create and run a new thread.
37extern int androidCreateThread(android_thread_func_t, void *);
38
39// Create thread with lots of parameters
40extern 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.
48extern android_thread_id_t androidGetThreadId();
49
50// Low-level thread creation -- never creates threads that can
51// interact with the Java VM.
52extern 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
Mathias Agopian6090df82013-03-07 15:34:28 -080059// set the same of the running thread
60extern void androidSetThreadName(const char* name);
61
Mathias Agopian2bd99592012-02-25 23:02:14 -080062// Used by the Java Runtime to control how threads are created, so that
63// they can be proper and lovely Java threads.
64typedef int (*android_create_thread_fn)(android_thread_func_t entryFunction,
65 void *userData,
66 const char* threadName,
67 int32_t threadPriority,
68 size_t threadStackSize,
69 android_thread_id_t *threadId);
70
71extern void androidSetCreateThreadFunc(android_create_thread_fn func);
72
73// ------------------------------------------------------------------
74// Extra functions working with raw pids.
75
Elliott Hughes9b828ad2015-07-30 08:47:35 -070076#if defined(__ANDROID__)
Mathias Agopian2bd99592012-02-25 23:02:14 -080077// Change the priority AND scheduling group of a particular thread. The priority
78// should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION
79// if the priority set failed, else another value if just the group set failed;
80// in either case errno is set. Thread ID zero means current thread.
81extern int androidSetThreadPriority(pid_t tid, int prio);
82
83// Get the current priority of a particular thread. Returns one of the
84// ANDROID_PRIORITY constants or a negative result in case of error.
85extern int androidGetThreadPriority(pid_t tid);
Jeff Brown27e6eaa2012-03-16 22:18:39 -070086#endif
Mathias Agopian2bd99592012-02-25 23:02:14 -080087
88#ifdef __cplusplus
89} // extern "C"
90#endif
91
92// ----------------------------------------------------------------------------
93// C++ API
94#ifdef __cplusplus
95namespace android {
96// ----------------------------------------------------------------------------
97
98// Create and run a new thread.
99inline bool createThread(thread_func_t f, void *a) {
100 return androidCreateThread(f, a) ? true : false;
101}
102
103// Create thread with lots of parameters
104inline bool createThreadEtc(thread_func_t entryFunction,
105 void *userData,
106 const char* threadName = "android:unnamed_thread",
107 int32_t threadPriority = PRIORITY_DEFAULT,
108 size_t threadStackSize = 0,
Yi Kongc1a15622018-07-11 17:37:34 -0700109 thread_id_t *threadId = nullptr)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800110{
111 return androidCreateThreadEtc(entryFunction, userData, threadName,
112 threadPriority, threadStackSize, threadId) ? true : false;
113}
114
115// Get some sort of unique identifier for the current thread.
116inline thread_id_t getThreadId() {
117 return androidGetThreadId();
118}
119
120// ----------------------------------------------------------------------------
Pirama Arumuga Nainar90affce2018-04-11 23:10:28 -0700121} // namespace android
Mathias Agopian2bd99592012-02-25 23:02:14 -0800122#endif // __cplusplus
123// ----------------------------------------------------------------------------
124
125#endif // _LIBS_UTILS_ANDROID_THREADS_H