summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/utils/threads.h4
-rw-r--r--libs/utils/Threads.cpp19
2 files changed, 23 insertions, 0 deletions
diff --git a/include/utils/threads.h b/include/utils/threads.h
index 41f67e4..c8e9c04 100644
--- a/include/utils/threads.h
+++ b/include/utils/threads.h
@@ -510,6 +510,10 @@ public:
// 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();
+
protected:
// exitPending() returns true if requestExit() has been called.
bool exitPending() const;
diff --git a/libs/utils/Threads.cpp b/libs/utils/Threads.cpp
index 71352a8..50312e7 100644
--- a/libs/utils/Threads.cpp
+++ b/libs/utils/Threads.cpp
@@ -846,6 +846,25 @@ status_t Thread::requestExitAndWait()
return mStatus;
}
+status_t Thread::join()
+{
+ Mutex::Autolock _l(mLock);
+ if (mThread == getThreadId()) {
+ LOGW(
+ "Thread (this=%p): don't call join() from this "
+ "Thread object's thread. It's a guaranteed deadlock!",
+ this);
+
+ return WOULD_BLOCK;
+ }
+
+ while (mRunning == true) {
+ mThreadExitedCondition.wait(mLock);
+ }
+
+ return mStatus;
+}
+
bool Thread::exitPending() const
{
Mutex::Autolock _l(mLock);