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_THREAD_H 182bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#define _LIBS_UTILS_THREAD_H 192bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 202bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <stdint.h> 212bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <sys/types.h> 222bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <time.h> 232bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 244a6e5a3b641dd99b658c4c336490371a3a5ae180Yabin Cui#if !defined(_WIN32) 252bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian# include <pthread.h> 262bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif 272bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 282bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <utils/Condition.h> 292bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <utils/Errors.h> 302bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <utils/Mutex.h> 312bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <utils/RefBase.h> 322bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <utils/Timers.h> 332bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#include <utils/ThreadDefs.h> 342bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 352bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// --------------------------------------------------------------------------- 362bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopiannamespace android { 372bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// --------------------------------------------------------------------------- 382bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 39b8f152d3e2b9368717743ebcb1277239a6c00659Steven Moreland// DO NOT USE: please use std::thread 40b8f152d3e2b9368717743ebcb1277239a6c00659Steven Moreland 412bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianclass Thread : virtual public RefBase 422bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian{ 432bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianpublic: 442bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // Create a Thread object, but doesn't create or start the associated 452bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // thread. See the run() method. 462a929968e1038d1940bab08a678263db3c2655eaChih-Hung Hsieh explicit Thread(bool canCallJava = true); 472bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian virtual ~Thread(); 482bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 492bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // Start the thread in threadLoop() which needs to be implemented. 50e71b9147756ab4da306e4c16461ad23936769603Brian Carlstrom virtual status_t run( const char* name, 512bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian int32_t priority = PRIORITY_DEFAULT, 522bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian size_t stack = 0); 532bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 542bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // Ask this object's thread to exit. This function is asynchronous, when the 552bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // function returns the thread might still be running. Of course, this 562bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // function can be called from a different thread. 572bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian virtual void requestExit(); 582bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 592bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // Good place to do one-time initializations 602bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian virtual status_t readyToRun(); 612bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 622bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // Call requestExit() and wait until this object's thread exits. 632bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // BE VERY CAREFUL of deadlocks. In particular, it would be silly to call 642bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // this function from this object's thread. Will return WOULD_BLOCK in 652bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // that case. 662bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian status_t requestExitAndWait(); 672bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 682bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // Wait until this object's thread exits. Returns immediately if not yet running. 692bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // Do not call from this object's thread; will return WOULD_BLOCK in that case. 702bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian status_t join(); 712bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 7231ba37f1c80801ee128749d4772c47f23323a3beRomain Guy // Indicates whether this thread is running or not. 7331ba37f1c80801ee128749d4772c47f23323a3beRomain Guy bool isRunning() const; 7431ba37f1c80801ee128749d4772c47f23323a3beRomain Guy 759b828adfad09200f3f1bd3602187fe3dd5335774Elliott Hughes#if defined(__ANDROID__) 76b7659613b2eed5df47e3e7ec536012772cc08b61Elliott Hughes // Return the thread's kernel ID, same as the thread itself calling gettid(), 77b7659613b2eed5df47e3e7ec536012772cc08b61Elliott Hughes // or -1 if the thread is not running. 782bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian pid_t getTid() const; 792bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif 802bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 812bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianprotected: 822bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // exitPending() returns true if requestExit() has been called. 832bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian bool exitPending() const; 842bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 852bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianprivate: 862bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // Derived class must implement threadLoop(). The thread starts its life 872bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // here. There are two ways of using the Thread object: 882bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // 1) loop: if threadLoop() returns true, it will be called again if 892bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // requestExit() wasn't called. 902bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // 2) once: if threadLoop() returns false, the thread will exit upon return. 912bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian virtual bool threadLoop() = 0; 922bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 932bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopianprivate: 942bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian Thread& operator=(const Thread&); 952bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian static int _threadLoop(void* user); 962bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian const bool mCanCallJava; 972bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // always hold mLock when reading or writing 982bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian thread_id_t mThread; 992bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian mutable Mutex mLock; 1002bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian Condition mThreadExitedCondition; 1012bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian status_t mStatus; 1022bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // note that all accesses of mExitPending and mRunning need to hold mLock 1032bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian volatile bool mExitPending; 1042bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian volatile bool mRunning; 1052bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian sp<Thread> mHoldSelf; 1069b828adfad09200f3f1bd3602187fe3dd5335774Elliott Hughes#if defined(__ANDROID__) 1072bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // legacy for debugging, not used by getTid() as it is set by the child thread 1082bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian // and so is not initialized until the child reaches that point 1092bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian pid_t mTid; 1102bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif 1112bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian}; 1122bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 1132bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 1142bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian}; // namespace android 1152bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian 1162bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// --------------------------------------------------------------------------- 1172bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian#endif // _LIBS_UTILS_THREAD_H 1182bd99599bb9eef197e6d1ccacb9f808ebfbcc598Mathias Agopian// --------------------------------------------------------------------------- 119