Pull the pthread_key_t functions out of pthread.c.
This was originally motivated by noticing that we were setting the
wrong bits for the well-known tls entries. That was a harmless bug
because none of the well-known tls entries has a destructor, but
it's best not to leave land mines lying around.
Also add some missing POSIX constants, a new test, and fix
pthread_key_create's return value when we hit the limit.
Change-Id: Ife26ea2f4b40865308e8410ec803b20bcc3e0ed1
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 3e144db..2cf45f3 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -28,6 +28,25 @@
ASSERT_EQ(EINVAL, pthread_key_delete(key));
}
+TEST(pthread, pthread_key_create_lots) {
+ // We can allocate _SC_THREAD_KEYS_MAX keys.
+ std::vector<pthread_key_t> keys;
+ for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) {
+ pthread_key_t key;
+ ASSERT_EQ(0, pthread_key_create(&key, NULL));
+ keys.push_back(key);
+ }
+
+ // ...and that really is the maximum.
+ pthread_key_t key;
+ ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
+
+ // (Don't leak all those keys!)
+ for (size_t i = 0; i < keys.size(); ++i) {
+ ASSERT_EQ(0, pthread_key_delete(keys[i]));
+ }
+}
+
static void* IdFn(void* arg) {
return arg;
}