summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2021-07-06 20:05:39 +0300
committerAndreas Kling <kling@serenityos.org>2021-07-07 17:08:40 +0200
commite8d5b167339517661556d294abd17133592c84fa (patch)
treed0cbe40d377c88f17f064c18dc72bee425ad81cd
parent65b0642dd0dd7a13436a17c1df6a9cece0619ede (diff)
downloadserenity-e8d5b167339517661556d294abd17133592c84fa.zip
test-pthread: Add a mutex test
-rw-r--r--Userland/Utilities/test-pthread.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/Userland/Utilities/test-pthread.cpp b/Userland/Utilities/test-pthread.cpp
index b1684dc795..0f3397aa97 100644
--- a/Userland/Utilities/test-pthread.cpp
+++ b/Userland/Utilities/test-pthread.cpp
@@ -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();