Merge "Thread::getTid returns pid_t gettid() after run"
This commit is contained in:
@@ -526,6 +526,12 @@ public:
|
|||||||
// Do not call from this object's thread; will return WOULD_BLOCK in that case.
|
// Do not call from this object's thread; will return WOULD_BLOCK in that case.
|
||||||
status_t join();
|
status_t join();
|
||||||
|
|
||||||
|
#ifdef HAVE_ANDROID_OS
|
||||||
|
// Return the thread's kernel ID, same as the thread itself calling gettid() or
|
||||||
|
// androidGetTid(), or -1 if the thread is not running.
|
||||||
|
pid_t getTid() const;
|
||||||
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// exitPending() returns true if requestExit() has been called.
|
// exitPending() returns true if requestExit() has been called.
|
||||||
bool exitPending() const;
|
bool exitPending() const;
|
||||||
@@ -551,8 +557,10 @@ private:
|
|||||||
volatile bool mExitPending;
|
volatile bool mExitPending;
|
||||||
volatile bool mRunning;
|
volatile bool mRunning;
|
||||||
sp<Thread> mHoldSelf;
|
sp<Thread> mHoldSelf;
|
||||||
#if HAVE_ANDROID_OS
|
#ifdef HAVE_ANDROID_OS
|
||||||
int mTid;
|
// 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
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -98,7 +98,8 @@ endif
|
|||||||
|
|
||||||
LOCAL_C_INCLUDES += \
|
LOCAL_C_INCLUDES += \
|
||||||
external/zlib \
|
external/zlib \
|
||||||
external/icu4c/common
|
external/icu4c/common \
|
||||||
|
bionic/libc/private
|
||||||
|
|
||||||
LOCAL_LDLIBS += -lpthread
|
LOCAL_LDLIBS += -lpthread
|
||||||
|
|
||||||
@@ -114,7 +115,10 @@ include $(BUILD_SHARED_LIBRARY)
|
|||||||
|
|
||||||
ifeq ($(TARGET_OS),linux)
|
ifeq ($(TARGET_OS),linux)
|
||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
LOCAL_C_INCLUDES += external/zlib external/icu4c/common
|
LOCAL_C_INCLUDES += \
|
||||||
|
external/zlib \
|
||||||
|
external/icu4c/common \
|
||||||
|
bionic/libc/private
|
||||||
LOCAL_LDLIBS := -lrt -ldl -lpthread
|
LOCAL_LDLIBS := -lrt -ldl -lpthread
|
||||||
LOCAL_MODULE := libutils
|
LOCAL_MODULE := libutils
|
||||||
LOCAL_SRC_FILES := $(commonSources) BackupData.cpp BackupHelpers.cpp
|
LOCAL_SRC_FILES := $(commonSources) BackupData.cpp BackupHelpers.cpp
|
||||||
|
|||||||
@@ -34,6 +34,9 @@
|
|||||||
# include <pthread.h>
|
# include <pthread.h>
|
||||||
# include <sched.h>
|
# include <sched.h>
|
||||||
# include <sys/resource.h>
|
# include <sys/resource.h>
|
||||||
|
#ifdef HAVE_ANDROID_OS
|
||||||
|
# include <bionic_pthread.h>
|
||||||
|
#endif
|
||||||
#elif defined(HAVE_WIN32_THREADS)
|
#elif defined(HAVE_WIN32_THREADS)
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
# include <stdint.h>
|
# include <stdint.h>
|
||||||
@@ -86,7 +89,7 @@ struct thread_data_t {
|
|||||||
char * threadName;
|
char * threadName;
|
||||||
|
|
||||||
// we use this trampoline when we need to set the priority with
|
// we use this trampoline when we need to set the priority with
|
||||||
// nice/setpriority.
|
// nice/setpriority, and name with prctl.
|
||||||
static int trampoline(const thread_data_t* t) {
|
static int trampoline(const thread_data_t* t) {
|
||||||
thread_func_t f = t->entryFunction;
|
thread_func_t f = t->entryFunction;
|
||||||
void* u = t->userData;
|
void* u = t->userData;
|
||||||
@@ -141,8 +144,13 @@ int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
|
|||||||
|
|
||||||
#ifdef HAVE_ANDROID_OS /* valgrind is rejecting RT-priority create reqs */
|
#ifdef HAVE_ANDROID_OS /* valgrind is rejecting RT-priority create reqs */
|
||||||
if (threadPriority != PRIORITY_DEFAULT || threadName != NULL) {
|
if (threadPriority != PRIORITY_DEFAULT || threadName != NULL) {
|
||||||
// We could avoid the trampoline if there was a way to get to the
|
// Now that the pthread_t has a method to find the associated
|
||||||
// android_thread_id_t (pid) from pthread_t
|
// android_thread_id_t (pid) from pthread_t, it would be possible to avoid
|
||||||
|
// this trampoline in some cases as the parent could set the properties
|
||||||
|
// for the child. However, there would be a race condition because the
|
||||||
|
// child becomes ready immediately, and it doesn't work for the name.
|
||||||
|
// prctl(PR_SET_NAME) only works for self; prctl(PR_SET_THREAD_NAME) was
|
||||||
|
// proposed but not yet accepted.
|
||||||
thread_data_t* t = new thread_data_t;
|
thread_data_t* t = new thread_data_t;
|
||||||
t->priority = threadPriority;
|
t->priority = threadPriority;
|
||||||
t->threadName = threadName ? strdup(threadName) : NULL;
|
t->threadName = threadName ? strdup(threadName) : NULL;
|
||||||
@@ -178,6 +186,13 @@ int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_ANDROID_OS
|
||||||
|
static pthread_t android_thread_id_t_to_pthread(android_thread_id_t thread)
|
||||||
|
{
|
||||||
|
return (pthread_t) thread;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
android_thread_id_t androidGetThreadId()
|
android_thread_id_t androidGetThreadId()
|
||||||
{
|
{
|
||||||
return (android_thread_id_t)pthread_self();
|
return (android_thread_id_t)pthread_self();
|
||||||
@@ -909,6 +924,23 @@ status_t Thread::join()
|
|||||||
return mStatus;
|
return mStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_ANDROID_OS
|
||||||
|
pid_t Thread::getTid() const
|
||||||
|
{
|
||||||
|
// mTid is not defined until the child initializes it, and the caller may need it earlier
|
||||||
|
Mutex::Autolock _l(mLock);
|
||||||
|
pid_t tid;
|
||||||
|
if (mRunning) {
|
||||||
|
pthread_t pthread = android_thread_id_t_to_pthread(mThread);
|
||||||
|
tid = __pthread_gettid(pthread);
|
||||||
|
} else {
|
||||||
|
ALOGW("Thread (this=%p): getTid() is undefined before run()", this);
|
||||||
|
tid = -1;
|
||||||
|
}
|
||||||
|
return tid;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
bool Thread::exitPending() const
|
bool Thread::exitPending() const
|
||||||
{
|
{
|
||||||
Mutex::Autolock _l(mLock);
|
Mutex::Autolock _l(mLock);
|
||||||
|
|||||||
Reference in New Issue
Block a user