1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 11:28:11 +00:00

test-pthread: Add a mutex test

This commit is contained in:
Sergey Bugaev 2021-07-06 20:05:39 +03:00 committed by Andreas Kling
parent 65b0642dd0
commit e8d5b16733

View file

@ -36,6 +36,36 @@ static void test_once()
VERIFY(v.size() == 1);
}
static void test_mutex()
{
constexpr size_t threads_count = 10;
constexpr size_t num_times = 100;
Vector<int> v;
NonnullRefPtrVector<Threading::Thread, threads_count> threads;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
for (size_t i = 0; i < threads_count; i++) {
threads.append(Threading::Thread::construct([&] {
for (size_t j = 0; j < num_times; j++) {
pthread_mutex_lock(&mutex);
v.append(35);
sched_yield();
pthread_mutex_unlock(&mutex);
sched_yield();
}
return 0;
}));
threads.last().start();
}
for (auto& thread : threads)
[[maybe_unused]] auto res = thread.join();
VERIFY(v.size() == threads_count * num_times);
VERIFY(pthread_mutex_trylock(&mutex) == 0);
VERIFY(pthread_mutex_trylock(&mutex) == EBUSY);
}
static void test_semaphore_as_lock()
{
constexpr size_t threads_count = 10;
@ -138,6 +168,7 @@ static void test_semaphore_nonbinary()
int main()
{
test_once();
test_mutex();
test_semaphore_as_lock();
test_semaphore_as_event();