Use std::thread in thread_test.cc.
The STL already came up with a threading abstraction for us. If this sticks, that also means we can more easily write tests elsewhere that use threads. (A test that makes a bunch of TLS connections on a shared SSL_CTX run under TSan would be nice. Likewise with some of the messy RSA locking.) Update-Note: This adds a dependency from crypto_test to C++11 threads. Hopefully it doesn't cause issues. Change-Id: I26f89f6b3b79240e516017877d06fd9a815fc315 Reviewed-on: https://boringssl-review.googlesource.com/28865 Reviewed-by: Steven Valdez <svaldez@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
This commit is contained in:
parent
1627871d18
commit
c1e4f338b1
@ -14,6 +14,9 @@
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
@ -24,83 +27,30 @@
|
||||
|
||||
#if !defined(OPENSSL_NO_THREADS)
|
||||
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
|
||||
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
||||
#include <windows.h>
|
||||
OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
|
||||
typedef HANDLE thread_t;
|
||||
|
||||
static DWORD WINAPI thread_run(LPVOID arg) {
|
||||
void (*thread_func)(void);
|
||||
// VC really doesn't like casting between data and function pointers.
|
||||
OPENSSL_memcpy(&thread_func, &arg, sizeof(thread_func));
|
||||
thread_func();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int run_thread(thread_t *out_thread, void (*thread_func)(void)) {
|
||||
void *arg;
|
||||
// VC really doesn't like casting between data and function pointers.
|
||||
OPENSSL_memcpy(&arg, &thread_func, sizeof(arg));
|
||||
|
||||
*out_thread = CreateThread(NULL /* security attributes */,
|
||||
0 /* default stack size */, thread_run, arg,
|
||||
0 /* run immediately */, NULL /* ignore id */);
|
||||
return *out_thread != NULL;
|
||||
}
|
||||
|
||||
static int wait_for_thread(thread_t thread) {
|
||||
return WaitForSingleObject(thread, INFINITE) == 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef pthread_t thread_t;
|
||||
|
||||
static void *thread_run(void *arg) {
|
||||
void (*thread_func)(void) = reinterpret_cast<void (*)(void)>(arg);
|
||||
thread_func();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int run_thread(thread_t *out_thread, void (*thread_func)(void)) {
|
||||
return pthread_create(out_thread, NULL /* default attributes */, thread_run,
|
||||
reinterpret_cast<void *>(thread_func)) == 0;
|
||||
}
|
||||
|
||||
static int wait_for_thread(thread_t thread) {
|
||||
return pthread_join(thread, NULL) == 0;
|
||||
}
|
||||
|
||||
#endif // OPENSSL_WINDOWS
|
||||
|
||||
static unsigned g_once_init_called = 0;
|
||||
|
||||
static void once_init(void) {
|
||||
g_once_init_called++;
|
||||
|
||||
// Sleep briefly so one |call_once_thread| instance will call |CRYPTO_once|
|
||||
// Sleep briefly so one |call_once_func| instance will call |CRYPTO_once|
|
||||
// while the other is running this function.
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
Sleep(1 /* milliseconds */);
|
||||
#else
|
||||
struct timespec req;
|
||||
OPENSSL_memset(&req, 0, sizeof(req));
|
||||
req.tv_nsec = 1000000;
|
||||
nanosleep(&req, NULL);
|
||||
#endif
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
static CRYPTO_once_t g_test_once = CRYPTO_ONCE_INIT;
|
||||
|
||||
static void call_once_thread(void) {
|
||||
TEST(ThreadTest, Once) {
|
||||
ASSERT_EQ(0u, g_once_init_called)
|
||||
<< "g_once_init_called was non-zero at start.";
|
||||
|
||||
auto call_once_func = [] { CRYPTO_once(&g_test_once, once_init); };
|
||||
std::thread thread1(call_once_func), thread2(call_once_func);
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
CRYPTO_once(&g_test_once, once_init);
|
||||
|
||||
EXPECT_EQ(1u, g_once_init_called);
|
||||
}
|
||||
|
||||
static CRYPTO_once_t once_init_value = CRYPTO_ONCE_INIT;
|
||||
@ -112,21 +62,6 @@ static struct CRYPTO_STATIC_MUTEX mutex_bss;
|
||||
static CRYPTO_EX_DATA_CLASS ex_data_class_value = CRYPTO_EX_DATA_CLASS_INIT;
|
||||
static CRYPTO_EX_DATA_CLASS ex_data_class_bss;
|
||||
|
||||
TEST(ThreadTest, Once) {
|
||||
ASSERT_EQ(0u, g_once_init_called)
|
||||
<< "g_once_init_called was non-zero at start.";
|
||||
|
||||
thread_t thread1, thread2;
|
||||
ASSERT_TRUE(run_thread(&thread1, call_once_thread));
|
||||
ASSERT_TRUE(run_thread(&thread2, call_once_thread));
|
||||
ASSERT_TRUE(wait_for_thread(thread1));
|
||||
ASSERT_TRUE(wait_for_thread(thread2));
|
||||
|
||||
CRYPTO_once(&g_test_once, once_init);
|
||||
|
||||
EXPECT_EQ(1u, g_once_init_called);
|
||||
}
|
||||
|
||||
TEST(ThreadTest, InitZeros) {
|
||||
if (FIPS_mode()) {
|
||||
// Our FIPS tooling currently requires that |CRYPTO_ONCE_INIT|,
|
||||
@ -158,8 +93,7 @@ TEST(ThreadTest, ThreadLocal) {
|
||||
ASSERT_EQ(nullptr, CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST))
|
||||
<< "Thread-local data was non-NULL at start.";
|
||||
|
||||
thread_t thread;
|
||||
ASSERT_TRUE(run_thread(&thread, []() {
|
||||
std::thread thread([] {
|
||||
if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != NULL ||
|
||||
!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST,
|
||||
&g_destructor_called_count,
|
||||
@ -170,16 +104,16 @@ TEST(ThreadTest, ThreadLocal) {
|
||||
}
|
||||
|
||||
g_test_thread_ok = 1;
|
||||
}));
|
||||
ASSERT_TRUE(wait_for_thread(thread));
|
||||
});
|
||||
thread.join();
|
||||
|
||||
EXPECT_TRUE(g_test_thread_ok) << "Thread-local data didn't work in thread.";
|
||||
EXPECT_EQ(1u, g_destructor_called_count);
|
||||
|
||||
// Create a no-op thread to test test that the thread destructor function
|
||||
// works even if thread-local storage wasn't used for a thread.
|
||||
ASSERT_TRUE(run_thread(&thread, []() {}));
|
||||
ASSERT_TRUE(wait_for_thread(thread));
|
||||
// Create a no-op thread to test that the thread destructor function works
|
||||
// even if thread-local storage wasn't used for a thread.
|
||||
thread = std::thread([] {});
|
||||
thread.join();
|
||||
}
|
||||
|
||||
TEST(ThreadTest, RandState) {
|
||||
@ -189,12 +123,11 @@ TEST(ThreadTest, RandState) {
|
||||
uint8_t buf[1];
|
||||
RAND_bytes(buf, sizeof(buf));
|
||||
|
||||
thread_t thread;
|
||||
ASSERT_TRUE(run_thread(&thread, []() {
|
||||
std::thread thread([] {
|
||||
uint8_t buf2[1];
|
||||
RAND_bytes(buf2, sizeof(buf2));
|
||||
}));
|
||||
ASSERT_TRUE(wait_for_thread(thread));
|
||||
});
|
||||
thread.join();
|
||||
}
|
||||
|
||||
#endif // !OPENSSL_NO_THREADS
|
||||
|
Loading…
Reference in New Issue
Block a user