2012-09-06 02:47:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2013-10-03 01:59:05 +02:00
|
|
|
#include <inttypes.h>
|
2013-07-15 23:51:07 +02:00
|
|
|
#include <limits.h>
|
2014-03-08 02:59:05 +01:00
|
|
|
#include <malloc.h>
|
2012-09-06 02:47:37 +02:00
|
|
|
#include <pthread.h>
|
2013-12-21 03:43:21 +01:00
|
|
|
#include <signal.h>
|
2015-02-03 19:32:00 +01:00
|
|
|
#include <stdio.h>
|
2013-11-15 20:51:07 +01:00
|
|
|
#include <sys/mman.h>
|
2016-04-11 21:43:05 +02:00
|
|
|
#include <sys/prctl.h>
|
2014-08-26 02:26:50 +02:00
|
|
|
#include <sys/syscall.h>
|
2014-03-03 16:38:51 +01:00
|
|
|
#include <time.h>
|
2012-09-08 01:47:54 +02:00
|
|
|
#include <unistd.h>
|
2015-09-22 20:16:15 +02:00
|
|
|
#include <unwind.h>
|
2012-09-06 02:47:37 +02:00
|
|
|
|
2015-02-12 02:04:36 +01:00
|
|
|
#include <atomic>
|
2018-10-18 00:23:03 +02:00
|
|
|
#include <future>
|
2015-03-17 06:46:42 +01:00
|
|
|
#include <vector>
|
2015-02-12 02:04:36 +01:00
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
#include <android-base/parseint.h>
|
2017-04-06 01:20:29 +02:00
|
|
|
#include <android-base/scopeguard.h>
|
2018-01-23 21:56:18 +01:00
|
|
|
#include <android-base/strings.h>
|
2017-04-06 01:20:29 +02:00
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
#include "private/bionic_constants.h"
|
2015-03-21 23:08:25 +01:00
|
|
|
#include "private/bionic_macros.h"
|
|
|
|
#include "BionicDeathTest.h"
|
2018-02-07 21:44:45 +01:00
|
|
|
#include "SignalUtils.h"
|
2015-09-23 01:40:14 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
2012-09-06 02:47:37 +02:00
|
|
|
TEST(pthread, pthread_key_create) {
|
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_key_create(&key, nullptr));
|
2012-09-06 02:47:37 +02:00
|
|
|
ASSERT_EQ(0, pthread_key_delete(key));
|
|
|
|
// Can't delete a key that's already been deleted.
|
|
|
|
ASSERT_EQ(EINVAL, pthread_key_delete(key));
|
|
|
|
}
|
2012-09-08 01:47:54 +02:00
|
|
|
|
2014-09-30 20:48:24 +02:00
|
|
|
TEST(pthread, pthread_keys_max) {
|
2014-12-12 05:50:41 +01:00
|
|
|
// POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
|
|
|
|
ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
|
2014-09-30 20:48:24 +02:00
|
|
|
}
|
2014-01-29 02:02:03 +01:00
|
|
|
|
2014-12-12 05:50:41 +01:00
|
|
|
TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
|
2014-09-30 20:48:24 +02:00
|
|
|
int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
|
2014-12-12 05:50:41 +01:00
|
|
|
ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
|
2014-09-30 20:48:24 +02:00
|
|
|
}
|
2013-12-12 20:02:41 +01:00
|
|
|
|
2014-09-30 20:48:24 +02:00
|
|
|
TEST(pthread, pthread_key_many_distinct) {
|
2014-12-12 05:50:41 +01:00
|
|
|
// As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
|
|
|
|
// pthread keys, but We should be able to allocate at least this many keys.
|
|
|
|
int nkeys = PTHREAD_KEYS_MAX / 2;
|
2013-02-11 21:18:47 +01:00
|
|
|
std::vector<pthread_key_t> keys;
|
2014-09-30 20:48:24 +02:00
|
|
|
|
2017-04-06 01:20:29 +02:00
|
|
|
auto scope_guard = android::base::make_scope_guard([&keys] {
|
2015-10-03 03:25:19 +02:00
|
|
|
for (const auto& key : keys) {
|
2014-09-30 20:48:24 +02:00
|
|
|
EXPECT_EQ(0, pthread_key_delete(key));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (int i = 0; i < nkeys; ++i) {
|
2013-02-11 21:18:47 +01:00
|
|
|
pthread_key_t key;
|
2015-03-31 19:56:58 +02:00
|
|
|
// If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong.
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_key_create(&key, nullptr)) << i << " of " << nkeys;
|
2013-02-11 21:18:47 +01:00
|
|
|
keys.push_back(key);
|
2014-09-30 20:48:24 +02:00
|
|
|
ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
|
2013-02-11 21:18:47 +01:00
|
|
|
}
|
|
|
|
|
2014-09-30 20:48:24 +02:00
|
|
|
for (int i = keys.size() - 1; i >= 0; --i) {
|
|
|
|
ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
|
|
|
|
pthread_key_t key = keys.back();
|
|
|
|
keys.pop_back();
|
|
|
|
ASSERT_EQ(0, pthread_key_delete(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 05:50:41 +01:00
|
|
|
TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
|
2014-09-30 20:48:24 +02:00
|
|
|
std::vector<pthread_key_t> keys;
|
|
|
|
int rv = 0;
|
2014-12-12 05:50:41 +01:00
|
|
|
|
|
|
|
// Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
|
|
|
|
// be more than we are allowed to allocate now.
|
|
|
|
for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
|
2014-09-30 20:48:24 +02:00
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
rv = pthread_key_create(&key, nullptr);
|
2014-09-30 20:48:24 +02:00
|
|
|
if (rv == EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(0, rv);
|
|
|
|
keys.push_back(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't leak keys.
|
2015-10-03 03:25:19 +02:00
|
|
|
for (const auto& key : keys) {
|
2014-09-30 20:48:24 +02:00
|
|
|
EXPECT_EQ(0, pthread_key_delete(key));
|
2013-02-11 21:18:47 +01:00
|
|
|
}
|
2014-09-30 20:48:24 +02:00
|
|
|
keys.clear();
|
|
|
|
|
|
|
|
// We should have eventually reached the maximum number of keys and received
|
|
|
|
// EAGAIN.
|
|
|
|
ASSERT_EQ(EAGAIN, rv);
|
2013-02-11 21:18:47 +01:00
|
|
|
}
|
|
|
|
|
2014-06-25 22:46:46 +02:00
|
|
|
TEST(pthread, pthread_key_delete) {
|
|
|
|
void* expected = reinterpret_cast<void*>(1234);
|
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_key_create(&key, nullptr));
|
2014-06-25 22:46:46 +02:00
|
|
|
ASSERT_EQ(0, pthread_setspecific(key, expected));
|
|
|
|
ASSERT_EQ(expected, pthread_getspecific(key));
|
|
|
|
ASSERT_EQ(0, pthread_key_delete(key));
|
2018-08-03 02:31:13 +02:00
|
|
|
// After deletion, pthread_getspecific returns nullptr.
|
|
|
|
ASSERT_EQ(nullptr, pthread_getspecific(key));
|
2014-06-25 22:46:46 +02:00
|
|
|
// And you can't use pthread_setspecific with the deleted key.
|
|
|
|
ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
|
|
|
|
}
|
|
|
|
|
2014-07-30 23:48:10 +02:00
|
|
|
TEST(pthread, pthread_key_fork) {
|
|
|
|
void* expected = reinterpret_cast<void*>(1234);
|
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_key_create(&key, nullptr));
|
2014-07-30 23:48:10 +02:00
|
|
|
ASSERT_EQ(0, pthread_setspecific(key, expected));
|
|
|
|
ASSERT_EQ(expected, pthread_getspecific(key));
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
ASSERT_NE(-1, pid) << strerror(errno);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
// The surviving thread inherits all the forking thread's TLS values...
|
|
|
|
ASSERT_EQ(expected, pthread_getspecific(key));
|
|
|
|
_exit(99);
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:04:57 +01:00
|
|
|
AssertChildExited(pid, 99);
|
2014-07-30 23:48:10 +02:00
|
|
|
|
|
|
|
ASSERT_EQ(expected, pthread_getspecific(key));
|
2014-09-03 00:24:26 +02:00
|
|
|
ASSERT_EQ(0, pthread_key_delete(key));
|
2014-07-30 23:48:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* DirtyKeyFn(void* key) {
|
|
|
|
return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_key_dirty) {
|
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_key_create(&key, nullptr));
|
2014-07-30 23:48:10 +02:00
|
|
|
|
2015-11-17 06:06:16 +01:00
|
|
|
size_t stack_size = 640 * 1024;
|
2018-08-03 02:31:13 +02:00
|
|
|
void* stack = mmap(nullptr, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
|
2014-07-30 23:48:10 +02:00
|
|
|
ASSERT_NE(MAP_FAILED, stack);
|
|
|
|
memset(stack, 0xff, stack_size);
|
|
|
|
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
|
|
|
|
|
|
|
|
pthread_t t;
|
|
|
|
ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
|
|
|
|
|
|
|
|
void* result;
|
|
|
|
ASSERT_EQ(0, pthread_join(t, &result));
|
|
|
|
ASSERT_EQ(nullptr, result); // Not ~0!
|
|
|
|
|
|
|
|
ASSERT_EQ(0, munmap(stack, stack_size));
|
2014-09-03 00:24:26 +02:00
|
|
|
ASSERT_EQ(0, pthread_key_delete(key));
|
2014-07-30 23:48:10 +02:00
|
|
|
}
|
|
|
|
|
2015-03-06 05:35:32 +01:00
|
|
|
TEST(pthread, static_pthread_key_used_before_creation) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
// See http://b/19625804. The bug is about a static/global pthread key being used before creation.
|
|
|
|
// So here tests if the static/global default value 0 can be detected as invalid key.
|
|
|
|
static pthread_key_t key;
|
|
|
|
ASSERT_EQ(nullptr, pthread_getspecific(key));
|
|
|
|
ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr));
|
|
|
|
ASSERT_EQ(EINVAL, pthread_key_delete(key));
|
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-08 01:47:54 +02:00
|
|
|
static void* IdFn(void* arg) {
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2014-12-02 02:41:04 +01:00
|
|
|
class SpinFunctionHelper {
|
|
|
|
public:
|
|
|
|
SpinFunctionHelper() {
|
|
|
|
SpinFunctionHelper::spin_flag_ = true;
|
|
|
|
}
|
2017-11-02 21:11:13 +01:00
|
|
|
|
2014-12-02 02:41:04 +01:00
|
|
|
~SpinFunctionHelper() {
|
|
|
|
UnSpin();
|
|
|
|
}
|
2017-11-02 21:11:13 +01:00
|
|
|
|
2014-12-02 02:41:04 +01:00
|
|
|
auto GetFunction() -> void* (*)(void*) {
|
|
|
|
return SpinFunctionHelper::SpinFn;
|
|
|
|
}
|
2012-09-08 01:47:54 +02:00
|
|
|
|
2014-12-02 02:41:04 +01:00
|
|
|
void UnSpin() {
|
|
|
|
SpinFunctionHelper::spin_flag_ = false;
|
2012-10-26 12:06:43 +02:00
|
|
|
}
|
2014-12-02 02:41:04 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void* SpinFn(void*) {
|
|
|
|
while (spin_flag_) {}
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2014-12-02 02:41:04 +01:00
|
|
|
}
|
2015-11-17 06:06:16 +01:00
|
|
|
static std::atomic<bool> spin_flag_;
|
2014-12-02 02:41:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// It doesn't matter if spin_flag_ is used in several tests,
|
|
|
|
// because it is always set to false after each test. Each thread
|
|
|
|
// loops on spin_flag_ can find it becomes false at some time.
|
2015-11-17 06:06:16 +01:00
|
|
|
std::atomic<bool> SpinFunctionHelper::spin_flag_;
|
2012-10-26 12:06:43 +02:00
|
|
|
|
2012-09-08 01:47:54 +02:00
|
|
|
static void* JoinFn(void* arg) {
|
2018-08-03 02:31:13 +02:00
|
|
|
return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), nullptr));
|
2012-09-08 01:47:54 +02:00
|
|
|
}
|
|
|
|
|
2012-10-26 12:06:43 +02:00
|
|
|
static void AssertDetached(pthread_t t, bool is_detached) {
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_getattr_np(t, &attr));
|
|
|
|
int detach_state;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:41:38 +01:00
|
|
|
static void MakeDeadThread(pthread_t& t) {
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, IdFn, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
2017-02-02 03:41:38 +01:00
|
|
|
}
|
|
|
|
|
2012-09-08 01:47:54 +02:00
|
|
|
TEST(pthread, pthread_create) {
|
|
|
|
void* expected_result = reinterpret_cast<void*>(123);
|
|
|
|
// Can we create a thread?
|
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, IdFn, expected_result));
|
2012-09-08 01:47:54 +02:00
|
|
|
// If we join, do we get the expected value back?
|
|
|
|
void* result;
|
|
|
|
ASSERT_EQ(0, pthread_join(t, &result));
|
|
|
|
ASSERT_EQ(expected_result, result);
|
|
|
|
}
|
|
|
|
|
2013-02-12 17:40:24 +01:00
|
|
|
TEST(pthread, pthread_create_EAGAIN) {
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attributes));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
|
|
|
|
|
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, nullptr));
|
2013-02-12 17:40:24 +01:00
|
|
|
}
|
|
|
|
|
2012-09-08 01:47:54 +02:00
|
|
|
TEST(pthread, pthread_no_join_after_detach) {
|
2016-03-23 19:20:47 +01:00
|
|
|
SpinFunctionHelper spin_helper;
|
2014-12-02 02:41:04 +01:00
|
|
|
|
2012-09-08 01:47:54 +02:00
|
|
|
pthread_t t1;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr));
|
2012-09-08 01:47:54 +02:00
|
|
|
|
|
|
|
// After a pthread_detach...
|
|
|
|
ASSERT_EQ(0, pthread_detach(t1));
|
2012-10-26 12:06:43 +02:00
|
|
|
AssertDetached(t1, true);
|
2012-09-08 01:47:54 +02:00
|
|
|
|
|
|
|
// ...pthread_join should fail.
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(EINVAL, pthread_join(t1, nullptr));
|
2012-09-08 01:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_no_op_detach_after_join) {
|
2016-03-23 19:20:47 +01:00
|
|
|
SpinFunctionHelper spin_helper;
|
2012-10-26 12:06:43 +02:00
|
|
|
|
2012-09-08 01:47:54 +02:00
|
|
|
pthread_t t1;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr));
|
2012-09-08 01:47:54 +02:00
|
|
|
|
|
|
|
// If thread 2 is already waiting to join thread 1...
|
|
|
|
pthread_t t2;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t2, nullptr, JoinFn, reinterpret_cast<void*>(t1)));
|
2012-09-08 01:47:54 +02:00
|
|
|
|
2012-10-26 12:06:43 +02:00
|
|
|
sleep(1); // (Give t2 a chance to call pthread_join.)
|
|
|
|
|
2015-03-19 23:19:25 +01:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
ASSERT_EQ(EINVAL, pthread_detach(t1));
|
|
|
|
#else
|
2012-09-08 01:47:54 +02:00
|
|
|
ASSERT_EQ(0, pthread_detach(t1));
|
2015-03-19 23:19:25 +01:00
|
|
|
#endif
|
2012-10-26 12:06:43 +02:00
|
|
|
AssertDetached(t1, false);
|
|
|
|
|
2016-03-23 19:20:47 +01:00
|
|
|
spin_helper.UnSpin();
|
2012-09-08 01:47:54 +02:00
|
|
|
|
2012-10-26 12:06:43 +02:00
|
|
|
// ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
|
2012-09-08 01:47:54 +02:00
|
|
|
void* join_result;
|
|
|
|
ASSERT_EQ(0, pthread_join(t2, &join_result));
|
2013-10-03 01:59:05 +02:00
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
|
2012-09-08 01:47:54 +02:00
|
|
|
}
|
2012-10-29 18:19:44 +01:00
|
|
|
|
|
|
|
TEST(pthread, pthread_join_self) {
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), nullptr));
|
2012-10-29 18:19:44 +01:00
|
|
|
}
|
2012-11-02 00:33:29 +01:00
|
|
|
|
2013-11-16 02:40:18 +01:00
|
|
|
struct TestBug37410 {
|
|
|
|
pthread_t main_thread;
|
|
|
|
pthread_mutex_t mutex;
|
2012-11-02 00:33:29 +01:00
|
|
|
|
2013-11-16 02:40:18 +01:00
|
|
|
static void main() {
|
|
|
|
TestBug37410 data;
|
|
|
|
data.main_thread = pthread_self();
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_mutex_init(&data.mutex, nullptr));
|
2013-11-16 02:40:18 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
|
|
|
|
|
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
|
2013-11-16 02:40:18 +01:00
|
|
|
|
|
|
|
// Wait for the thread to be running...
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
|
|
|
|
|
|
|
|
// ...and exit.
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_exit(nullptr);
|
2013-11-16 02:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void* thread_fn(void* arg) {
|
|
|
|
TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
|
|
|
|
|
|
|
|
// Let the main thread know we're running.
|
|
|
|
pthread_mutex_unlock(&data->mutex);
|
|
|
|
|
|
|
|
// And wait for the main thread to exit.
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_join(data->main_thread, nullptr);
|
2013-11-16 02:40:18 +01:00
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2013-11-16 02:40:18 +01:00
|
|
|
}
|
|
|
|
};
|
2012-11-02 00:33:29 +01:00
|
|
|
|
2013-02-15 01:33:52 +01:00
|
|
|
// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
|
|
|
|
// run this test (which exits normally) in its own process.
|
2014-11-06 03:01:01 +01:00
|
|
|
|
|
|
|
class pthread_DeathTest : public BionicDeathTest {};
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_bug_37410) {
|
2012-11-02 00:33:29 +01:00
|
|
|
// http://code.google.com/p/android/issues/detail?id=37410
|
2013-11-16 02:40:18 +01:00
|
|
|
ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
|
2012-11-02 00:33:29 +01:00
|
|
|
}
|
2013-01-10 23:42:14 +01:00
|
|
|
|
|
|
|
static void* SignalHandlerFn(void* arg) {
|
2018-01-31 00:09:51 +01:00
|
|
|
sigset64_t wait_set;
|
|
|
|
sigfillset64(&wait_set);
|
|
|
|
return reinterpret_cast<void*>(sigwait64(&wait_set, reinterpret_cast<int*>(arg)));
|
2013-01-10 23:42:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_sigmask) {
|
2013-10-15 20:23:57 +02:00
|
|
|
// Check that SIGUSR1 isn't blocked.
|
|
|
|
sigset_t original_set;
|
|
|
|
sigemptyset(&original_set);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, nullptr, &original_set));
|
2013-10-15 20:23:57 +02:00
|
|
|
ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
|
|
|
|
|
2013-01-10 23:42:14 +01:00
|
|
|
// Block SIGUSR1.
|
|
|
|
sigset_t set;
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGUSR1);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, nullptr));
|
2013-01-10 23:42:14 +01:00
|
|
|
|
2013-10-15 20:23:57 +02:00
|
|
|
// Check that SIGUSR1 is blocked.
|
|
|
|
sigset_t final_set;
|
|
|
|
sigemptyset(&final_set);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, nullptr, &final_set));
|
2013-10-15 20:23:57 +02:00
|
|
|
ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
|
|
|
|
// ...and that sigprocmask agrees with pthread_sigmask.
|
|
|
|
sigemptyset(&final_set);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &final_set));
|
2013-10-15 20:23:57 +02:00
|
|
|
ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
|
|
|
|
|
2013-01-10 23:42:14 +01:00
|
|
|
// Spawn a thread that calls sigwait and tells us what it received.
|
|
|
|
pthread_t signal_thread;
|
|
|
|
int received_signal = -1;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&signal_thread, nullptr, SignalHandlerFn, &received_signal));
|
2013-01-10 23:42:14 +01:00
|
|
|
|
|
|
|
// Send that thread SIGUSR1.
|
|
|
|
pthread_kill(signal_thread, SIGUSR1);
|
|
|
|
|
|
|
|
// See what it got.
|
|
|
|
void* join_result;
|
|
|
|
ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
|
|
|
|
ASSERT_EQ(SIGUSR1, received_signal);
|
2013-10-03 01:59:05 +02:00
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
|
2013-10-15 20:23:57 +02:00
|
|
|
|
|
|
|
// Restore the original signal mask.
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, nullptr));
|
2013-01-10 23:42:14 +01:00
|
|
|
}
|
2013-02-12 01:36:48 +01:00
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
TEST(pthread, pthread_sigmask64_SIGTRMIN) {
|
|
|
|
// Check that SIGRTMIN isn't blocked.
|
|
|
|
sigset64_t original_set;
|
|
|
|
sigemptyset64(&original_set);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, nullptr, &original_set));
|
2018-01-31 00:09:51 +01:00
|
|
|
ASSERT_FALSE(sigismember64(&original_set, SIGRTMIN));
|
|
|
|
|
|
|
|
// Block SIGRTMIN.
|
|
|
|
sigset64_t set;
|
|
|
|
sigemptyset64(&set);
|
|
|
|
sigaddset64(&set, SIGRTMIN);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &set, nullptr));
|
2018-01-31 00:09:51 +01:00
|
|
|
|
|
|
|
// Check that SIGRTMIN is blocked.
|
|
|
|
sigset64_t final_set;
|
|
|
|
sigemptyset64(&final_set);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, nullptr, &final_set));
|
2018-01-31 00:09:51 +01:00
|
|
|
ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN));
|
|
|
|
// ...and that sigprocmask64 agrees with pthread_sigmask64.
|
|
|
|
sigemptyset64(&final_set);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, nullptr, &final_set));
|
2018-01-31 00:09:51 +01:00
|
|
|
ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN));
|
|
|
|
|
|
|
|
// Spawn a thread that calls sigwait64 and tells us what it received.
|
|
|
|
pthread_t signal_thread;
|
|
|
|
int received_signal = -1;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&signal_thread, nullptr, SignalHandlerFn, &received_signal));
|
2018-01-31 00:09:51 +01:00
|
|
|
|
|
|
|
// Send that thread SIGRTMIN.
|
|
|
|
pthread_kill(signal_thread, SIGRTMIN);
|
|
|
|
|
|
|
|
// See what it got.
|
|
|
|
void* join_result;
|
|
|
|
ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
|
|
|
|
ASSERT_EQ(SIGRTMIN, received_signal);
|
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
|
|
|
|
|
|
|
|
// Restore the original signal mask.
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &original_set, nullptr));
|
2018-01-31 00:09:51 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:20:47 +01:00
|
|
|
static void test_pthread_setname_np__pthread_getname_np(pthread_t t) {
|
|
|
|
ASSERT_EQ(0, pthread_setname_np(t, "short"));
|
|
|
|
char name[32];
|
|
|
|
ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
|
|
|
|
ASSERT_STREQ("short", name);
|
|
|
|
|
2015-04-25 19:05:24 +02:00
|
|
|
// The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL.
|
2016-03-23 19:20:47 +01:00
|
|
|
ASSERT_EQ(0, pthread_setname_np(t, "123456789012345"));
|
|
|
|
ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
|
|
|
|
ASSERT_STREQ("123456789012345", name);
|
|
|
|
|
|
|
|
ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456"));
|
|
|
|
|
|
|
|
// The passed-in buffer should be at least 16 bytes.
|
|
|
|
ASSERT_EQ(0, pthread_getname_np(t, name, 16));
|
|
|
|
ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15));
|
2013-02-12 17:40:24 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:20:47 +01:00
|
|
|
TEST(pthread, pthread_setname_np__pthread_getname_np__self) {
|
|
|
|
test_pthread_setname_np__pthread_getname_np(pthread_self());
|
2013-02-12 17:40:24 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:20:47 +01:00
|
|
|
TEST(pthread, pthread_setname_np__pthread_getname_np__other) {
|
|
|
|
SpinFunctionHelper spin_helper;
|
2014-12-02 02:41:04 +01:00
|
|
|
|
2016-03-23 19:20:47 +01:00
|
|
|
pthread_t t;
|
2016-04-11 21:43:05 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
|
|
|
|
test_pthread_setname_np__pthread_getname_np(t);
|
|
|
|
spin_helper.UnSpin();
|
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// http://b/28051133: a kernel misfeature means that you can't change the
|
|
|
|
// name of another thread if you've set PR_SET_DUMPABLE to 0.
|
|
|
|
TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) {
|
|
|
|
ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0)) << strerror(errno);
|
|
|
|
|
|
|
|
SpinFunctionHelper spin_helper;
|
|
|
|
|
|
|
|
pthread_t t;
|
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
|
2016-03-23 19:20:47 +01:00
|
|
|
test_pthread_setname_np__pthread_getname_np(t);
|
|
|
|
spin_helper.UnSpin();
|
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
2013-02-12 17:40:24 +01:00
|
|
|
}
|
|
|
|
|
Be more strict about using invalid `pthread_t`s.
Another release, another attempt to remove the global thread list.
But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.
This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".
Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.
(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)
We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.
Taking the affected functions one by one:
* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.
* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.
* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.
* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.
That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.
Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
2017-02-14 02:59:29 +01:00
|
|
|
TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) {
|
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
2017-02-21 22:15:20 +01:00
|
|
|
EXPECT_DEATH(pthread_setname_np(dead_thread, "short 3"), "invalid pthread_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_setname_np__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
|
|
|
EXPECT_EQ(ENOENT, pthread_setname_np(null_thread, "short 3"));
|
Be more strict about using invalid `pthread_t`s.
Another release, another attempt to remove the global thread list.
But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.
This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".
Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.
(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)
We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.
Taking the affected functions one by one:
* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.
* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.
* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.
* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.
That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.
Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
2017-02-14 02:59:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_getname_np__no_such_thread) {
|
2017-02-07 22:05:30 +01:00
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
|
|
|
char name[64];
|
2017-02-21 22:15:20 +01:00
|
|
|
EXPECT_DEATH(pthread_getname_np(dead_thread, name, sizeof(name)), "invalid pthread_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_getname_np__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
|
|
|
|
|
|
|
char name[64];
|
|
|
|
EXPECT_EQ(ENOENT, pthread_getname_np(null_thread, name, sizeof(name)));
|
2017-02-07 22:05:30 +01:00
|
|
|
}
|
|
|
|
|
2013-02-16 04:21:51 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_kill__invalid_signal) {
|
|
|
|
ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
|
|
|
|
}
|
|
|
|
|
2013-02-21 20:22:23 +01:00
|
|
|
static void pthread_kill__in_signal_handler_helper(int signal_number) {
|
|
|
|
static int count = 0;
|
|
|
|
ASSERT_EQ(SIGALRM, signal_number);
|
|
|
|
if (++count == 1) {
|
|
|
|
// Can we call pthread_kill from a signal handler?
|
|
|
|
ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_kill__in_signal_handler) {
|
2014-03-05 00:58:02 +01:00
|
|
|
ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
|
2013-02-21 20:22:23 +01:00
|
|
|
ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
|
|
|
|
}
|
|
|
|
|
2018-10-18 00:23:03 +02:00
|
|
|
TEST(pthread, pthread_kill__exited_thread) {
|
|
|
|
static std::promise<pid_t> tid_promise;
|
|
|
|
pthread_t thread;
|
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
|
|
|
[](void*) -> void* {
|
|
|
|
tid_promise.set_value(gettid());
|
|
|
|
return nullptr;
|
|
|
|
},
|
|
|
|
nullptr));
|
|
|
|
|
|
|
|
pid_t tid = tid_promise.get_future().get();
|
|
|
|
while (TEMP_FAILURE_RETRY(syscall(__NR_tgkill, getpid(), tid, 0)) != -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(ESRCH, errno);
|
|
|
|
|
|
|
|
ASSERT_EQ(ESRCH, pthread_kill(thread, 0));
|
|
|
|
}
|
|
|
|
|
Be more strict about using invalid `pthread_t`s.
Another release, another attempt to remove the global thread list.
But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.
This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".
Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.
(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)
We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.
Taking the affected functions one by one:
* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.
* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.
* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.
* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.
That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.
Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
2017-02-14 02:59:29 +01:00
|
|
|
TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) {
|
2017-02-02 03:41:38 +01:00
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
2017-02-21 22:15:20 +01:00
|
|
|
EXPECT_DEATH(pthread_detach(dead_thread), "invalid pthread_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_detach__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
|
|
|
EXPECT_EQ(ESRCH, pthread_detach(null_thread));
|
2017-02-02 03:41:38 +01:00
|
|
|
}
|
|
|
|
|
2013-08-15 23:51:16 +02:00
|
|
|
TEST(pthread, pthread_getcpuclockid__clock_gettime) {
|
2016-03-23 19:20:47 +01:00
|
|
|
SpinFunctionHelper spin_helper;
|
2014-12-02 02:41:04 +01:00
|
|
|
|
2013-08-15 23:51:16 +02:00
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
|
2013-08-15 23:51:16 +02:00
|
|
|
|
|
|
|
clockid_t c;
|
|
|
|
ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
|
|
|
|
timespec ts;
|
|
|
|
ASSERT_EQ(0, clock_gettime(c, &ts));
|
2016-03-23 19:20:47 +01:00
|
|
|
spin_helper.UnSpin();
|
2015-11-17 06:06:16 +01:00
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
2013-08-15 23:51:16 +02:00
|
|
|
}
|
|
|
|
|
Be more strict about using invalid `pthread_t`s.
Another release, another attempt to remove the global thread list.
But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.
This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".
Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.
(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)
We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.
Taking the affected functions one by one:
* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.
* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.
* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.
* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.
That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.
Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
2017-02-14 02:59:29 +01:00
|
|
|
TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) {
|
2017-02-07 22:05:30 +01:00
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
|
|
|
clockid_t c;
|
2017-02-21 22:15:20 +01:00
|
|
|
EXPECT_DEATH(pthread_getcpuclockid(dead_thread, &c), "invalid pthread_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_getcpuclockid__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
|
|
|
clockid_t c;
|
|
|
|
EXPECT_EQ(ESRCH, pthread_getcpuclockid(null_thread, &c));
|
2017-02-07 22:05:30 +01:00
|
|
|
}
|
|
|
|
|
Be more strict about using invalid `pthread_t`s.
Another release, another attempt to remove the global thread list.
But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.
This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".
Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.
(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)
We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.
Taking the affected functions one by one:
* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.
* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.
* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.
* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.
That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.
Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
2017-02-14 02:59:29 +01:00
|
|
|
TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) {
|
2017-02-07 22:05:30 +01:00
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
|
|
|
int policy;
|
|
|
|
sched_param param;
|
2017-02-21 22:15:20 +01:00
|
|
|
EXPECT_DEATH(pthread_getschedparam(dead_thread, &policy, ¶m), "invalid pthread_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_getschedparam__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
|
|
|
int policy;
|
|
|
|
sched_param param;
|
|
|
|
EXPECT_EQ(ESRCH, pthread_getschedparam(null_thread, &policy, ¶m));
|
2017-02-07 22:05:30 +01:00
|
|
|
}
|
|
|
|
|
Be more strict about using invalid `pthread_t`s.
Another release, another attempt to remove the global thread list.
But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.
This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".
Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.
(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)
We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.
Taking the affected functions one by one:
* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.
* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.
* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.
* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.
That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.
Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
2017-02-14 02:59:29 +01:00
|
|
|
TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) {
|
2017-02-07 22:05:30 +01:00
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
|
|
|
int policy = 0;
|
|
|
|
sched_param param;
|
2017-02-21 22:15:20 +01:00
|
|
|
EXPECT_DEATH(pthread_setschedparam(dead_thread, policy, ¶m), "invalid pthread_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_setschedparam__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
|
|
|
int policy = 0;
|
|
|
|
sched_param param;
|
|
|
|
EXPECT_EQ(ESRCH, pthread_setschedparam(null_thread, policy, ¶m));
|
2017-02-07 22:05:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-16 18:58:45 +02:00
|
|
|
TEST_F(pthread_DeathTest, pthread_setschedprio__no_such_thread) {
|
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
|
|
|
EXPECT_DEATH(pthread_setschedprio(dead_thread, 123), "invalid pthread_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_setschedprio__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
|
|
|
EXPECT_EQ(ESRCH, pthread_setschedprio(null_thread, 123));
|
|
|
|
}
|
|
|
|
|
Be more strict about using invalid `pthread_t`s.
Another release, another attempt to remove the global thread list.
But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.
This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".
Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.
(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)
We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.
Taking the affected functions one by one:
* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.
* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.
* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.
* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.
That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.
Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
2017-02-14 02:59:29 +01:00
|
|
|
TEST_F(pthread_DeathTest, pthread_join__no_such_thread) {
|
2017-02-02 03:41:38 +01:00
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_DEATH(pthread_join(dead_thread, nullptr), "invalid pthread_t");
|
2017-02-21 22:15:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_join__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(ESRCH, pthread_join(null_thread, nullptr));
|
2017-02-02 03:41:38 +01:00
|
|
|
}
|
|
|
|
|
Be more strict about using invalid `pthread_t`s.
Another release, another attempt to remove the global thread list.
But this time, let's admit that it's not going away. We can switch to using
a read/write lock for the global thread list, and to aborting rather than
quietly returning ESRCH if we're given an invalid pthread_t.
This change affects pthread_detach, pthread_getcpuclockid,
pthread_getschedparam/pthread_setschedparam, pthread_join, and pthread_kill:
instead of returning ESRCH when passed an invalid pthread_t, if you're
targeting O or above, they'll abort with the message "attempt to use
invalid pthread_t".
Note that this doesn't change behavior as much as you might think: the old
lookup only held the global thread list lock for the duration of the lookup,
so there was still a race between that and the dereference in the caller,
given that callers actually need the tid to pass to some syscall or other,
and sometimes update fields in the pthread_internal_t struct too.
(This patch replaces such users with calls to pthread_gettid_np, which
at least makes the TOCTOU window smaller.)
We can't check thread->tid against 0 to see whether a pthread_t is still
valid because a dead thread gets its thread struct unmapped along with its
stack, so the dereference isn't safe.
Taking the affected functions one by one:
* pthread_getcpuclockid and pthread_getschedparam/pthread_setschedparam
should be fine. Unsafe calls to those seem highly unlikely.
* Unsafe pthread_detach callers probably want to switch to
pthread_attr_setdetachstate instead, or using
pthread_detach(pthread_self()) from the new thread's start routine
rather than doing the detach in the parent.
* pthread_join calls should be safe anyway, because a joinable thread
won't actually exit and unmap until it's joined. If you're joining an
unjoinable thread, the fix is to stop marking it detached. If you're
joining an already-joined thread, you need to rethink your design.
* Unsafe pthread_kill calls aren't portably fixable. (And are obviously
inherently non-portable as-is.) The best alternative on Android is to
use pthread_gettid_np at some point that you know the thread to be
alive, and then call kill/tgkill directly.
That's still not completely safe because if you're too late, the tid
may have been reused, but then your code is inherently unsafe anyway.
Bug: http://b/19636317
Test: ran tests
Change-Id: I0372c4428e8a7f1c3af5c9334f5d9c25f2c73f21
2017-02-14 02:59:29 +01:00
|
|
|
TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) {
|
2017-02-02 03:41:38 +01:00
|
|
|
pthread_t dead_thread;
|
|
|
|
MakeDeadThread(dead_thread);
|
|
|
|
|
2017-02-21 22:15:20 +01:00
|
|
|
EXPECT_DEATH(pthread_kill(dead_thread, 0), "invalid pthread_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_kill__null_thread) {
|
|
|
|
pthread_t null_thread = 0;
|
|
|
|
EXPECT_EQ(ESRCH, pthread_kill(null_thread, 0));
|
2017-02-02 03:41:38 +01:00
|
|
|
}
|
|
|
|
|
2013-06-06 20:59:28 +02:00
|
|
|
TEST(pthread, pthread_join__multijoin) {
|
2016-03-23 19:20:47 +01:00
|
|
|
SpinFunctionHelper spin_helper;
|
2013-06-06 20:59:28 +02:00
|
|
|
|
|
|
|
pthread_t t1;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr));
|
2013-06-06 20:59:28 +02:00
|
|
|
|
|
|
|
pthread_t t2;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t2, nullptr, JoinFn, reinterpret_cast<void*>(t1)));
|
2013-06-06 20:59:28 +02:00
|
|
|
|
|
|
|
sleep(1); // (Give t2 a chance to call pthread_join.)
|
|
|
|
|
|
|
|
// Multiple joins to the same thread should fail.
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(EINVAL, pthread_join(t1, nullptr));
|
2013-06-06 20:59:28 +02:00
|
|
|
|
2016-03-23 19:20:47 +01:00
|
|
|
spin_helper.UnSpin();
|
2013-06-06 20:59:28 +02:00
|
|
|
|
|
|
|
// ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
|
|
|
|
void* join_result;
|
|
|
|
ASSERT_EQ(0, pthread_join(t2, &join_result));
|
2013-10-03 01:59:05 +02:00
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
|
2013-06-06 20:59:28 +02:00
|
|
|
}
|
2013-07-15 23:51:07 +02:00
|
|
|
|
2013-11-15 20:51:07 +01:00
|
|
|
TEST(pthread, pthread_join__race) {
|
|
|
|
// http://b/11693195 --- pthread_join could return before the thread had actually exited.
|
|
|
|
// If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
|
|
|
|
for (size_t i = 0; i < 1024; ++i) {
|
2015-11-17 06:06:16 +01:00
|
|
|
size_t stack_size = 640*1024;
|
2018-08-03 02:31:13 +02:00
|
|
|
void* stack = mmap(nullptr, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
|
2013-11-15 20:51:07 +01:00
|
|
|
|
|
|
|
pthread_attr_t a;
|
|
|
|
pthread_attr_init(&a);
|
|
|
|
pthread_attr_setstack(&a, stack, stack_size);
|
|
|
|
|
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, &a, IdFn, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
2013-11-15 20:51:07 +01:00
|
|
|
ASSERT_EQ(0, munmap(stack, stack_size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 23:51:07 +02:00
|
|
|
static void* GetActualGuardSizeFn(void* arg) {
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
pthread_getattr_np(pthread_self(), &attributes);
|
|
|
|
pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2013-07-15 23:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
|
|
|
|
size_t result;
|
|
|
|
pthread_t t;
|
|
|
|
pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_join(t, nullptr);
|
2013-07-15 23:51:07 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* GetActualStackSizeFn(void* arg) {
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
pthread_getattr_np(pthread_self(), &attributes);
|
|
|
|
pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2013-07-15 23:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t GetActualStackSize(const pthread_attr_t& attributes) {
|
|
|
|
size_t result;
|
|
|
|
pthread_t t;
|
|
|
|
pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_join(t, nullptr);
|
2013-07-15 23:51:07 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-28 02:01:57 +02:00
|
|
|
TEST(pthread, pthread_attr_setguardsize_tiny) {
|
2013-07-15 23:51:07 +02:00
|
|
|
pthread_attr_t attributes;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attributes));
|
|
|
|
|
|
|
|
// No such thing as too small: will be rounded up to one page by pthread_create.
|
|
|
|
ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
|
|
|
|
size_t guard_size;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
|
|
|
|
ASSERT_EQ(128U, guard_size);
|
|
|
|
ASSERT_EQ(4096U, GetActualGuardSize(attributes));
|
2017-06-28 02:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_setguardsize_reasonable) {
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attributes));
|
2013-07-15 23:51:07 +02:00
|
|
|
|
|
|
|
// Large enough and a multiple of the page size.
|
|
|
|
ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
|
2017-06-28 02:01:57 +02:00
|
|
|
size_t guard_size;
|
2013-07-15 23:51:07 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
|
|
|
|
ASSERT_EQ(32*1024U, guard_size);
|
2017-06-28 02:01:57 +02:00
|
|
|
ASSERT_EQ(32*1024U, GetActualGuardSize(attributes));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_setguardsize_needs_rounding) {
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attributes));
|
2013-07-15 23:51:07 +02:00
|
|
|
|
2017-06-28 02:01:57 +02:00
|
|
|
// Large enough but not a multiple of the page size.
|
2013-07-15 23:51:07 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
|
2017-06-28 02:01:57 +02:00
|
|
|
size_t guard_size;
|
2013-07-15 23:51:07 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
|
|
|
|
ASSERT_EQ(32*1024U + 1, guard_size);
|
2017-06-28 02:01:57 +02:00
|
|
|
ASSERT_EQ(36*1024U, GetActualGuardSize(attributes));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_setguardsize_enormous) {
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attributes));
|
|
|
|
|
|
|
|
// Larger than the stack itself. (Historically we mistakenly carved
|
|
|
|
// the guard out of the stack itself, rather than adding it after the
|
|
|
|
// end.)
|
|
|
|
ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024*1024));
|
|
|
|
size_t guard_size;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
|
|
|
|
ASSERT_EQ(32*1024*1024U, guard_size);
|
|
|
|
ASSERT_EQ(32*1024*1024U, GetActualGuardSize(attributes));
|
2013-07-15 23:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_setstacksize) {
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attributes));
|
|
|
|
|
|
|
|
// Get the default stack size.
|
|
|
|
size_t default_stack_size;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
|
|
|
|
|
|
|
|
// Too small.
|
|
|
|
ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
|
|
|
|
size_t stack_size;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
|
|
|
|
ASSERT_EQ(default_stack_size, stack_size);
|
|
|
|
ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
|
|
|
|
|
2015-01-08 21:32:42 +01:00
|
|
|
// Large enough and a multiple of the page size; may be rounded up by pthread_create.
|
2013-07-15 23:51:07 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
|
|
|
|
ASSERT_EQ(32*1024U, stack_size);
|
2015-01-08 21:32:42 +01:00
|
|
|
ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
|
2013-07-15 23:51:07 +02:00
|
|
|
|
2015-01-08 21:32:42 +01:00
|
|
|
// Large enough but not aligned; will be rounded up by pthread_create.
|
2013-07-15 23:51:07 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
|
|
|
|
ASSERT_EQ(32*1024U + 1, stack_size);
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__)
|
2015-01-08 21:32:42 +01:00
|
|
|
ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2013-07-15 23:51:07 +02:00
|
|
|
// glibc rounds down, in violation of POSIX. They document this in their BUGS section.
|
|
|
|
ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-07-15 23:51:07 +02:00
|
|
|
}
|
2013-10-30 22:40:09 +01:00
|
|
|
|
2015-03-17 22:22:09 +01:00
|
|
|
TEST(pthread, pthread_rwlockattr_smoke) {
|
|
|
|
pthread_rwlockattr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
|
|
|
|
|
|
|
|
int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED};
|
|
|
|
for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) {
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i]));
|
|
|
|
int pshared;
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared));
|
|
|
|
ASSERT_EQ(pshared_value_array[i], pshared);
|
|
|
|
}
|
|
|
|
|
|
|
|
int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP,
|
|
|
|
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP};
|
|
|
|
for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) {
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i]));
|
|
|
|
int kind;
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind));
|
|
|
|
ASSERT_EQ(kind_array[i], kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) {
|
|
|
|
pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER;
|
|
|
|
pthread_rwlock_t lock2;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_init(&lock2, nullptr));
|
2015-03-17 22:22:09 +01:00
|
|
|
ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1)));
|
|
|
|
}
|
|
|
|
|
2013-10-30 22:40:09 +01:00
|
|
|
TEST(pthread, pthread_rwlock_smoke) {
|
|
|
|
pthread_rwlock_t l;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_init(&l, nullptr));
|
2013-10-30 22:40:09 +01:00
|
|
|
|
2014-05-19 14:41:10 +02:00
|
|
|
// Single read lock
|
2013-10-30 22:40:09 +01:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
|
2014-05-19 14:41:10 +02:00
|
|
|
// Multiple read lock
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
|
|
|
|
// Write lock
|
2014-05-22 20:21:22 +02:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
2014-05-19 14:41:10 +02:00
|
|
|
|
|
|
|
// Try writer lock
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
|
|
|
|
ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
|
|
|
|
ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
|
|
|
|
// Try reader lock
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
|
|
|
|
ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
|
|
|
|
// Try writer lock after unlock
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
|
|
|
|
// EDEADLK in "read after write"
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
|
|
|
|
ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
|
|
|
|
// EDEADLK in "write after write"
|
2013-10-30 22:40:09 +01:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
|
2014-05-19 14:41:10 +02:00
|
|
|
ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
|
2013-10-30 22:40:09 +01:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_destroy(&l));
|
|
|
|
}
|
|
|
|
|
2015-02-12 02:04:36 +01:00
|
|
|
struct RwlockWakeupHelperArg {
|
|
|
|
pthread_rwlock_t lock;
|
|
|
|
enum Progress {
|
|
|
|
LOCK_INITIALIZED,
|
|
|
|
LOCK_WAITING,
|
|
|
|
LOCK_RELEASED,
|
2015-11-06 00:36:08 +01:00
|
|
|
LOCK_ACCESSED,
|
|
|
|
LOCK_TIMEDOUT,
|
2015-02-12 02:04:36 +01:00
|
|
|
};
|
|
|
|
std::atomic<Progress> progress;
|
2015-04-03 02:47:48 +02:00
|
|
|
std::atomic<pid_t> tid;
|
2015-11-06 00:36:08 +01:00
|
|
|
std::function<int (pthread_rwlock_t*)> trylock_function;
|
|
|
|
std::function<int (pthread_rwlock_t*)> lock_function;
|
|
|
|
std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function;
|
2018-03-05 23:14:44 +01:00
|
|
|
clockid_t clock;
|
2015-02-12 02:04:36 +01:00
|
|
|
};
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) {
|
2015-04-03 02:47:48 +02:00
|
|
|
arg->tid = gettid();
|
2015-02-12 02:04:36 +01:00
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
|
|
|
|
arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
|
|
|
|
ASSERT_EQ(0, arg->lock_function(&arg->lock));
|
2015-02-12 02:04:36 +01:00
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
|
|
|
|
|
|
|
|
arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
static void test_pthread_rwlock_reader_wakeup_writer(std::function<int (pthread_rwlock_t*)> lock_function) {
|
2015-02-12 02:04:36 +01:00
|
|
|
RwlockWakeupHelperArg wakeup_arg;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
|
2015-02-12 02:04:36 +01:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
|
|
|
|
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
|
2015-04-03 02:47:48 +02:00
|
|
|
wakeup_arg.tid = 0;
|
2018-02-21 00:40:02 +01:00
|
|
|
wakeup_arg.trylock_function = &pthread_rwlock_trywrlock;
|
2015-11-06 00:36:08 +01:00
|
|
|
wakeup_arg.lock_function = lock_function;
|
2015-02-12 02:04:36 +01:00
|
|
|
|
|
|
|
pthread_t thread;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
2015-11-06 00:36:08 +01:00
|
|
|
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
|
2015-04-03 02:47:48 +02:00
|
|
|
WaitUntilThreadSleep(wakeup_arg.tid);
|
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
|
|
|
|
|
2015-02-12 02:04:36 +01:00
|
|
|
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
2015-02-12 02:04:36 +01:00
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
|
|
|
|
test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock);
|
|
|
|
}
|
2015-02-12 02:04:36 +01:00
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) {
|
|
|
|
timespec ts;
|
|
|
|
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
|
|
|
|
ts.tv_sec += 1;
|
|
|
|
test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) {
|
|
|
|
return pthread_rwlock_timedwrlock(lock, &ts);
|
|
|
|
});
|
2015-02-12 02:04:36 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait_monotonic_np) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
timespec ts;
|
|
|
|
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
|
|
|
|
ts.tv_sec += 1;
|
|
|
|
test_pthread_rwlock_reader_wakeup_writer(
|
|
|
|
[&](pthread_rwlock_t* lock) { return pthread_rwlock_timedwrlock_monotonic_np(lock, &ts); });
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedwrlock_monotonic_np is "
|
|
|
|
"only supported on bionic";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) {
|
2015-02-12 02:04:36 +01:00
|
|
|
RwlockWakeupHelperArg wakeup_arg;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
|
2015-02-12 02:04:36 +01:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
|
|
|
|
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
|
2015-04-03 02:47:48 +02:00
|
|
|
wakeup_arg.tid = 0;
|
2018-02-21 00:40:02 +01:00
|
|
|
wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock;
|
2015-11-06 00:36:08 +01:00
|
|
|
wakeup_arg.lock_function = lock_function;
|
2015-02-12 02:04:36 +01:00
|
|
|
|
|
|
|
pthread_t thread;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
2015-11-06 00:36:08 +01:00
|
|
|
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
|
2015-04-03 02:47:48 +02:00
|
|
|
WaitUntilThreadSleep(wakeup_arg.tid);
|
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
|
|
|
|
|
2015-02-12 02:04:36 +01:00
|
|
|
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
2015-02-12 02:04:36 +01:00
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
|
|
|
|
test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) {
|
|
|
|
timespec ts;
|
|
|
|
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
|
|
|
|
ts.tv_sec += 1;
|
|
|
|
test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) {
|
|
|
|
return pthread_rwlock_timedrdlock(lock, &ts);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait_monotonic_np) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
timespec ts;
|
|
|
|
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
|
|
|
|
ts.tv_sec += 1;
|
|
|
|
test_pthread_rwlock_writer_wakeup_reader(
|
|
|
|
[&](pthread_rwlock_t* lock) { return pthread_rwlock_timedrdlock_monotonic_np(lock, &ts); });
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedrdlock_monotonic_np is "
|
|
|
|
"only supported on bionic";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) {
|
|
|
|
arg->tid = gettid();
|
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
|
|
|
|
arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
|
|
|
|
|
|
|
|
ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
|
|
|
|
|
|
|
|
timespec ts;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(0, clock_gettime(arg->clock, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
|
|
|
|
ts.tv_nsec = -1;
|
|
|
|
ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
|
|
|
|
ts.tv_nsec = NS_PER_S;
|
|
|
|
ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
|
|
|
|
ts.tv_nsec = NS_PER_S - 1;
|
|
|
|
ts.tv_sec = -1;
|
|
|
|
ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(0, clock_gettime(arg->clock, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_sec += 1;
|
|
|
|
ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
|
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, arg->progress);
|
|
|
|
arg->progress = RwlockWakeupHelperArg::LOCK_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
static void pthread_rwlock_timedrdlock_timeout_helper(
|
|
|
|
clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) {
|
2015-11-06 00:36:08 +01:00
|
|
|
RwlockWakeupHelperArg wakeup_arg;
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
|
|
|
|
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
|
|
|
|
wakeup_arg.tid = 0;
|
2018-02-21 00:40:02 +01:00
|
|
|
wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock;
|
2018-03-05 23:14:44 +01:00
|
|
|
wakeup_arg.timed_lock_function = lock_function;
|
|
|
|
wakeup_arg.clock = clock;
|
2015-11-06 00:36:08 +01:00
|
|
|
|
|
|
|
pthread_t thread;
|
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
|
|
|
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
|
|
|
|
WaitUntilThreadSleep(wakeup_arg.tid);
|
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
|
|
|
|
}
|
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
TEST(pthread, pthread_rwlock_timedrdlock_timeout) {
|
|
|
|
pthread_rwlock_timedrdlock_timeout_helper(CLOCK_REALTIME, pthread_rwlock_timedrdlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_rwlock_timedrdlock_monotonic_np_timeout) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
pthread_rwlock_timedrdlock_timeout_helper(CLOCK_MONOTONIC,
|
|
|
|
pthread_rwlock_timedrdlock_monotonic_np);
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedrdlock_monotonic_np is "
|
|
|
|
"only supported on bionic";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pthread_rwlock_timedwrlock_timeout_helper(
|
|
|
|
clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) {
|
2015-11-06 00:36:08 +01:00
|
|
|
RwlockWakeupHelperArg wakeup_arg;
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
|
|
|
|
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
|
|
|
|
wakeup_arg.tid = 0;
|
2018-02-21 00:40:02 +01:00
|
|
|
wakeup_arg.trylock_function = &pthread_rwlock_trywrlock;
|
2018-03-05 23:14:44 +01:00
|
|
|
wakeup_arg.timed_lock_function = lock_function;
|
|
|
|
wakeup_arg.clock = clock;
|
2015-11-06 00:36:08 +01:00
|
|
|
|
|
|
|
pthread_t thread;
|
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
|
|
|
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
|
|
|
|
WaitUntilThreadSleep(wakeup_arg.tid);
|
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
|
|
|
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
|
|
|
|
}
|
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
TEST(pthread, pthread_rwlock_timedwrlock_timeout) {
|
|
|
|
pthread_rwlock_timedwrlock_timeout_helper(CLOCK_REALTIME, pthread_rwlock_timedwrlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_rwlock_timedwrlock_monotonic_np_timeout) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
pthread_rwlock_timedwrlock_timeout_helper(CLOCK_MONOTONIC,
|
|
|
|
pthread_rwlock_timedwrlock_monotonic_np);
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing since pthread_rwlock_timedwrlock_monotonic_np is "
|
|
|
|
"only supported on bionic";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:22:09 +01:00
|
|
|
class RwlockKindTestHelper {
|
|
|
|
private:
|
|
|
|
struct ThreadArg {
|
|
|
|
RwlockKindTestHelper* helper;
|
|
|
|
std::atomic<pid_t>& tid;
|
|
|
|
|
|
|
|
ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid)
|
|
|
|
: helper(helper), tid(tid) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
pthread_rwlock_t lock;
|
|
|
|
|
|
|
|
public:
|
2016-05-03 21:08:05 +02:00
|
|
|
explicit RwlockKindTestHelper(int kind_type) {
|
2015-03-17 22:22:09 +01:00
|
|
|
InitRwlock(kind_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
~RwlockKindTestHelper() {
|
|
|
|
DestroyRwlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) {
|
|
|
|
tid = 0;
|
|
|
|
ThreadArg* arg = new ThreadArg(this, tid);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
2015-03-17 22:22:09 +01:00
|
|
|
reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) {
|
|
|
|
tid = 0;
|
|
|
|
ThreadArg* arg = new ThreadArg(this, tid);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
2015-03-17 22:22:09 +01:00
|
|
|
reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void InitRwlock(int kind_type) {
|
|
|
|
pthread_rwlockattr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr));
|
|
|
|
ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyRwlock() {
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_destroy(&lock));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WriterThreadFn(ThreadArg* arg) {
|
|
|
|
arg->tid = gettid();
|
|
|
|
|
|
|
|
RwlockKindTestHelper* helper = arg->helper;
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
|
|
|
|
delete arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReaderThreadFn(ThreadArg* arg) {
|
|
|
|
arg->tid = gettid();
|
|
|
|
|
|
|
|
RwlockKindTestHelper* helper = arg->helper;
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
|
|
|
|
delete arg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) {
|
|
|
|
RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP);
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
|
|
|
|
|
|
|
|
pthread_t writer_thread;
|
|
|
|
std::atomic<pid_t> writer_tid;
|
|
|
|
helper.CreateWriterThread(writer_thread, writer_tid);
|
|
|
|
WaitUntilThreadSleep(writer_tid);
|
|
|
|
|
|
|
|
pthread_t reader_thread;
|
|
|
|
std::atomic<pid_t> reader_tid;
|
|
|
|
helper.CreateReaderThread(reader_thread, reader_tid);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(reader_thread, nullptr));
|
2015-03-17 22:22:09 +01:00
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(writer_thread, nullptr));
|
2015-03-17 22:22:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) {
|
|
|
|
RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
|
|
|
|
|
|
|
|
pthread_t writer_thread;
|
|
|
|
std::atomic<pid_t> writer_tid;
|
|
|
|
helper.CreateWriterThread(writer_thread, writer_tid);
|
|
|
|
WaitUntilThreadSleep(writer_tid);
|
|
|
|
|
|
|
|
pthread_t reader_thread;
|
|
|
|
std::atomic<pid_t> reader_tid;
|
|
|
|
helper.CreateReaderThread(reader_thread, reader_tid);
|
|
|
|
WaitUntilThreadSleep(reader_tid);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(writer_thread, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_join(reader_thread, nullptr));
|
2015-03-17 22:22:09 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 19:02:03 +02:00
|
|
|
static int g_once_fn_call_count = 0;
|
2013-10-30 22:40:09 +01:00
|
|
|
static void OnceFn() {
|
2014-05-14 19:02:03 +02:00
|
|
|
++g_once_fn_call_count;
|
2013-10-30 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_once_smoke) {
|
|
|
|
pthread_once_t once_control = PTHREAD_ONCE_INIT;
|
|
|
|
ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
|
|
|
|
ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
|
2014-05-14 19:02:03 +02:00
|
|
|
ASSERT_EQ(1, g_once_fn_call_count);
|
2013-10-30 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 20:46:08 +02:00
|
|
|
static std::string pthread_once_1934122_result = "";
|
|
|
|
|
|
|
|
static void Routine2() {
|
|
|
|
pthread_once_1934122_result += "2";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Routine1() {
|
|
|
|
pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
|
|
|
|
pthread_once_1934122_result += "1";
|
|
|
|
pthread_once(&once_control_2, &Routine2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_once_1934122) {
|
|
|
|
// Very old versions of Android couldn't call pthread_once from a
|
|
|
|
// pthread_once init routine. http://b/1934122.
|
|
|
|
pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
|
|
|
|
ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
|
|
|
|
ASSERT_EQ("12", pthread_once_1934122_result);
|
|
|
|
}
|
|
|
|
|
2014-05-14 19:02:03 +02:00
|
|
|
static int g_atfork_prepare_calls = 0;
|
2014-11-21 05:47:02 +01:00
|
|
|
static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; }
|
|
|
|
static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; }
|
2014-05-14 19:02:03 +02:00
|
|
|
static int g_atfork_parent_calls = 0;
|
2014-11-21 05:47:02 +01:00
|
|
|
static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; }
|
|
|
|
static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; }
|
2014-05-14 19:02:03 +02:00
|
|
|
static int g_atfork_child_calls = 0;
|
2014-11-21 05:47:02 +01:00
|
|
|
static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; }
|
|
|
|
static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; }
|
2013-10-30 22:40:09 +01:00
|
|
|
|
2014-11-21 01:53:47 +01:00
|
|
|
TEST(pthread, pthread_atfork_smoke) {
|
2015-03-16 22:15:46 +01:00
|
|
|
ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
|
|
|
|
ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
|
2013-10-30 22:40:09 +01:00
|
|
|
|
2016-01-26 22:04:57 +01:00
|
|
|
pid_t pid = fork();
|
2015-03-16 22:15:46 +01:00
|
|
|
ASSERT_NE(-1, pid) << strerror(errno);
|
2013-10-30 22:40:09 +01:00
|
|
|
|
2015-03-16 22:15:46 +01:00
|
|
|
// Child and parent calls are made in the order they were registered.
|
|
|
|
if (pid == 0) {
|
2014-11-21 05:47:02 +01:00
|
|
|
ASSERT_EQ(12, g_atfork_child_calls);
|
2015-03-16 22:15:46 +01:00
|
|
|
_exit(0);
|
|
|
|
}
|
2014-11-21 05:47:02 +01:00
|
|
|
ASSERT_EQ(12, g_atfork_parent_calls);
|
2013-10-30 22:40:09 +01:00
|
|
|
|
2015-03-16 22:15:46 +01:00
|
|
|
// Prepare calls are made in the reverse order.
|
2014-11-21 05:47:02 +01:00
|
|
|
ASSERT_EQ(21, g_atfork_prepare_calls);
|
2016-01-26 22:04:57 +01:00
|
|
|
AssertChildExited(pid, 0);
|
2014-11-21 05:47:02 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 22:40:09 +01:00
|
|
|
TEST(pthread, pthread_attr_getscope) {
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
|
|
|
|
int scope;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
|
|
|
|
ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
|
|
|
|
}
|
2014-03-03 16:38:51 +01:00
|
|
|
|
|
|
|
TEST(pthread, pthread_condattr_init) {
|
|
|
|
pthread_condattr_t attr;
|
|
|
|
pthread_condattr_init(&attr);
|
|
|
|
|
|
|
|
clockid_t clock;
|
|
|
|
ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
|
|
|
|
ASSERT_EQ(CLOCK_REALTIME, clock);
|
|
|
|
|
|
|
|
int pshared;
|
|
|
|
ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
|
|
|
|
ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_condattr_setclock) {
|
|
|
|
pthread_condattr_t attr;
|
|
|
|
pthread_condattr_init(&attr);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
|
|
|
|
clockid_t clock;
|
|
|
|
ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
|
|
|
|
ASSERT_EQ(CLOCK_REALTIME, clock);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
|
|
|
|
ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
|
|
|
|
ASSERT_EQ(CLOCK_MONOTONIC, clock);
|
|
|
|
|
|
|
|
ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
|
2015-03-14 04:30:00 +01:00
|
|
|
#if defined(__BIONIC__)
|
2014-03-03 16:38:51 +01:00
|
|
|
pthread_condattr_t attr;
|
|
|
|
pthread_condattr_init(&attr);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
|
|
|
|
ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
|
|
|
|
|
|
|
|
pthread_cond_t cond_var;
|
|
|
|
ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_cond_signal(&cond_var));
|
|
|
|
ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
|
|
|
|
|
2015-03-14 04:30:00 +01:00
|
|
|
attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
|
2014-03-03 16:38:51 +01:00
|
|
|
clockid_t clock;
|
|
|
|
ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
|
|
|
|
ASSERT_EQ(CLOCK_MONOTONIC, clock);
|
|
|
|
int pshared;
|
|
|
|
ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
|
|
|
|
ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
|
2015-03-14 04:30:00 +01:00
|
|
|
#else // !defined(__BIONIC__)
|
|
|
|
GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
|
|
|
|
#endif // !defined(__BIONIC__)
|
|
|
|
}
|
|
|
|
|
|
|
|
class pthread_CondWakeupTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t cond;
|
|
|
|
|
|
|
|
enum Progress {
|
|
|
|
INITIALIZED,
|
|
|
|
WAITING,
|
|
|
|
SIGNALED,
|
|
|
|
FINISHED,
|
|
|
|
};
|
|
|
|
std::atomic<Progress> progress;
|
|
|
|
pthread_t thread;
|
2015-11-06 00:36:08 +01:00
|
|
|
std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function;
|
2015-03-14 04:30:00 +01:00
|
|
|
|
|
|
|
protected:
|
2015-11-06 00:36:08 +01:00
|
|
|
void SetUp() override {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
|
2015-03-14 04:30:00 +01:00
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
void InitCond(clockid_t clock=CLOCK_REALTIME) {
|
|
|
|
pthread_condattr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_condattr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_condattr_setclock(&attr, clock));
|
|
|
|
ASSERT_EQ(0, pthread_cond_init(&cond, &attr));
|
|
|
|
ASSERT_EQ(0, pthread_condattr_destroy(&attr));
|
2015-03-14 04:30:00 +01:00
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
void StartWaitingThread(std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) {
|
|
|
|
progress = INITIALIZED;
|
|
|
|
this->wait_function = wait_function;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
|
2015-11-06 00:36:08 +01:00
|
|
|
while (progress != WAITING) {
|
2015-03-14 04:30:00 +01:00
|
|
|
usleep(5000);
|
|
|
|
}
|
|
|
|
usleep(5000);
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
void TearDown() override {
|
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
|
|
|
ASSERT_EQ(FINISHED, progress);
|
|
|
|
ASSERT_EQ(0, pthread_cond_destroy(&cond));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:30:00 +01:00
|
|
|
private:
|
|
|
|
static void WaitThreadFn(pthread_CondWakeupTest* test) {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
|
|
|
|
test->progress = WAITING;
|
|
|
|
while (test->progress == WAITING) {
|
2015-11-06 00:36:08 +01:00
|
|
|
ASSERT_EQ(0, test->wait_function(&test->cond, &test->mutex));
|
2015-03-14 04:30:00 +01:00
|
|
|
}
|
|
|
|
ASSERT_EQ(SIGNALED, test->progress);
|
|
|
|
test->progress = FINISHED;
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
TEST_F(pthread_CondWakeupTest, signal_wait) {
|
|
|
|
InitCond();
|
|
|
|
StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
|
|
|
|
return pthread_cond_wait(cond, mutex);
|
|
|
|
});
|
2015-03-14 04:30:00 +01:00
|
|
|
progress = SIGNALED;
|
2015-11-06 00:36:08 +01:00
|
|
|
ASSERT_EQ(0, pthread_cond_signal(&cond));
|
2015-03-14 04:30:00 +01:00
|
|
|
}
|
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
TEST_F(pthread_CondWakeupTest, broadcast_wait) {
|
|
|
|
InitCond();
|
|
|
|
StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
|
|
|
|
return pthread_cond_wait(cond, mutex);
|
|
|
|
});
|
2015-03-14 04:30:00 +01:00
|
|
|
progress = SIGNALED;
|
2015-11-06 00:36:08 +01:00
|
|
|
ASSERT_EQ(0, pthread_cond_broadcast(&cond));
|
2014-03-03 16:38:51 +01:00
|
|
|
}
|
2014-03-04 01:42:47 +01:00
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) {
|
|
|
|
InitCond(CLOCK_REALTIME);
|
2014-03-04 01:42:47 +01:00
|
|
|
timespec ts;
|
|
|
|
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_sec += 1;
|
|
|
|
StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
|
|
|
|
return pthread_cond_timedwait(cond, mutex, &ts);
|
|
|
|
});
|
|
|
|
progress = SIGNALED;
|
|
|
|
ASSERT_EQ(0, pthread_cond_signal(&cond));
|
|
|
|
}
|
2014-03-04 01:42:47 +01:00
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) {
|
|
|
|
InitCond(CLOCK_MONOTONIC);
|
|
|
|
timespec ts;
|
|
|
|
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
|
|
|
|
ts.tv_sec += 1;
|
|
|
|
StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
|
|
|
|
return pthread_cond_timedwait(cond, mutex, &ts);
|
|
|
|
});
|
|
|
|
progress = SIGNALED;
|
|
|
|
ASSERT_EQ(0, pthread_cond_signal(&cond));
|
|
|
|
}
|
2014-03-04 01:42:47 +01:00
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC_np) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
InitCond(CLOCK_REALTIME);
|
|
|
|
timespec ts;
|
|
|
|
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
|
|
|
|
ts.tv_sec += 1;
|
|
|
|
StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
|
|
|
|
return pthread_cond_timedwait_monotonic_np(cond, mutex, &ts);
|
|
|
|
});
|
|
|
|
progress = SIGNALED;
|
|
|
|
ASSERT_EQ(0, pthread_cond_signal(&cond));
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing since pthread_cond_timedwait_monotonic_np is only "
|
|
|
|
"supported on bionic";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pthread_cond_timedwait_timeout_helper(clockid_t clock,
|
|
|
|
int (*wait_function)(pthread_cond_t* __cond,
|
|
|
|
pthread_mutex_t* __mutex,
|
|
|
|
const timespec* __timeout)) {
|
2015-11-06 00:36:08 +01:00
|
|
|
pthread_mutex_t mutex;
|
|
|
|
ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
|
|
|
|
pthread_cond_t cond;
|
|
|
|
ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&mutex));
|
2018-03-05 23:14:44 +01:00
|
|
|
|
2015-11-06 00:36:08 +01:00
|
|
|
timespec ts;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(0, clock_gettime(clock, &ts));
|
|
|
|
ASSERT_EQ(ETIMEDOUT, wait_function(&cond, &mutex, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_nsec = -1;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(EINVAL, wait_function(&cond, &mutex, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_nsec = NS_PER_S;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(EINVAL, wait_function(&cond, &mutex, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_nsec = NS_PER_S - 1;
|
|
|
|
ts.tv_sec = -1;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(ETIMEDOUT, wait_function(&cond, &mutex, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&mutex));
|
2014-03-04 01:42:47 +01:00
|
|
|
}
|
2014-08-26 02:26:50 +02:00
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
TEST(pthread, pthread_cond_timedwait_timeout) {
|
|
|
|
pthread_cond_timedwait_timeout_helper(CLOCK_REALTIME, pthread_cond_timedwait);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_cond_timedwait_monotonic_np_timeout) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
pthread_cond_timedwait_timeout_helper(CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np);
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing since pthread_cond_timedwait_monotonic_np is only "
|
|
|
|
"supported on bionic";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
|
|
|
|
2014-08-26 02:26:50 +02:00
|
|
|
TEST(pthread, pthread_attr_getstack__main_thread) {
|
|
|
|
// This test is only meaningful for the main thread, so make sure we're running on it!
|
|
|
|
ASSERT_EQ(getpid(), syscall(__NR_gettid));
|
|
|
|
|
|
|
|
// Get the main thread's attributes.
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
|
|
|
|
|
|
|
|
// Check that we correctly report that the main thread has no guard page.
|
|
|
|
size_t guard_size;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
|
|
|
|
ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
|
|
|
|
|
|
|
|
// Get the stack base and the stack size (both ways).
|
|
|
|
void* stack_base;
|
|
|
|
size_t stack_size;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
|
|
|
|
size_t stack_size2;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
|
|
|
|
|
|
|
|
// The two methods of asking for the stack size should agree.
|
|
|
|
EXPECT_EQ(stack_size, stack_size2);
|
|
|
|
|
2015-05-20 00:09:23 +02:00
|
|
|
#if defined(__BIONIC__)
|
2018-01-30 13:24:28 +01:00
|
|
|
// Find stack in /proc/self/maps using a pointer to the stack.
|
|
|
|
//
|
|
|
|
// We do not use "[stack]" label because in native-bridge environment it is not
|
|
|
|
// guaranteed to point to the right stack. A native bridge implementation may
|
|
|
|
// keep separate stack for the guest code.
|
2018-08-03 02:31:13 +02:00
|
|
|
void* maps_stack_hi = nullptr;
|
2015-09-23 01:40:14 +02:00
|
|
|
std::vector<map_record> maps;
|
|
|
|
ASSERT_TRUE(Maps::parse_maps(&maps));
|
2018-01-30 13:24:28 +01:00
|
|
|
uintptr_t stack_address = reinterpret_cast<uintptr_t>(&maps_stack_hi);
|
2015-10-03 03:25:19 +02:00
|
|
|
for (const auto& map : maps) {
|
2018-01-30 13:24:28 +01:00
|
|
|
if (map.addr_start <= stack_address && map.addr_end > stack_address){
|
2015-09-23 01:40:14 +02:00
|
|
|
maps_stack_hi = reinterpret_cast<void*>(map.addr_end);
|
2014-08-26 02:26:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:24:28 +01:00
|
|
|
// The high address of the /proc/self/maps stack region should equal stack_base + stack_size.
|
2015-05-20 00:09:23 +02:00
|
|
|
// Remember that the stack grows down (and is mapped in on demand), so the low address of the
|
|
|
|
// region isn't very interesting.
|
|
|
|
EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
|
|
|
|
|
2014-08-28 00:32:01 +02:00
|
|
|
// The stack size should correspond to RLIMIT_STACK.
|
|
|
|
rlimit rl;
|
|
|
|
ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
|
2014-09-05 01:09:25 +02:00
|
|
|
uint64_t original_rlim_cur = rl.rlim_cur;
|
|
|
|
if (rl.rlim_cur == RLIM_INFINITY) {
|
|
|
|
rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
|
|
|
|
}
|
2014-08-28 00:32:01 +02:00
|
|
|
EXPECT_EQ(rl.rlim_cur, stack_size);
|
2014-08-26 02:26:50 +02:00
|
|
|
|
2017-04-06 01:20:29 +02:00
|
|
|
auto guard = android::base::make_scope_guard([&rl, original_rlim_cur]() {
|
2014-09-05 01:09:25 +02:00
|
|
|
rl.rlim_cur = original_rlim_cur;
|
|
|
|
ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
|
|
|
|
});
|
|
|
|
|
2014-08-26 02:26:50 +02:00
|
|
|
//
|
2014-08-28 00:32:01 +02:00
|
|
|
// What if RLIMIT_STACK is smaller than the stack's current extent?
|
2014-08-26 02:26:50 +02:00
|
|
|
//
|
|
|
|
rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
|
|
|
|
rl.rlim_max = RLIM_INFINITY;
|
|
|
|
ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
|
|
|
|
|
|
|
|
EXPECT_EQ(stack_size, stack_size2);
|
|
|
|
ASSERT_EQ(1024U, stack_size);
|
|
|
|
|
|
|
|
//
|
2014-08-28 00:32:01 +02:00
|
|
|
// What if RLIMIT_STACK isn't a whole number of pages?
|
2014-08-26 02:26:50 +02:00
|
|
|
//
|
|
|
|
rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
|
|
|
|
rl.rlim_max = RLIM_INFINITY;
|
|
|
|
ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
|
|
|
|
|
|
|
|
EXPECT_EQ(stack_size, stack_size2);
|
|
|
|
ASSERT_EQ(6666U, stack_size);
|
2015-05-20 00:09:23 +02:00
|
|
|
#endif
|
2014-08-26 02:26:50 +02:00
|
|
|
}
|
2014-09-12 23:43:07 +02:00
|
|
|
|
2015-09-11 07:31:36 +02:00
|
|
|
struct GetStackSignalHandlerArg {
|
|
|
|
volatile bool done;
|
2016-06-02 23:40:09 +02:00
|
|
|
void* signal_stack_base;
|
|
|
|
size_t signal_stack_size;
|
2015-09-11 07:31:36 +02:00
|
|
|
void* main_stack_base;
|
|
|
|
size_t main_stack_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GetStackSignalHandlerArg getstack_signal_handler_arg;
|
|
|
|
|
|
|
|
static void getstack_signal_handler(int sig) {
|
|
|
|
ASSERT_EQ(SIGUSR1, sig);
|
|
|
|
// Use sleep() to make current thread be switched out by the kernel to provoke the error.
|
|
|
|
sleep(1);
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
|
|
|
|
void* stack_base;
|
|
|
|
size_t stack_size;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size));
|
2016-06-02 23:40:09 +02:00
|
|
|
|
|
|
|
// Verify if the stack used by the signal handler is the alternate stack just registered.
|
|
|
|
ASSERT_LE(getstack_signal_handler_arg.signal_stack_base, &attr);
|
|
|
|
ASSERT_LT(static_cast<void*>(&attr),
|
|
|
|
static_cast<char*>(getstack_signal_handler_arg.signal_stack_base) +
|
|
|
|
getstack_signal_handler_arg.signal_stack_size);
|
|
|
|
|
|
|
|
// Verify if the main thread's stack got in the signal handler is correct.
|
|
|
|
ASSERT_EQ(getstack_signal_handler_arg.main_stack_base, stack_base);
|
|
|
|
ASSERT_LE(getstack_signal_handler_arg.main_stack_size, stack_size);
|
|
|
|
|
2015-09-11 07:31:36 +02:00
|
|
|
getstack_signal_handler_arg.done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The previous code obtained the main thread's stack by reading the entry in
|
|
|
|
// /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel
|
|
|
|
// relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel
|
|
|
|
// switches a process while the main thread is in an alternate stack, then the kernel will label
|
|
|
|
// the wrong map with [stack]. This test verifies that when the above situation happens, the main
|
|
|
|
// thread's stack is found correctly.
|
|
|
|
TEST(pthread, pthread_attr_getstack_in_signal_handler) {
|
2016-03-08 02:44:58 +01:00
|
|
|
// This test is only meaningful for the main thread, so make sure we're running on it!
|
|
|
|
ASSERT_EQ(getpid(), syscall(__NR_gettid));
|
|
|
|
|
2015-09-11 07:31:36 +02:00
|
|
|
const size_t sig_stack_size = 16 * 1024;
|
2018-08-03 02:31:13 +02:00
|
|
|
void* sig_stack = mmap(nullptr, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
|
2015-09-11 07:31:36 +02:00
|
|
|
-1, 0);
|
|
|
|
ASSERT_NE(MAP_FAILED, sig_stack);
|
|
|
|
stack_t ss;
|
|
|
|
ss.ss_sp = sig_stack;
|
|
|
|
ss.ss_size = sig_stack_size;
|
|
|
|
ss.ss_flags = 0;
|
|
|
|
stack_t oss;
|
|
|
|
ASSERT_EQ(0, sigaltstack(&ss, &oss));
|
|
|
|
|
2016-03-08 02:44:58 +01:00
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
|
|
|
|
void* main_stack_base;
|
|
|
|
size_t main_stack_size;
|
|
|
|
ASSERT_EQ(0, pthread_attr_getstack(&attr, &main_stack_base, &main_stack_size));
|
|
|
|
|
2015-09-11 07:31:36 +02:00
|
|
|
ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK);
|
|
|
|
getstack_signal_handler_arg.done = false;
|
2016-06-02 23:40:09 +02:00
|
|
|
getstack_signal_handler_arg.signal_stack_base = sig_stack;
|
|
|
|
getstack_signal_handler_arg.signal_stack_size = sig_stack_size;
|
|
|
|
getstack_signal_handler_arg.main_stack_base = main_stack_base;
|
|
|
|
getstack_signal_handler_arg.main_stack_size = main_stack_size;
|
2015-09-11 07:31:36 +02:00
|
|
|
kill(getpid(), SIGUSR1);
|
|
|
|
ASSERT_EQ(true, getstack_signal_handler_arg.done);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, sigaltstack(&oss, nullptr));
|
|
|
|
ASSERT_EQ(0, munmap(sig_stack, sig_stack_size));
|
|
|
|
}
|
|
|
|
|
2015-01-08 21:32:42 +01:00
|
|
|
static void pthread_attr_getstack_18908062_helper(void*) {
|
|
|
|
char local_variable;
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
pthread_getattr_np(pthread_self(), &attributes);
|
|
|
|
void* stack_base;
|
|
|
|
size_t stack_size;
|
|
|
|
pthread_attr_getstack(&attributes, &stack_base, &stack_size);
|
|
|
|
|
|
|
|
// Test whether &local_variable is in [stack_base, stack_base + stack_size).
|
|
|
|
ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
|
|
|
|
ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether something on stack is in the range of
|
|
|
|
// [stack_base, stack_base + stack_size). see b/18908062.
|
|
|
|
TEST(pthread, pthread_attr_getstack_18908062) {
|
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr,
|
2015-01-08 21:32:42 +01:00
|
|
|
reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
|
2018-08-03 02:31:13 +02:00
|
|
|
nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
2015-01-08 21:32:42 +01:00
|
|
|
}
|
|
|
|
|
2014-09-12 23:43:07 +02:00
|
|
|
#if defined(__BIONIC__)
|
2015-11-11 22:32:28 +01:00
|
|
|
static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2014-09-12 23:43:07 +02:00
|
|
|
static void* pthread_gettid_np_helper(void* arg) {
|
|
|
|
*reinterpret_cast<pid_t*>(arg) = gettid();
|
2015-11-11 22:32:28 +01:00
|
|
|
|
|
|
|
// Wait for our parent to call pthread_gettid_np on us before exiting.
|
|
|
|
pthread_mutex_lock(&pthread_gettid_np_mutex);
|
|
|
|
pthread_mutex_unlock(&pthread_gettid_np_mutex);
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2014-09-12 23:43:07 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST(pthread, pthread_gettid_np) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
|
|
|
|
|
2015-11-11 22:32:28 +01:00
|
|
|
// Ensure the other thread doesn't exit until after we've called
|
|
|
|
// pthread_gettid_np on it.
|
|
|
|
pthread_mutex_lock(&pthread_gettid_np_mutex);
|
|
|
|
|
2014-09-12 23:43:07 +02:00
|
|
|
pid_t t_gettid_result;
|
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_create(&t, nullptr, pthread_gettid_np_helper, &t_gettid_result);
|
2014-09-12 23:43:07 +02:00
|
|
|
|
|
|
|
pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
|
|
|
|
|
2015-11-11 22:32:28 +01:00
|
|
|
// Release the other thread and wait for it to exit.
|
|
|
|
pthread_mutex_unlock(&pthread_gettid_np_mutex);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
2014-09-12 23:43:07 +02:00
|
|
|
|
|
|
|
ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
|
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
|
|
|
#endif
|
|
|
|
}
|
2014-09-23 01:01:26 +02:00
|
|
|
|
|
|
|
static size_t cleanup_counter = 0;
|
|
|
|
|
2014-09-25 12:05:32 +02:00
|
|
|
static void AbortCleanupRoutine(void*) {
|
2014-09-23 01:01:26 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:05:32 +02:00
|
|
|
static void CountCleanupRoutine(void*) {
|
2014-09-23 01:01:26 +02:00
|
|
|
++cleanup_counter;
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:05:32 +02:00
|
|
|
static void PthreadCleanupTester() {
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_cleanup_push(CountCleanupRoutine, nullptr);
|
|
|
|
pthread_cleanup_push(CountCleanupRoutine, nullptr);
|
|
|
|
pthread_cleanup_push(AbortCleanupRoutine, nullptr);
|
2014-09-23 01:01:26 +02:00
|
|
|
|
|
|
|
pthread_cleanup_pop(0); // Pop the abort without executing it.
|
|
|
|
pthread_cleanup_pop(1); // Pop one count while executing it.
|
|
|
|
ASSERT_EQ(1U, cleanup_counter);
|
|
|
|
// Exit while the other count is still on the cleanup stack.
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_exit(nullptr);
|
2014-09-23 01:01:26 +02:00
|
|
|
|
|
|
|
// Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
|
|
|
|
pthread_cleanup_pop(0);
|
|
|
|
}
|
|
|
|
|
2014-09-25 12:05:32 +02:00
|
|
|
static void* PthreadCleanupStartRoutine(void*) {
|
2014-09-23 01:01:26 +02:00
|
|
|
PthreadCleanupTester();
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2014-09-23 01:01:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
|
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, PthreadCleanupStartRoutine, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
2014-09-23 01:01:26 +02:00
|
|
|
ASSERT_EQ(2U, cleanup_counter);
|
|
|
|
}
|
2014-09-25 12:05:32 +02:00
|
|
|
|
|
|
|
TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
|
|
|
|
ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutexattr_gettype) {
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
|
|
|
|
|
|
|
int attr_type;
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
|
|
|
|
ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
|
|
|
|
ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
|
|
|
|
ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
|
2015-03-05 02:36:59 +01:00
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
2014-09-25 12:05:32 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
TEST(pthread, pthread_mutexattr_protocol) {
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
|
|
|
|
|
|
|
int protocol;
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_getprotocol(&attr, &protocol));
|
|
|
|
ASSERT_EQ(PTHREAD_PRIO_NONE, protocol);
|
|
|
|
for (size_t repeat = 0; repeat < 2; ++repeat) {
|
|
|
|
for (int set_protocol : {PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT}) {
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, set_protocol));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_getprotocol(&attr, &protocol));
|
|
|
|
ASSERT_EQ(protocol, set_protocol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 23:08:25 +01:00
|
|
|
struct PthreadMutex {
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
explicit PthreadMutex(int mutex_type, int protocol = PTHREAD_PRIO_NONE) {
|
|
|
|
init(mutex_type, protocol);
|
2015-03-21 23:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~PthreadMutex() {
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-01-23 21:56:18 +01:00
|
|
|
void init(int mutex_type, int protocol) {
|
2015-03-21 23:08:25 +01:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
|
2018-01-23 21:56:18 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, protocol));
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy() {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&lock));
|
|
|
|
}
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(PthreadMutex);
|
|
|
|
};
|
2014-09-25 12:05:32 +02:00
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
static void TestPthreadMutexLockNormal(int protocol) {
|
|
|
|
PthreadMutex m(PTHREAD_MUTEX_NORMAL, protocol);
|
2014-09-25 12:05:32 +02:00
|
|
|
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
2015-12-15 02:35:10 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
|
|
|
|
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
2014-09-25 12:05:32 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
static void TestPthreadMutexLockErrorCheck(int protocol) {
|
|
|
|
PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK, protocol);
|
2014-09-25 12:05:32 +02:00
|
|
|
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
|
|
|
|
ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
|
2018-01-23 21:56:18 +01:00
|
|
|
if (protocol == PTHREAD_PRIO_NONE) {
|
|
|
|
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(EDEADLK, pthread_mutex_trylock(&m.lock));
|
|
|
|
}
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
|
|
|
ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
|
2014-09-25 12:05:32 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
static void TestPthreadMutexLockRecursive(int protocol) {
|
|
|
|
PthreadMutex m(PTHREAD_MUTEX_RECURSIVE, protocol);
|
2014-09-25 12:05:32 +02:00
|
|
|
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
|
2015-12-15 02:35:10 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
|
|
|
ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
TEST(pthread, pthread_mutex_lock_NORMAL) {
|
|
|
|
TestPthreadMutexLockNormal(PTHREAD_PRIO_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
|
|
|
|
TestPthreadMutexLockErrorCheck(PTHREAD_PRIO_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_lock_RECURSIVE) {
|
|
|
|
TestPthreadMutexLockRecursive(PTHREAD_PRIO_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_lock_pi) {
|
|
|
|
TestPthreadMutexLockNormal(PTHREAD_PRIO_INHERIT);
|
|
|
|
TestPthreadMutexLockErrorCheck(PTHREAD_PRIO_INHERIT);
|
|
|
|
TestPthreadMutexLockRecursive(PTHREAD_PRIO_INHERIT);
|
|
|
|
}
|
|
|
|
|
2018-01-27 02:32:31 +01:00
|
|
|
TEST(pthread, pthread_mutex_pi_count_limit) {
|
|
|
|
#if defined(__BIONIC__) && !defined(__LP64__)
|
|
|
|
// Bionic only supports 65536 pi mutexes in 32-bit programs.
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT));
|
|
|
|
std::vector<pthread_mutex_t> mutexes(65536);
|
|
|
|
// Test if we can use 65536 pi mutexes at the same time.
|
|
|
|
// Run 2 times to check if freed pi mutexes can be recycled.
|
|
|
|
for (int repeat = 0; repeat < 2; ++repeat) {
|
|
|
|
for (auto& m : mutexes) {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_init(&m, &attr));
|
|
|
|
}
|
|
|
|
pthread_mutex_t m;
|
|
|
|
ASSERT_EQ(ENOMEM, pthread_mutex_init(&m, &attr));
|
|
|
|
for (auto& m : mutexes) {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&m));
|
|
|
|
}
|
|
|
|
for (auto& m : mutexes) {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m));
|
|
|
|
}
|
|
|
|
for (auto& m : mutexes) {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&m));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing as pi mutex count isn't limited.\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-21 23:08:25 +01:00
|
|
|
TEST(pthread, pthread_mutex_init_same_as_static_initializers) {
|
|
|
|
pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
PthreadMutex m1(PTHREAD_MUTEX_NORMAL);
|
|
|
|
ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t)));
|
|
|
|
pthread_mutex_destroy(&lock_normal);
|
|
|
|
|
|
|
|
pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
|
|
|
|
PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK);
|
|
|
|
ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t)));
|
|
|
|
pthread_mutex_destroy(&lock_errorcheck);
|
|
|
|
|
|
|
|
pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
|
|
|
PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
|
2014-09-25 12:05:32 +02:00
|
|
|
}
|
2018-01-27 02:32:31 +01:00
|
|
|
|
2015-03-05 02:36:59 +01:00
|
|
|
class MutexWakeupHelper {
|
|
|
|
private:
|
2015-03-21 23:08:25 +01:00
|
|
|
PthreadMutex m;
|
2015-03-05 02:36:59 +01:00
|
|
|
enum Progress {
|
|
|
|
LOCK_INITIALIZED,
|
|
|
|
LOCK_WAITING,
|
|
|
|
LOCK_RELEASED,
|
|
|
|
LOCK_ACCESSED
|
|
|
|
};
|
|
|
|
std::atomic<Progress> progress;
|
2015-04-03 02:47:48 +02:00
|
|
|
std::atomic<pid_t> tid;
|
2015-03-05 02:36:59 +01:00
|
|
|
|
|
|
|
static void thread_fn(MutexWakeupHelper* helper) {
|
2015-04-03 02:47:48 +02:00
|
|
|
helper->tid = gettid();
|
2015-03-05 02:36:59 +01:00
|
|
|
ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
|
|
|
|
helper->progress = LOCK_WAITING;
|
|
|
|
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
|
2015-03-05 02:36:59 +01:00
|
|
|
ASSERT_EQ(LOCK_RELEASED, helper->progress);
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
|
2015-03-05 02:36:59 +01:00
|
|
|
|
|
|
|
helper->progress = LOCK_ACCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-05-03 21:08:05 +02:00
|
|
|
explicit MutexWakeupHelper(int mutex_type) : m(mutex_type) {
|
2015-03-21 23:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test() {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
|
2015-03-05 02:36:59 +01:00
|
|
|
progress = LOCK_INITIALIZED;
|
2015-04-03 02:47:48 +02:00
|
|
|
tid = 0;
|
2015-03-05 02:36:59 +01:00
|
|
|
|
|
|
|
pthread_t thread;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
2015-03-05 02:36:59 +01:00
|
|
|
reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
|
|
|
|
|
2015-04-03 02:47:48 +02:00
|
|
|
WaitUntilThreadSleep(tid);
|
|
|
|
ASSERT_EQ(LOCK_WAITING, progress);
|
|
|
|
|
2015-03-05 02:36:59 +01:00
|
|
|
progress = LOCK_RELEASED;
|
2015-03-21 23:08:25 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
2015-03-05 02:36:59 +01:00
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
2015-03-05 02:36:59 +01:00
|
|
|
ASSERT_EQ(LOCK_ACCESSED, progress);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_NORMAL_wakeup) {
|
2015-03-21 23:08:25 +01:00
|
|
|
MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL);
|
|
|
|
helper.test();
|
2015-03-05 02:36:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
|
2015-03-21 23:08:25 +01:00
|
|
|
MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK);
|
|
|
|
helper.test();
|
2015-03-05 02:36:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
|
2015-03-21 23:08:25 +01:00
|
|
|
MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
helper.test();
|
2015-03-05 02:36:59 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
static int GetThreadPriority(pid_t tid) {
|
|
|
|
// sched_getparam() returns the static priority of a thread, which can't reflect a thread's
|
|
|
|
// priority after priority inheritance. So read /proc/<pid>/stat to get the dynamic priority.
|
|
|
|
std::string filename = android::base::StringPrintf("/proc/%d/stat", tid);
|
|
|
|
std::string content;
|
|
|
|
int result = INT_MAX;
|
|
|
|
if (!android::base::ReadFileToString(filename, &content)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
std::vector<std::string> strs = android::base::Split(content, " ");
|
|
|
|
if (strs.size() < 18) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (!android::base::ParseInt(strs[17], &result)) {
|
|
|
|
return INT_MAX;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
class PIMutexWakeupHelper {
|
|
|
|
private:
|
|
|
|
PthreadMutex m;
|
|
|
|
int protocol;
|
|
|
|
enum Progress {
|
|
|
|
LOCK_INITIALIZED,
|
|
|
|
LOCK_CHILD_READY,
|
|
|
|
LOCK_WAITING,
|
|
|
|
LOCK_RELEASED,
|
|
|
|
};
|
|
|
|
std::atomic<Progress> progress;
|
|
|
|
std::atomic<pid_t> main_tid;
|
|
|
|
std::atomic<pid_t> child_tid;
|
|
|
|
PthreadMutex start_thread_m;
|
|
|
|
|
|
|
|
static void thread_fn(PIMutexWakeupHelper* helper) {
|
|
|
|
helper->child_tid = gettid();
|
|
|
|
ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
|
|
|
|
ASSERT_EQ(0, setpriority(PRIO_PROCESS, gettid(), 1));
|
|
|
|
ASSERT_EQ(21, GetThreadPriority(gettid()));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
|
|
|
|
helper->progress = LOCK_CHILD_READY;
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&helper->start_thread_m.lock));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&helper->start_thread_m.lock));
|
|
|
|
WaitUntilThreadSleep(helper->main_tid);
|
|
|
|
ASSERT_EQ(LOCK_WAITING, helper->progress);
|
|
|
|
|
|
|
|
if (helper->protocol == PTHREAD_PRIO_INHERIT) {
|
|
|
|
ASSERT_EQ(20, GetThreadPriority(gettid()));
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(21, GetThreadPriority(gettid()));
|
|
|
|
}
|
|
|
|
helper->progress = LOCK_RELEASED;
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit PIMutexWakeupHelper(int mutex_type, int protocol)
|
|
|
|
: m(mutex_type, protocol), protocol(protocol), start_thread_m(PTHREAD_MUTEX_NORMAL) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void test() {
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&start_thread_m.lock));
|
|
|
|
main_tid = gettid();
|
|
|
|
ASSERT_EQ(20, GetThreadPriority(main_tid));
|
|
|
|
progress = LOCK_INITIALIZED;
|
|
|
|
child_tid = 0;
|
|
|
|
|
|
|
|
pthread_t thread;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
2018-01-23 21:56:18 +01:00
|
|
|
reinterpret_cast<void* (*)(void*)>(PIMutexWakeupHelper::thread_fn), this));
|
|
|
|
|
|
|
|
WaitUntilThreadSleep(child_tid);
|
|
|
|
ASSERT_EQ(LOCK_CHILD_READY, progress);
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&start_thread_m.lock));
|
|
|
|
progress = LOCK_WAITING;
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
|
|
|
|
|
|
|
|
ASSERT_EQ(LOCK_RELEASED, progress);
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_pi_wakeup) {
|
|
|
|
for (int type : {PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK}) {
|
|
|
|
for (int protocol : {PTHREAD_PRIO_INHERIT}) {
|
|
|
|
PIMutexWakeupHelper helper(type, protocol);
|
|
|
|
helper.test();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 19:32:00 +01:00
|
|
|
TEST(pthread, pthread_mutex_owner_tid_limit) {
|
2015-02-14 01:21:25 +01:00
|
|
|
#if defined(__BIONIC__) && !defined(__LP64__)
|
2015-02-03 19:32:00 +01:00
|
|
|
FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_TRUE(fp != nullptr);
|
2015-02-03 19:32:00 +01:00
|
|
|
long pid_max;
|
|
|
|
ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
|
|
|
|
fclose(fp);
|
2015-02-14 01:21:25 +01:00
|
|
|
// Bionic's pthread_mutex implementation on 32-bit devices uses 16 bits to represent owner tid.
|
2015-02-03 19:32:00 +01:00
|
|
|
ASSERT_LE(pid_max, 65536);
|
2015-02-14 01:21:25 +01:00
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing as 32-bit tid is supported by pthread_mutex.\n";
|
|
|
|
#endif
|
2015-02-03 19:32:00 +01:00
|
|
|
}
|
2015-03-17 06:46:42 +01:00
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
static void pthread_mutex_timedlock_helper(clockid_t clock,
|
|
|
|
int (*lock_function)(pthread_mutex_t* __mutex,
|
|
|
|
const timespec* __timeout)) {
|
2015-11-06 00:36:08 +01:00
|
|
|
pthread_mutex_t m;
|
|
|
|
ASSERT_EQ(0, pthread_mutex_init(&m, nullptr));
|
|
|
|
|
|
|
|
// If the mutex is already locked, pthread_mutex_timedlock should time out.
|
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(&m));
|
|
|
|
|
|
|
|
timespec ts;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(0, clock_gettime(clock, &ts));
|
|
|
|
ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_nsec = -1;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(EINVAL, lock_function(&m, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_nsec = NS_PER_S;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(EINVAL, lock_function(&m, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_nsec = NS_PER_S - 1;
|
|
|
|
ts.tv_sec = -1;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
|
|
|
|
// If the mutex is unlocked, pthread_mutex_timedlock should succeed.
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m));
|
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(0, clock_gettime(clock, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
ts.tv_sec += 1;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(0, lock_function(&m, &ts));
|
2015-11-06 00:36:08 +01:00
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&m));
|
|
|
|
}
|
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
TEST(pthread, pthread_mutex_timedlock) {
|
|
|
|
pthread_mutex_timedlock_helper(CLOCK_REALTIME, pthread_mutex_timedlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_timedlock_monotonic_np) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
pthread_mutex_timedlock_helper(CLOCK_MONOTONIC, pthread_mutex_timedlock_monotonic_np);
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing since pthread_mutex_timedlock_monotonic_np is only "
|
|
|
|
"supported on bionic";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pthread_mutex_timedlock_pi_helper(clockid_t clock,
|
|
|
|
int (*lock_function)(pthread_mutex_t* __mutex,
|
|
|
|
const timespec* __timeout)) {
|
2018-01-23 21:56:18 +01:00
|
|
|
PthreadMutex m(PTHREAD_MUTEX_NORMAL, PTHREAD_PRIO_INHERIT);
|
2018-03-05 23:14:44 +01:00
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
timespec ts;
|
2018-03-05 23:14:44 +01:00
|
|
|
clock_gettime(clock, &ts);
|
2018-01-23 21:56:18 +01:00
|
|
|
ts.tv_sec += 1;
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EQ(0, lock_function(&m.lock, &ts));
|
|
|
|
|
|
|
|
struct ThreadArgs {
|
|
|
|
clockid_t clock;
|
|
|
|
int (*lock_function)(pthread_mutex_t* __mutex, const timespec* __timeout);
|
|
|
|
PthreadMutex& m;
|
|
|
|
};
|
|
|
|
|
|
|
|
ThreadArgs thread_args = {
|
|
|
|
.clock = clock,
|
|
|
|
.lock_function = lock_function,
|
|
|
|
.m = m,
|
|
|
|
};
|
2018-01-23 21:56:18 +01:00
|
|
|
|
|
|
|
auto ThreadFn = [](void* arg) -> void* {
|
2018-03-05 23:14:44 +01:00
|
|
|
auto args = static_cast<ThreadArgs*>(arg);
|
2018-01-23 21:56:18 +01:00
|
|
|
timespec ts;
|
2018-03-05 23:14:44 +01:00
|
|
|
clock_gettime(args->clock, &ts);
|
2018-01-23 21:56:18 +01:00
|
|
|
ts.tv_sec += 1;
|
2018-03-05 23:14:44 +01:00
|
|
|
intptr_t result = args->lock_function(&args->m.lock, &ts);
|
2018-01-23 21:56:18 +01:00
|
|
|
return reinterpret_cast<void*>(result);
|
|
|
|
};
|
|
|
|
|
|
|
|
pthread_t thread;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr, ThreadFn, &thread_args));
|
2018-01-23 21:56:18 +01:00
|
|
|
void* result;
|
|
|
|
ASSERT_EQ(0, pthread_join(thread, &result));
|
|
|
|
ASSERT_EQ(ETIMEDOUT, reinterpret_cast<intptr_t>(result));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
|
|
|
|
}
|
|
|
|
|
2018-03-05 23:14:44 +01:00
|
|
|
TEST(pthread, pthread_mutex_timedlock_pi) {
|
|
|
|
pthread_mutex_timedlock_pi_helper(CLOCK_REALTIME, pthread_mutex_timedlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_timedlock_monotonic_np_pi) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
pthread_mutex_timedlock_pi_helper(CLOCK_MONOTONIC, pthread_mutex_timedlock_monotonic_np);
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing since pthread_mutex_timedlock_monotonic_np is only "
|
|
|
|
"supported on bionic";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:02:21 +01:00
|
|
|
TEST(pthread, pthread_mutex_using_destroyed_mutex) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
pthread_mutex_t m;
|
|
|
|
ASSERT_EQ(0, pthread_mutex_init(&m, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&m));
|
|
|
|
ASSERT_EXIT(pthread_mutex_lock(&m), ::testing::KilledBySignal(SIGABRT),
|
|
|
|
"pthread_mutex_lock called on a destroyed mutex");
|
|
|
|
ASSERT_EXIT(pthread_mutex_unlock(&m), ::testing::KilledBySignal(SIGABRT),
|
|
|
|
"pthread_mutex_unlock called on a destroyed mutex");
|
|
|
|
ASSERT_EXIT(pthread_mutex_trylock(&m), ::testing::KilledBySignal(SIGABRT),
|
|
|
|
"pthread_mutex_trylock called on a destroyed mutex");
|
|
|
|
timespec ts;
|
|
|
|
ASSERT_EXIT(pthread_mutex_timedlock(&m, &ts), ::testing::KilledBySignal(SIGABRT),
|
|
|
|
"pthread_mutex_timedlock called on a destroyed mutex");
|
2018-03-05 23:14:44 +01:00
|
|
|
ASSERT_EXIT(pthread_mutex_timedlock_monotonic_np(&m, &ts), ::testing::KilledBySignal(SIGABRT),
|
|
|
|
"pthread_mutex_timedlock_monotonic_np called on a destroyed mutex");
|
2018-03-14 20:02:21 +01:00
|
|
|
ASSERT_EXIT(pthread_mutex_destroy(&m), ::testing::KilledBySignal(SIGABRT),
|
|
|
|
"pthread_mutex_destroy called on a destroyed mutex");
|
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test tests bionic pthread mutex implementation details.";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-17 06:46:42 +01:00
|
|
|
class StrictAlignmentAllocator {
|
|
|
|
public:
|
|
|
|
void* allocate(size_t size, size_t alignment) {
|
|
|
|
char* p = new char[size + alignment * 2];
|
|
|
|
allocated_array.push_back(p);
|
|
|
|
while (!is_strict_aligned(p, alignment)) {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
~StrictAlignmentAllocator() {
|
2015-10-03 03:25:19 +02:00
|
|
|
for (const auto& p : allocated_array) {
|
|
|
|
delete[] p;
|
2015-03-17 06:46:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_strict_aligned(char* p, size_t alignment) {
|
|
|
|
return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char*> allocated_array;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(pthread, pthread_types_allow_four_bytes_alignment) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
// For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types.
|
|
|
|
StrictAlignmentAllocator allocator;
|
|
|
|
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(
|
|
|
|
allocator.allocate(sizeof(pthread_mutex_t), 4));
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_mutex_init(mutex, nullptr));
|
2015-03-17 06:46:42 +01:00
|
|
|
ASSERT_EQ(0, pthread_mutex_lock(mutex));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_unlock(mutex));
|
|
|
|
ASSERT_EQ(0, pthread_mutex_destroy(mutex));
|
|
|
|
|
|
|
|
pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(
|
|
|
|
allocator.allocate(sizeof(pthread_cond_t), 4));
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_cond_init(cond, nullptr));
|
2015-03-17 06:46:42 +01:00
|
|
|
ASSERT_EQ(0, pthread_cond_signal(cond));
|
|
|
|
ASSERT_EQ(0, pthread_cond_broadcast(cond));
|
|
|
|
ASSERT_EQ(0, pthread_cond_destroy(cond));
|
|
|
|
|
|
|
|
pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(
|
|
|
|
allocator.allocate(sizeof(pthread_rwlock_t), 4));
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_init(rwlock, nullptr));
|
2015-03-17 06:46:42 +01:00
|
|
|
ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
|
|
|
|
ASSERT_EQ(0, pthread_rwlock_destroy(rwlock));
|
|
|
|
|
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test tests bionic implementation details.";
|
|
|
|
#endif
|
|
|
|
}
|
2015-06-10 03:46:15 +02:00
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_lock_null_32) {
|
|
|
|
#if defined(__BIONIC__) && !defined(__LP64__)
|
2015-08-14 01:58:50 +02:00
|
|
|
// For LP32, the pthread lock/unlock functions allow a NULL mutex and return
|
|
|
|
// EINVAL in that case: http://b/19995172.
|
|
|
|
//
|
|
|
|
// We decorate the public defintion with _Nonnull so that people recompiling
|
|
|
|
// their code with get a warning and might fix their bug, but need to pass
|
|
|
|
// NULL here to test that we remain compatible.
|
|
|
|
pthread_mutex_t* null_value = nullptr;
|
|
|
|
ASSERT_EQ(EINVAL, pthread_mutex_lock(null_value));
|
2015-06-10 03:46:15 +02:00
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_mutex_unlock_null_32) {
|
|
|
|
#if defined(__BIONIC__) && !defined(__LP64__)
|
2015-08-14 01:58:50 +02:00
|
|
|
// For LP32, the pthread lock/unlock functions allow a NULL mutex and return
|
|
|
|
// EINVAL in that case: http://b/19995172.
|
|
|
|
//
|
|
|
|
// We decorate the public defintion with _Nonnull so that people recompiling
|
|
|
|
// their code with get a warning and might fix their bug, but need to pass
|
|
|
|
// NULL here to test that we remain compatible.
|
|
|
|
pthread_mutex_t* null_value = nullptr;
|
|
|
|
ASSERT_EQ(EINVAL, pthread_mutex_unlock(null_value));
|
2015-06-10 03:46:15 +02:00
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) {
|
|
|
|
#if defined(__BIONIC__) && defined(__LP64__)
|
|
|
|
pthread_mutex_t* null_value = nullptr;
|
|
|
|
ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), "");
|
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) {
|
|
|
|
#if defined(__BIONIC__) && defined(__LP64__)
|
|
|
|
pthread_mutex_t* null_value = nullptr;
|
|
|
|
ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), "");
|
|
|
|
#else
|
|
|
|
GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
|
|
|
|
#endif
|
|
|
|
}
|
2015-09-22 20:16:15 +02:00
|
|
|
|
|
|
|
extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg);
|
|
|
|
|
|
|
|
static volatile bool signal_handler_on_altstack_done;
|
|
|
|
|
2017-03-16 03:42:05 +01:00
|
|
|
__attribute__((__noinline__))
|
|
|
|
static void signal_handler_backtrace() {
|
|
|
|
// Check if we have enough stack space for unwinding.
|
|
|
|
int count = 0;
|
|
|
|
_Unwind_Backtrace(FrameCounter, &count);
|
|
|
|
ASSERT_GT(count, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((__noinline__))
|
|
|
|
static void signal_handler_logging() {
|
|
|
|
// Check if we have enough stack space for logging.
|
|
|
|
std::string s(2048, '*');
|
|
|
|
GTEST_LOG_(INFO) << s;
|
|
|
|
signal_handler_on_altstack_done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((__noinline__))
|
|
|
|
static void signal_handler_snprintf() {
|
|
|
|
// Check if we have enough stack space for snprintf to a PATH_MAX buffer, plus some extra.
|
|
|
|
char buf[PATH_MAX + 2048];
|
|
|
|
ASSERT_GT(snprintf(buf, sizeof(buf), "/proc/%d/status", getpid()), 0);
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:16:15 +02:00
|
|
|
static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) {
|
|
|
|
ASSERT_EQ(SIGUSR1, signo);
|
2017-03-16 03:42:05 +01:00
|
|
|
signal_handler_backtrace();
|
|
|
|
signal_handler_logging();
|
|
|
|
signal_handler_snprintf();
|
2015-09-22 20:16:15 +02:00
|
|
|
}
|
|
|
|
|
2017-03-07 02:45:33 +01:00
|
|
|
TEST(pthread, big_enough_signal_stack) {
|
2015-09-22 20:16:15 +02:00
|
|
|
signal_handler_on_altstack_done = false;
|
|
|
|
ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK);
|
|
|
|
kill(getpid(), SIGUSR1);
|
|
|
|
ASSERT_TRUE(signal_handler_on_altstack_done);
|
|
|
|
}
|
2015-11-06 07:06:09 +01:00
|
|
|
|
|
|
|
TEST(pthread, pthread_barrierattr_smoke) {
|
|
|
|
pthread_barrierattr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_barrierattr_init(&attr));
|
|
|
|
int pshared;
|
|
|
|
ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
|
|
|
|
ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
|
|
|
|
ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
|
|
|
|
ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
|
|
|
|
ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
|
|
|
|
ASSERT_EQ(0, pthread_barrierattr_destroy(&attr));
|
|
|
|
}
|
|
|
|
|
2016-03-22 21:45:55 +01:00
|
|
|
struct BarrierTestHelperData {
|
|
|
|
size_t thread_count;
|
|
|
|
pthread_barrier_t barrier;
|
|
|
|
std::atomic<int> finished_mask;
|
|
|
|
std::atomic<int> serial_thread_count;
|
2015-11-06 07:06:09 +01:00
|
|
|
size_t iteration_count;
|
2016-03-22 21:45:55 +01:00
|
|
|
std::atomic<size_t> finished_iteration_count;
|
|
|
|
|
|
|
|
BarrierTestHelperData(size_t thread_count, size_t iteration_count)
|
|
|
|
: thread_count(thread_count), finished_mask(0), serial_thread_count(0),
|
|
|
|
iteration_count(iteration_count), finished_iteration_count(0) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BarrierTestHelperArg {
|
|
|
|
int id;
|
|
|
|
BarrierTestHelperData* data;
|
2015-11-06 07:06:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void BarrierTestHelper(BarrierTestHelperArg* arg) {
|
2016-03-22 21:45:55 +01:00
|
|
|
for (size_t i = 0; i < arg->data->iteration_count; ++i) {
|
|
|
|
int result = pthread_barrier_wait(&arg->data->barrier);
|
|
|
|
if (result == PTHREAD_BARRIER_SERIAL_THREAD) {
|
|
|
|
arg->data->serial_thread_count++;
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(0, result);
|
|
|
|
}
|
2017-05-02 21:57:39 +02:00
|
|
|
int mask = arg->data->finished_mask.fetch_or(1 << arg->id);
|
2017-05-03 01:18:13 +02:00
|
|
|
mask |= 1 << arg->id;
|
2017-05-02 21:57:39 +02:00
|
|
|
if (mask == ((1 << arg->data->thread_count) - 1)) {
|
2016-03-22 21:45:55 +01:00
|
|
|
ASSERT_EQ(1, arg->data->serial_thread_count);
|
|
|
|
arg->data->finished_iteration_count++;
|
|
|
|
arg->data->finished_mask = 0;
|
|
|
|
arg->data->serial_thread_count = 0;
|
|
|
|
}
|
2015-11-06 07:06:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_barrier_smoke) {
|
|
|
|
const size_t BARRIER_ITERATION_COUNT = 10;
|
|
|
|
const size_t BARRIER_THREAD_COUNT = 10;
|
2016-03-22 21:45:55 +01:00
|
|
|
BarrierTestHelperData data(BARRIER_THREAD_COUNT, BARRIER_ITERATION_COUNT);
|
|
|
|
ASSERT_EQ(0, pthread_barrier_init(&data.barrier, nullptr, data.thread_count));
|
|
|
|
std::vector<pthread_t> threads(data.thread_count);
|
2015-11-06 07:06:09 +01:00
|
|
|
std::vector<BarrierTestHelperArg> args(threads.size());
|
|
|
|
for (size_t i = 0; i < threads.size(); ++i) {
|
2016-03-22 21:45:55 +01:00
|
|
|
args[i].id = i;
|
|
|
|
args[i].data = &data;
|
2015-11-06 07:06:09 +01:00
|
|
|
ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
|
|
|
|
reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i]));
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < threads.size(); ++i) {
|
|
|
|
ASSERT_EQ(0, pthread_join(threads[i], nullptr));
|
|
|
|
}
|
2016-03-22 21:45:55 +01:00
|
|
|
ASSERT_EQ(data.iteration_count, data.finished_iteration_count);
|
|
|
|
ASSERT_EQ(0, pthread_barrier_destroy(&data.barrier));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BarrierDestroyTestArg {
|
|
|
|
std::atomic<int> tid;
|
|
|
|
pthread_barrier_t* barrier;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void BarrierDestroyTestHelper(BarrierDestroyTestArg* arg) {
|
|
|
|
arg->tid = gettid();
|
|
|
|
ASSERT_EQ(0, pthread_barrier_wait(arg->barrier));
|
2015-11-06 07:06:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_barrier_destroy) {
|
|
|
|
pthread_barrier_t barrier;
|
|
|
|
ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2));
|
|
|
|
pthread_t thread;
|
2016-03-22 21:45:55 +01:00
|
|
|
BarrierDestroyTestArg arg;
|
2015-11-06 07:06:09 +01:00
|
|
|
arg.tid = 0;
|
|
|
|
arg.barrier = &barrier;
|
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
2016-03-22 21:45:55 +01:00
|
|
|
reinterpret_cast<void* (*)(void*)>(BarrierDestroyTestHelper), &arg));
|
2015-11-06 07:06:09 +01:00
|
|
|
WaitUntilThreadSleep(arg.tid);
|
|
|
|
ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier));
|
|
|
|
ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier));
|
|
|
|
// Verify if the barrier can be destroyed directly after pthread_barrier_wait().
|
|
|
|
ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
|
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BarrierOrderingTestHelperArg {
|
|
|
|
pthread_barrier_t* barrier;
|
|
|
|
size_t* array;
|
|
|
|
size_t array_length;
|
|
|
|
size_t id;
|
|
|
|
};
|
|
|
|
|
|
|
|
void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) {
|
|
|
|
const size_t ITERATION_COUNT = 10000;
|
|
|
|
for (size_t i = 1; i <= ITERATION_COUNT; ++i) {
|
|
|
|
arg->array[arg->id] = i;
|
2015-11-06 00:36:08 +01:00
|
|
|
int result = pthread_barrier_wait(arg->barrier);
|
|
|
|
ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
|
2015-11-06 07:06:09 +01:00
|
|
|
for (size_t j = 0; j < arg->array_length; ++j) {
|
|
|
|
ASSERT_EQ(i, arg->array[j]);
|
|
|
|
}
|
2015-11-06 00:36:08 +01:00
|
|
|
result = pthread_barrier_wait(arg->barrier);
|
|
|
|
ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
|
2015-11-06 07:06:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_barrier_check_ordering) {
|
|
|
|
const size_t THREAD_COUNT = 4;
|
|
|
|
pthread_barrier_t barrier;
|
|
|
|
ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT));
|
|
|
|
size_t array[THREAD_COUNT];
|
|
|
|
std::vector<pthread_t> threads(THREAD_COUNT);
|
|
|
|
std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT);
|
|
|
|
for (size_t i = 0; i < THREAD_COUNT; ++i) {
|
|
|
|
args[i].barrier = &barrier;
|
|
|
|
args[i].array = array;
|
|
|
|
args[i].array_length = THREAD_COUNT;
|
|
|
|
args[i].id = i;
|
|
|
|
ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
|
|
|
|
reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper),
|
|
|
|
&args[i]));
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < THREAD_COUNT; ++i) {
|
|
|
|
ASSERT_EQ(0, pthread_join(threads[i], nullptr));
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 01:03:18 +01:00
|
|
|
|
2018-07-06 23:34:49 +02:00
|
|
|
TEST(pthread, pthread_barrier_init_zero_count) {
|
|
|
|
pthread_barrier_t barrier;
|
|
|
|
ASSERT_EQ(EINVAL, pthread_barrier_init(&barrier, nullptr, 0));
|
|
|
|
}
|
|
|
|
|
2015-11-18 01:03:18 +01:00
|
|
|
TEST(pthread, pthread_spinlock_smoke) {
|
|
|
|
pthread_spinlock_t lock;
|
|
|
|
ASSERT_EQ(0, pthread_spin_init(&lock, 0));
|
|
|
|
ASSERT_EQ(0, pthread_spin_trylock(&lock));
|
|
|
|
ASSERT_EQ(0, pthread_spin_unlock(&lock));
|
|
|
|
ASSERT_EQ(0, pthread_spin_lock(&lock));
|
|
|
|
ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock));
|
|
|
|
ASSERT_EQ(0, pthread_spin_unlock(&lock));
|
|
|
|
ASSERT_EQ(0, pthread_spin_destroy(&lock));
|
|
|
|
}
|
2017-09-19 23:02:50 +02:00
|
|
|
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
TEST(pthread, pthread_attr_getdetachstate__pthread_attr_setdetachstate) {
|
2017-09-19 23:02:50 +02:00
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
int state;
|
2017-09-19 23:02:50 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
|
|
|
|
ASSERT_EQ(PTHREAD_CREATE_DETACHED, state);
|
|
|
|
|
2017-09-19 23:02:50 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
|
|
|
|
ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state);
|
|
|
|
|
2017-09-19 23:02:50 +02:00
|
|
|
ASSERT_EQ(EINVAL, pthread_attr_setdetachstate(&attr, 123));
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
|
|
|
|
ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state);
|
2017-09-19 23:02:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_create__mmap_failures) {
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
|
|
|
|
|
|
|
|
const auto kPageSize = sysconf(_SC_PAGE_SIZE);
|
|
|
|
|
2017-10-03 07:49:18 +02:00
|
|
|
// Use up all the VMAs. By default this is 64Ki (though some will already be in use).
|
2017-09-19 23:02:50 +02:00
|
|
|
std::vector<void*> pages;
|
2017-10-03 07:49:18 +02:00
|
|
|
pages.reserve(64 * 1024);
|
2017-09-19 23:02:50 +02:00
|
|
|
int prot = PROT_NONE;
|
|
|
|
while (true) {
|
|
|
|
void* page = mmap(nullptr, kPageSize, prot, MAP_ANON|MAP_PRIVATE, -1, 0);
|
|
|
|
if (page == MAP_FAILED) break;
|
|
|
|
pages.push_back(page);
|
|
|
|
prot = (prot == PROT_NONE) ? PROT_READ : PROT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try creating threads, freeing up a page each time we fail.
|
|
|
|
size_t EAGAIN_count = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
for (; i < pages.size(); ++i) {
|
|
|
|
pthread_t t;
|
|
|
|
int status = pthread_create(&t, &attr, IdFn, nullptr);
|
|
|
|
if (status != EAGAIN) break;
|
|
|
|
++EAGAIN_count;
|
|
|
|
ASSERT_EQ(0, munmap(pages[i], kPageSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating a thread uses at least six VMAs: the stack, the TLS, and a guard each side of both.
|
|
|
|
// So we should have seen at least six failures.
|
|
|
|
ASSERT_GE(EAGAIN_count, 6U);
|
|
|
|
|
|
|
|
for (; i < pages.size(); ++i) {
|
|
|
|
ASSERT_EQ(0, munmap(pages[i], kPageSize));
|
|
|
|
}
|
|
|
|
}
|
2017-10-16 18:58:45 +02:00
|
|
|
|
|
|
|
TEST(pthread, pthread_setschedparam) {
|
|
|
|
sched_param p = { .sched_priority = INT_MIN };
|
|
|
|
ASSERT_EQ(EINVAL, pthread_setschedparam(pthread_self(), INT_MIN, &p));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_setschedprio) {
|
|
|
|
ASSERT_EQ(EINVAL, pthread_setschedprio(pthread_self(), INT_MIN));
|
|
|
|
}
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_getinheritsched__pthread_attr_setinheritsched) {
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
|
|
|
|
int state;
|
|
|
|
ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
|
|
|
|
ASSERT_EQ(PTHREAD_INHERIT_SCHED, state);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
|
|
|
|
ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state);
|
|
|
|
|
|
|
|
ASSERT_EQ(EINVAL, pthread_attr_setinheritsched(&attr, 123));
|
|
|
|
ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
|
|
|
|
ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_setinheritsched__PTHREAD_INHERIT_SCHED__PTHREAD_EXPLICIT_SCHED) {
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
|
|
|
|
// If we set invalid scheduling attributes but choose to inherit, everything's fine...
|
|
|
|
sched_param param = { .sched_priority = sched_get_priority_max(SCHED_FIFO) + 1 };
|
|
|
|
ASSERT_EQ(0, pthread_attr_setschedparam(&attr, ¶m));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_FIFO));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
|
|
|
|
|
|
|
|
pthread_t t;
|
|
|
|
ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, nullptr));
|
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
|
|
|
|
2017-10-30 17:26:06 +01:00
|
|
|
#if defined(__LP64__)
|
|
|
|
// If we ask to use them, though, we'll see a failure...
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
|
|
|
|
ASSERT_EQ(EINVAL, pthread_create(&t, &attr, IdFn, nullptr));
|
2017-10-30 17:26:06 +01:00
|
|
|
#else
|
|
|
|
// For backwards compatibility with broken apps, we just ignore failures
|
|
|
|
// to set scheduler attributes on LP32.
|
|
|
|
#endif
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_setinheritsched_PTHREAD_INHERIT_SCHED_takes_effect) {
|
|
|
|
sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
|
|
|
|
int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
|
|
|
|
if (rc == EPERM) {
|
|
|
|
GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(0, rc);
|
|
|
|
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
|
|
|
|
|
2017-11-02 21:11:13 +01:00
|
|
|
SpinFunctionHelper spin_helper;
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
pthread_t t;
|
2017-11-02 21:11:13 +01:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr));
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
int actual_policy;
|
|
|
|
sched_param actual_param;
|
|
|
|
ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
|
|
|
|
ASSERT_EQ(SCHED_FIFO, actual_policy);
|
2017-11-02 21:11:13 +01:00
|
|
|
spin_helper.UnSpin();
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_setinheritsched_PTHREAD_EXPLICIT_SCHED_takes_effect) {
|
|
|
|
sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
|
|
|
|
int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
|
|
|
|
if (rc == EPERM) {
|
|
|
|
GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(0, rc);
|
|
|
|
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_OTHER));
|
|
|
|
|
2017-11-02 21:11:13 +01:00
|
|
|
SpinFunctionHelper spin_helper;
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
pthread_t t;
|
2017-11-02 21:11:13 +01:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr));
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
int actual_policy;
|
|
|
|
sched_param actual_param;
|
|
|
|
ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
|
|
|
|
ASSERT_EQ(SCHED_OTHER, actual_policy);
|
2017-11-02 21:11:13 +01:00
|
|
|
spin_helper.UnSpin();
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pthread, pthread_attr_setinheritsched__takes_effect_despite_SCHED_RESET_ON_FORK) {
|
|
|
|
sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
|
|
|
|
int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m);
|
|
|
|
if (rc == EPERM) {
|
|
|
|
GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(0, rc);
|
|
|
|
|
|
|
|
pthread_attr_t attr;
|
|
|
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
|
|
|
ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
|
|
|
|
|
2017-11-02 21:11:13 +01:00
|
|
|
SpinFunctionHelper spin_helper;
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
pthread_t t;
|
2017-11-02 21:11:13 +01:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr));
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
int actual_policy;
|
|
|
|
sched_param actual_param;
|
|
|
|
ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
|
|
|
|
ASSERT_EQ(SCHED_FIFO | SCHED_RESET_ON_FORK, actual_policy);
|
2017-11-02 21:11:13 +01:00
|
|
|
spin_helper.UnSpin();
|
Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.
Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)
Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).
An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).
If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!
-*-
This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.
Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.
Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-18 00:34:41 +02:00
|
|
|
ASSERT_EQ(0, pthread_join(t, nullptr));
|
|
|
|
}
|