diff options
| author | Vijay Venkatraman <vijaykv@google.com> | 2017-01-05 10:39:38 -0800 |
|---|---|---|
| committer | Vijay Venkatraman <vijaykv@google.com> | 2017-01-24 22:51:36 +0000 |
| commit | 75acc7bf81d43850694d39d2c45a20ca81d99379 (patch) | |
| tree | 39f8b964c90102fbc6a8b954110342724cf6f394 /libutils/include/utils/Thread.h | |
| parent | 897bc9b2b38ead33aa883359593eb4356b68bda2 (diff) | |
| download | system_core-75acc7bf81d43850694d39d2c45a20ca81d99379.tar.gz system_core-75acc7bf81d43850694d39d2c45a20ca81d99379.tar.bz2 system_core-75acc7bf81d43850694d39d2c45a20ca81d99379.zip | |
Exporting C++ headers from system/core
Moved headers from include/libutils and include/libsysutils to
libutils/include and libsysutils/include respectively, so they can be
exported via these libs. They needed to be moved since Soong does
not allow export from external folder.
Added symlink from old locations. They are needed since Soong
includes system/core/include by default. Once all modules are
cleaned up to explicitly add the required libs, the symlinks will be
removed.
Moved headers of libutils to libutils_headers. They should be used
by modules for header-only inlines. Added libutils_headers as
dependency of libutils.
Split of C++ headers into those that have no dependency and those that
have dependency on libutils.so will be handled in a later CL.
Test: Add above libs to shared lib of local module
Change-Id: I122db72056b26b1f39bad1d9a0c2a1c5efda3550
Diffstat (limited to 'libutils/include/utils/Thread.h')
| -rw-r--r-- | libutils/include/utils/Thread.h | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/libutils/include/utils/Thread.h b/libutils/include/utils/Thread.h new file mode 100644 index 000000000..a261fc8d9 --- /dev/null +++ b/libutils/include/utils/Thread.h @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _LIBS_UTILS_THREAD_H +#define _LIBS_UTILS_THREAD_H + +#include <stdint.h> +#include <sys/types.h> +#include <time.h> + +#if !defined(_WIN32) +# include <pthread.h> +#endif + +#include <utils/Condition.h> +#include <utils/Errors.h> +#include <utils/Mutex.h> +#include <utils/RefBase.h> +#include <utils/Timers.h> +#include <utils/ThreadDefs.h> + +// --------------------------------------------------------------------------- +namespace android { +// --------------------------------------------------------------------------- + +class Thread : virtual public RefBase +{ +public: + // Create a Thread object, but doesn't create or start the associated + // thread. See the run() method. + explicit Thread(bool canCallJava = true); + virtual ~Thread(); + + // Start the thread in threadLoop() which needs to be implemented. + virtual status_t run( const char* name, + int32_t priority = PRIORITY_DEFAULT, + size_t stack = 0); + + // Ask this object's thread to exit. This function is asynchronous, when the + // function returns the thread might still be running. Of course, this + // function can be called from a different thread. + virtual void requestExit(); + + // Good place to do one-time initializations + virtual status_t readyToRun(); + + // Call requestExit() and wait until this object's thread exits. + // BE VERY CAREFUL of deadlocks. In particular, it would be silly to call + // this function from this object's thread. Will return WOULD_BLOCK in + // that case. + status_t requestExitAndWait(); + + // Wait until this object's thread exits. Returns immediately if not yet running. + // Do not call from this object's thread; will return WOULD_BLOCK in that case. + status_t join(); + + // Indicates whether this thread is running or not. + bool isRunning() const; + +#if defined(__ANDROID__) + // Return the thread's kernel ID, same as the thread itself calling gettid(), + // or -1 if the thread is not running. + pid_t getTid() const; +#endif + +protected: + // exitPending() returns true if requestExit() has been called. + bool exitPending() const; + +private: + // Derived class must implement threadLoop(). The thread starts its life + // here. There are two ways of using the Thread object: + // 1) loop: if threadLoop() returns true, it will be called again if + // requestExit() wasn't called. + // 2) once: if threadLoop() returns false, the thread will exit upon return. + virtual bool threadLoop() = 0; + +private: + Thread& operator=(const Thread&); + static int _threadLoop(void* user); + const bool mCanCallJava; + // always hold mLock when reading or writing + thread_id_t mThread; + mutable Mutex mLock; + Condition mThreadExitedCondition; + status_t mStatus; + // note that all accesses of mExitPending and mRunning need to hold mLock + volatile bool mExitPending; + volatile bool mRunning; + sp<Thread> mHoldSelf; +#if defined(__ANDROID__) + // legacy for debugging, not used by getTid() as it is set by the child thread + // and so is not initialized until the child reaches that point + pid_t mTid; +#endif +}; + + +}; // namespace android + +// --------------------------------------------------------------------------- +#endif // _LIBS_UTILS_THREAD_H +// --------------------------------------------------------------------------- |
