Skip to content

Commit

Permalink
Cherry pick PR #3234: Migrate nplb tests to use pthread (#3240)
Browse files Browse the repository at this point in the history
Refer to the original PR: #3234

Test-On-Device: true
b/302335657

Change-Id: I6ec4b84c80753d79ea5958f57c6f1f3e8392ce77

Co-authored-by: Yavor Goulishev <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and y4vor authored May 14, 2024
1 parent 6f3178f commit 3e680f3
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 86 deletions.
19 changes: 7 additions & 12 deletions starboard/nplb/atomic_base_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <pthread.h>
#include <sched.h>
#include <algorithm>
#include <numeric>
Expand All @@ -33,30 +34,24 @@ namespace {
// thread. Subclasses must override Run().
class TestThread {
public:
TestThread() : thread_(kSbThreadInvalid) {}
TestThread() : thread_(0) {}
virtual ~TestThread() {}

// Subclasses should override the Run method.
virtual void Run() = 0;

// Calls SbThreadCreate() with default parameters.
// Calls pthread_create() with default parameters.
void Start() {
SbThreadEntryPoint entry_point = ThreadEntryPoint;
pthread_create(&thread_, nullptr, ThreadEntryPoint, this);

thread_ = SbThreadCreate(0, // default stack_size.
kSbThreadNoPriority, // default priority.
kSbThreadNoAffinity, // default affinity.
true, // joinable.
"TestThread", entry_point, this);

if (kSbThreadInvalid == thread_) {
if (thread_ == 0) {
ADD_FAILURE_AT(__FILE__, __LINE__) << "Invalid thread.";
}
return;
}

void Join() {
if (!SbThreadJoin(thread_, NULL)) {
if (pthread_join(thread_, NULL) != 0) {
ADD_FAILURE_AT(__FILE__, __LINE__) << "Could not join thread.";
}
}
Expand All @@ -68,7 +63,7 @@ class TestThread {
return NULL;
}

SbThread thread_;
pthread_t thread_;

TestThread(const TestThread&) = delete;
void operator=(const TestThread&) = delete;
Expand Down
32 changes: 16 additions & 16 deletions starboard/nplb/atomic_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ struct TestOnceContext {
// The entry point for all the threads spawned during TestOnce().
template <class SbAtomicType>
void* TestOnceEntryPoint(void* raw_context) {
pthread_setname_np(pthread_self(), "TestOnceThread");
// Force every thread to sleep immediately so the first thread doesn't always
// just win.
usleep(1000);
Expand Down Expand Up @@ -376,21 +377,20 @@ TYPED_TEST(AdvancedSbAtomicTest, OnceMultipleThreads) {

// Start up kNumThreads to fight over initializing |target_data|.
TestOnceContext<TypeParam> contexts[kNumThreads] = {0};
SbThread threads[kNumThreads] = {0};
pthread_t threads[kNumThreads] = {0};
for (int i = 0; i < kNumThreads; ++i) {
contexts[i].data = data + i * kDataPerThread;
contexts[i].out_data = target_data;
contexts[i].state = &state;
contexts[i].size = kDataPerThread;
threads[i] = SbThreadCreate(
0, kSbThreadNoPriority, kSbThreadNoAffinity, true, "TestOnceThread",
TestOnceEntryPoint<TypeParam>, &(contexts[i]));
EXPECT_TRUE(SbThreadIsValid(threads[i]));
pthread_create(&threads[i], nullptr, TestOnceEntryPoint<TypeParam>,
&(contexts[i]));
EXPECT_TRUE(threads[i] != 0);
}

// Wait for all threads to complete, and clean up their resources.
for (int i = 0; i < kNumThreads; ++i) {
EXPECT_TRUE(SbThreadJoin(threads[i], NULL));
EXPECT_EQ(pthread_join(threads[i], NULL), 0);
}

// Ensure that exactly one thread initialized the data.
Expand All @@ -412,6 +412,7 @@ const int kNumIncrements = 4000;

template <class SbAtomicType>
void* IncrementEntryPoint(void* raw_context) {
pthread_setname_np(pthread_self(), "TestIncrementThread");
SbAtomicType* target = reinterpret_cast<SbAtomicType*>(raw_context);
for (int i = 0; i < kNumIncrements; ++i) {
atomic::NoBarrier_Increment(target, 1);
Expand All @@ -421,6 +422,7 @@ void* IncrementEntryPoint(void* raw_context) {

template <class SbAtomicType>
void* DecrementEntryPoint(void* raw_context) {
pthread_setname_np(pthread_self(), "TestDecrementThread");
SbAtomicType* target = reinterpret_cast<SbAtomicType*>(raw_context);
for (int i = 0; i < kNumIncrements; ++i) {
atomic::NoBarrier_Increment(target, -1);
Expand All @@ -439,27 +441,25 @@ TYPED_TEST(AdvancedSbAtomicTest, IncrementDecrementMultipleThreads) {
TypeParam value = kTestValue;

// Start up kNumThreads to fight.
SbThread threads[kNumThreads] = {0};
pthread_t threads[kNumThreads] = {0};

// First half are incrementers.
for (int i = 0; i < kNumThreads / 2; ++i) {
threads[i] = SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity,
true, "TestIncrementThread",
IncrementEntryPoint<TypeParam>, &value);
EXPECT_TRUE(SbThreadIsValid(threads[i]));
pthread_create(&threads[i], nullptr, IncrementEntryPoint<TypeParam>,
&value);
EXPECT_TRUE(threads[i] != 0);
}

// Second half are decrementers.
for (int i = kNumThreads / 2; i < kNumThreads; ++i) {
threads[i] = SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity,
true, "TestDecrementThread",
DecrementEntryPoint<TypeParam>, &value);
EXPECT_TRUE(SbThreadIsValid(threads[i]));
pthread_create(&threads[i], nullptr, DecrementEntryPoint<TypeParam>,
&value);
EXPECT_TRUE(threads[i] != 0);
}

// Wait for all threads to complete, and clean up their resources.
for (int i = 0; i < kNumThreads; ++i) {
EXPECT_TRUE(SbThreadJoin(threads[i], NULL));
EXPECT_EQ(pthread_join(threads[i], NULL), 0);
}

// |value| should be back to its original value. If the increment/decrement
Expand Down
4 changes: 2 additions & 2 deletions starboard/nplb/multiple_player_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "starboard/nplb/maximum_player_configuration_explorer.h"
#include "starboard/nplb/player_test_fixture.h"
#include "starboard/nplb/player_test_util.h"
#include "starboard/nplb/thread_helpers.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "starboard/testing/fake_graphics_context_provider.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand All @@ -36,7 +36,7 @@ typedef std::function<void(const SbPlayerTestConfig&,
FakeGraphicsContextProvider*)>
MultiplePlayerTestFunctor;

class PlayerThread : public AbstractTestThread {
class PlayerThread : public posix::AbstractTestThread {
public:
explicit PlayerThread(const std::function<void()>& functor)
: functor_(functor) {}
Expand Down
4 changes: 2 additions & 2 deletions starboard/nplb/player_write_sample_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "starboard/nplb/player_creation_param_helpers.h"
#include "starboard/nplb/player_test_fixture.h"
#include "starboard/nplb/player_test_util.h"
#include "starboard/nplb/thread_helpers.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "starboard/string.h"
#include "starboard/testing/fake_graphics_context_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -329,7 +329,7 @@ TEST_P(SbPlayerWriteSampleTest, DiscardAllAudio) {
<< ".";
}

class SecondaryPlayerTestThread : public AbstractTestThread {
class SecondaryPlayerTestThread : public posix::AbstractTestThread {
public:
SecondaryPlayerTestThread(
const SbPlayerTestConfig& config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "starboard/configuration.h"
#include "testing/gtest/include/gtest/gtest.h"

#include "starboard/nplb/thread_helpers.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "starboard/thread.h"

namespace starboard {
Expand All @@ -32,7 +32,7 @@ struct TestContext {
};

void* EntryPoint(void* parameter) {
pthread_setname_np(pthread_self(), nplb::kThreadName);
pthread_setname_np(pthread_self(), posix::kThreadName);
TestContext* context = static_cast<TestContext*>(parameter);
context->was_locked_ = (pthread_mutex_trylock(context->mutex_) == 0);
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions starboard/nplb/posix_compliance/posix_thread_get_name_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

#include <pthread.h>

#include "starboard/nplb/thread_helpers.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

void* GetThreadNameEntryPoint(void* context) {
pthread_setname_np(pthread_self(), kThreadName);
pthread_setname_np(pthread_self(), posix::kThreadName);

char name[4096] = {0};
pthread_getname_np(pthread_self(), name, SB_ARRAY_SIZE_INT(name));
Expand All @@ -39,7 +39,7 @@ TEST(PosixThreadGetNameTest, SunnyDay) {

EXPECT_TRUE(thread != 0);
EXPECT_EQ(pthread_join(thread, NULL), 0);
EXPECT_EQ(kThreadName, result);
EXPECT_EQ(posix::kThreadName, result);
}

} // namespace
Expand Down
8 changes: 4 additions & 4 deletions starboard/nplb/posix_compliance/posix_thread_set_name_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include <pthread.h>

#include "starboard/nplb/thread_helpers.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
Expand Down Expand Up @@ -45,14 +45,14 @@ void* SetThreadNameEntryPoint(void* context) {

TEST(PosixThreadSetNameTest, SunnyDay) {
Context context;
context.name_to_set = kAltThreadName;
context.name_to_set = posix::kAltThreadName;
pthread_t thread;
EXPECT_EQ(pthread_create(&thread, NULL, SetThreadNameEntryPoint, &context),
0);
EXPECT_TRUE(thread != 0);
EXPECT_EQ(pthread_join(thread, NULL), 0);
EXPECT_NE(kAltThreadName, context.got_name1);
EXPECT_EQ(kAltThreadName, context.got_name2);
EXPECT_NE(posix::kAltThreadName, context.got_name1);
EXPECT_EQ(posix::kAltThreadName, context.got_name2);
}

} // namespace
Expand Down
7 changes: 4 additions & 3 deletions starboard/nplb/recursive_mutex_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// limitations under the License.

#include "starboard/common/recursive_mutex.h"
#include "starboard/nplb/thread_helpers.h"
#include "starboard/common/condition_variable.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
Expand Down Expand Up @@ -79,7 +80,7 @@ TEST(RecursiveMutex, TryLocksInRecursion) {
EXPECT_TRUE(rmutex.AcquireTry());
}

class ThreadBlockedRecursiveMutex : public AbstractTestThread {
class ThreadBlockedRecursiveMutex : public posix::AbstractTestThread {
public:
explicit ThreadBlockedRecursiveMutex(RecursiveMutex* s) : rmutex_(s) {}
void Run() override { EXPECT_FALSE(rmutex_->AcquireTry()); }
Expand All @@ -98,7 +99,7 @@ TEST(RecursiveMutex, BlockOtherThread) {
EXPECT_TRUE(rmutex.AcquireTry());
}

class ThreadAcquiresRecursiveMutex : public AbstractTestThread {
class ThreadAcquiresRecursiveMutex : public posix::AbstractTestThread {
public:
explicit ThreadAcquiresRecursiveMutex(RecursiveMutex* s,
Mutex* cv_mutex,
Expand Down
14 changes: 6 additions & 8 deletions starboard/nplb/rwlock_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#include <unistd.h>

#include "starboard/common/rwlock.h"
#include "starboard/common/semaphore.h"
#include "starboard/common/time.h"
#include "starboard/configuration.h"
#include "starboard/nplb/thread_helpers.h"
#include "starboard/thread.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

// Increasing threads by 2x increases testing time by 3x, due to write
Expand All @@ -43,7 +43,7 @@ TEST(RWLock, Use) {
}

// Enters a RWLock as a reader and then increments a counter indicating that it
class ReadAndSignalTestThread : public AbstractTestThread {
class ReadAndSignalTestThread : public posix::AbstractTestThread {
public:
struct SharedData {
SharedData()
Expand Down Expand Up @@ -100,7 +100,7 @@ TEST(RWLock, ReadAcquisitionTwoThreads) {

// Tests the expectation that a read lock will be blocked for X milliseconds
// while the thread is holding the write lock.
class ThreadHoldsWriteLockForTime : public AbstractTestThread {
class ThreadHoldsWriteLockForTime : public posix::AbstractTestThread {
public:
struct SharedData {
explicit SharedData(int64_t time_hold) : time_to_hold(time_hold) {}
Expand Down Expand Up @@ -144,7 +144,7 @@ TEST(RWLock, FLAKY_HoldsLockForTime) {

// This thread tests RWLock by generating numbers and writing to a
// shared set<int32_t>. Additionally readbacks are interleaved in writes.
class ThreadRWLockStressTest : public AbstractTestThread {
class ThreadRWLockStressTest : public posix::AbstractTestThread {
public:
struct SharedData {
RWLock rw_lock;
Expand All @@ -159,8 +159,6 @@ class ThreadRWLockStressTest : public AbstractTestThread {
shared_data_(shared_data) {}

void Run() override {
SbThread current_thread = SbThreadGetCurrent();

for (int32_t i = begin_value_; i < end_value_; ++i) {
DoReadAll();
DoWrite(i);
Expand Down Expand Up @@ -199,7 +197,7 @@ TEST(RWLock, RWLockStressTest) {
kNumValuesEachThread * NUM_STRESS_THREADS;

ThreadRWLockStressTest::SharedData shared_data;
std::vector<AbstractTestThread*> threads;
std::vector<posix::AbstractTestThread*> threads;

for (int i = 0; i < NUM_STRESS_THREADS; ++i) {
int32_t start_value = i * kNumValuesEachThread;
Expand Down
8 changes: 4 additions & 4 deletions starboard/nplb/semaphore_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "starboard/common/semaphore.h"
#include "starboard/common/time.h"
#include "starboard/nplb/thread_helpers.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
Expand Down Expand Up @@ -49,7 +49,7 @@ TEST(Semaphore, InitialValue_One) {
EXPECT_FALSE(semaphore.TakeTry());
}

class ThreadTakesSemaphore : public AbstractTestThread {
class ThreadTakesSemaphore : public posix::AbstractTestThread {
public:
explicit ThreadTakesSemaphore(Semaphore* s) : semaphore_(s) {}
void Run() override { semaphore_->Take(); }
Expand All @@ -65,7 +65,7 @@ TEST(Semaphore, ThreadTakes) {
thread.Join();
}

class ThreadTakesWaitSemaphore : public AbstractTestThread {
class ThreadTakesWaitSemaphore : public posix::AbstractTestThread {
public:
explicit ThreadTakesWaitSemaphore(int64_t wait_us)
: thread_started_(false),
Expand Down Expand Up @@ -153,7 +153,7 @@ TEST(Semaphore, ThreadTakesWait_TimeExpires) {
EXPECT_TRUE(false) << "Thread waited, but time exceeded expectations.";
}

class ThreadPutsSemaphore : public AbstractTestThread {
class ThreadPutsSemaphore : public posix::AbstractTestThread {
public:
explicit ThreadPutsSemaphore(Semaphore* s) : semaphore_(s) {}
void Run() override { semaphore_->Put(); }
Expand Down
Loading

0 comments on commit 3e680f3

Please sign in to comment.