Merge "Stop checking the global thread list in several trivial cases."

am: 4708f435f5

Change-Id: I1b3ba92036b68a94f342579ea34c385e3496a84b
This commit is contained in:
Elliott Hughes 2017-02-07 00:36:27 +00:00 committed by android-build-merger
commit 56209f0dd4
5 changed files with 13 additions and 66 deletions

View file

@ -31,13 +31,11 @@
#include "pthread_internal.h"
int pthread_getcpuclockid(pthread_t t, clockid_t* clockid) {
pthread_internal_t* thread = __pthread_internal_find(t);
if (thread == NULL) {
return ESRCH;
}
pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
if (tid == 0) return ESRCH;
// The tid is stored in the top bits, but negated.
clockid_t result = ~static_cast<clockid_t>(thread->tid) << 3;
clockid_t result = ~static_cast<clockid_t>(tid) << 3;
// Bits 0 and 1: clock type (0 = CPUCLOCK_PROF, 1 = CPUCLOCK_VIRT, 2 = CPUCLOCK_SCHED).
result |= 2;
// Bit 2: thread (set) or process (clear)?

View file

@ -34,15 +34,11 @@
int pthread_getschedparam(pthread_t t, int* policy, sched_param* param) {
ErrnoRestorer errno_restorer;
pthread_internal_t* thread = __pthread_internal_find(t);
if (thread == NULL) {
return ESRCH;
}
pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
if (tid == 0) return ESRCH;
int rc = sched_getparam(thread->tid, param);
if (rc == -1) {
return errno;
}
*policy = sched_getscheduler(thread->tid);
if (sched_getparam(tid, param) == -1) return errno;
*policy = sched_getscheduler(tid);
return 0;
}

View file

@ -43,14 +43,9 @@
#define MAX_TASK_COMM_LEN 16
static int __open_task_comm_fd(pthread_t t, int flags) {
pthread_internal_t* thread = __pthread_internal_find(t);
if (thread == nullptr) {
errno = ENOENT;
return -1;
}
char comm_name[64];
snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm", thread->tid);
snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm",
reinterpret_cast<pthread_internal_t*>(t)->tid);
return open(comm_name, O_CLOEXEC | flags);
}

View file

@ -34,14 +34,8 @@
int pthread_setschedparam(pthread_t t, int policy, const sched_param* param) {
ErrnoRestorer errno_restorer;
pthread_internal_t* thread = __pthread_internal_find(t);
if (thread == NULL) {
return ESRCH;
}
pid_t tid = reinterpret_cast<pthread_internal_t*>(t)->tid;
if (tid == 0) return ESRCH;
int rc = sched_setscheduler(thread->tid, policy, param);
if (rc == -1) {
return errno;
}
return 0;
return (sched_setscheduler(tid, policy, param) == -1) ? errno : 0;
}

View file

@ -443,16 +443,6 @@ TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) {
ASSERT_EQ(0, pthread_join(t, nullptr));
}
TEST(pthread, pthread_setname_np__pthread_getname_np__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
// Call pthread_getname_np and pthread_setname_np after the thread has already exited.
ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
char name[64];
ASSERT_EQ(ENOENT, pthread_getname_np(dead_thread, name, sizeof(name)));
}
TEST(pthread, pthread_kill__0) {
// Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
@ -497,32 +487,6 @@ TEST(pthread, pthread_getcpuclockid__clock_gettime) {
ASSERT_EQ(0, pthread_join(t, nullptr));
}
TEST(pthread, pthread_getcpuclockid__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
clockid_t c;
ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
}
TEST(pthread, pthread_getschedparam__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
int policy;
sched_param param;
ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
}
TEST(pthread, pthread_setschedparam__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);
int policy = 0;
sched_param param;
ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
}
TEST(pthread, pthread_join__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);