aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2015-02-03 10:32:00 -0800
committerYabin Cui <yabinc@google.com>2015-02-03 12:20:46 -0800
commit140f3678f0f21eeda5916e9b8de87b93fd660a61 (patch)
tree4115c771e910e8969cb093fa124df987259bad3c
parent86fc96f73311f43980df770f4ff8022f1e9b296a (diff)
downloadandroid_bionic-140f3678f0f21eeda5916e9b8de87b93fd660a61.tar.gz
android_bionic-140f3678f0f21eeda5916e9b8de87b93fd660a61.tar.bz2
android_bionic-140f3678f0f21eeda5916e9b8de87b93fd660a61.zip
Add test about pthread_mutex_t owner tid limit.
Bug: 19216648 Change-Id: I7b12955bdcad31c13bf8ec2740ff88ba15223ec0
-rw-r--r--libc/bionic/pthread_mutex.cpp27
-rw-r--r--tests/pthread_test.cpp12
2 files changed, 15 insertions, 24 deletions
diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp
index a0628b01d..83d6b541a 100644
--- a/libc/bionic/pthread_mutex.cpp
+++ b/libc/bionic/pthread_mutex.cpp
@@ -150,30 +150,9 @@
/* Mutex owner field:
*
* This is only used for recursive and errorcheck mutexes. It holds the
- * tid of the owning thread. Note that this works because the Linux
- * kernel _only_ uses 16-bit values for tids.
- *
- * More specifically, it will wrap to 10000 when it reaches over 32768 for
- * application processes. You can check this by running the following inside
- * an adb shell session:
- *
- OLDPID=$$;
- while true; do
- NEWPID=$(sh -c 'echo $$')
- if [ "$NEWPID" -gt 32768 ]; then
- echo "AARGH: new PID $NEWPID is too high!"
- exit 1
- fi
- if [ "$NEWPID" -lt "$OLDPID" ]; then
- echo "****** Wrapping from PID $OLDPID to $NEWPID. *******"
- else
- echo -n "$NEWPID!"
- fi
- OLDPID=$NEWPID
- done
-
- * Note that you can run the same example on a desktop Linux system,
- * the wrapping will also happen at 32768, but will go back to 300 instead.
+ * tid of the owning thread. We use 16 bits to represent tid here,
+ * so the highest tid is 65535. There is a test to check /proc/sys/kernel/pid_max
+ * to make sure it will not exceed our limit.
*/
#define MUTEX_OWNER_SHIFT 16
#define MUTEX_OWNER_LEN 16
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index cb3207923..5dc60eeee 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -27,6 +27,7 @@
#include <malloc.h>
#include <pthread.h>
#include <signal.h>
+#include <stdio.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <time.h>
@@ -1092,3 +1093,14 @@ TEST(pthread, pthread_mutex_lock_RECURSIVE) {
ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
ASSERT_EQ(0, pthread_mutex_destroy(&lock));
}
+
+TEST(pthread, pthread_mutex_owner_tid_limit) {
+ FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
+ ASSERT_TRUE(fp != NULL);
+ long pid_max;
+ ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
+ fclose(fp);
+ // Current pthread_mutex uses 16 bits to represent owner tid.
+ // Change the implementation if we need to support higher value than 65535.
+ ASSERT_LE(pid_max, 65536);
+}