diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index e24c852b90..8cb8521ede 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -199,7 +199,7 @@ target_link_libraries(telws LibProtocol LibLine LibMain) target_link_libraries(test-bindtodevice LibMain) target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell LibMain) target_link_libraries(test-imap LibIMAP LibMain) -target_link_libraries(test-pthread LibThreading) +target_link_libraries(test-pthread LibThreading LibMain) target_link_libraries(timezone LibMain) target_link_libraries(top LibMain) target_link_libraries(touch LibMain) diff --git a/Userland/Utilities/test-pthread.cpp b/Userland/Utilities/test-pthread.cpp index 0f3397aa97..0c15f30c88 100644 --- a/Userland/Utilities/test-pthread.cpp +++ b/Userland/Utilities/test-pthread.cpp @@ -6,13 +6,14 @@ #include #include +#include #include #include #include #include #include -static void test_once() +static ErrorOr test_once() { constexpr size_t threads_count = 10; @@ -22,21 +23,23 @@ static void test_once() NonnullRefPtrVector threads; for (size_t i = 0; i < threads_count; i++) { - threads.append(Threading::Thread::construct([&] { + threads.unchecked_append(TRY(Threading::Thread::try_create([&] { return pthread_once(&once, [] { v.append(35); sleep(1); }); - })); + }))); threads.last().start(); } for (auto& thread : threads) [[maybe_unused]] auto res = thread.join(); VERIFY(v.size() == 1); + + return {}; } -static void test_mutex() +static ErrorOr test_mutex() { constexpr size_t threads_count = 10; constexpr size_t num_times = 100; @@ -46,7 +49,7 @@ static void test_mutex() pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; for (size_t i = 0; i < threads_count; i++) { - threads.append(Threading::Thread::construct([&] { + threads.unchecked_append(TRY(Threading::Thread::try_create([&] { for (size_t j = 0; j < num_times; j++) { pthread_mutex_lock(&mutex); v.append(35); @@ -55,7 +58,7 @@ static void test_mutex() sched_yield(); } return 0; - })); + }))); threads.last().start(); } for (auto& thread : threads) @@ -64,9 +67,11 @@ static void test_mutex() VERIFY(v.size() == threads_count * num_times); VERIFY(pthread_mutex_trylock(&mutex) == 0); VERIFY(pthread_mutex_trylock(&mutex) == EBUSY); + + return {}; } -static void test_semaphore_as_lock() +static ErrorOr test_semaphore_as_lock() { constexpr size_t threads_count = 10; constexpr size_t num_times = 100; @@ -77,7 +82,7 @@ static void test_semaphore_as_lock() sem_init(&semaphore, 0, 1); for (size_t i = 0; i < threads_count; i++) { - threads.append(Threading::Thread::construct([&] { + threads.unchecked_append(TRY(Threading::Thread::try_create([&] { for (size_t j = 0; j < num_times; j++) { sem_wait(&semaphore); v.append(35); @@ -86,7 +91,7 @@ static void test_semaphore_as_lock() sched_yield(); } return 0; - })); + }))); threads.last().start(); } for (auto& thread : threads) @@ -95,36 +100,40 @@ static void test_semaphore_as_lock() VERIFY(v.size() == threads_count * num_times); VERIFY(sem_trywait(&semaphore) == 0); VERIFY(sem_trywait(&semaphore) == EAGAIN); + + return {}; } -static void test_semaphore_as_event() +static ErrorOr test_semaphore_as_event() { Vector v; sem_t semaphore; sem_init(&semaphore, 0, 0); - auto reader = Threading::Thread::construct([&] { + auto reader = TRY(Threading::Thread::try_create([&] { sem_wait(&semaphore); VERIFY(v.size() == 1); return 0; - }); + })); reader->start(); - auto writer = Threading::Thread::construct([&] { + auto writer = TRY(Threading::Thread::try_create([&] { sched_yield(); v.append(35); sem_post(&semaphore); return 0; - }); + })); writer->start(); [[maybe_unused]] auto r1 = reader->join(); [[maybe_unused]] auto r2 = writer->join(); VERIFY(sem_trywait(&semaphore) == EAGAIN); + + return {}; } -static void test_semaphore_nonbinary() +static ErrorOr test_semaphore_nonbinary() { constexpr size_t num = 5; constexpr size_t threads_count = 10; @@ -138,7 +147,7 @@ static void test_semaphore_nonbinary() Atomic seen_more_than_two = false; for (size_t i = 0; i < threads_count; i++) { - threads.append(Threading::Thread::construct([&] { + threads.unchecked_append(TRY(Threading::Thread::try_create([&] { for (size_t j = 0; j < num_times; j++) { sem_wait(&semaphore); u32 v = 1 + value.fetch_add(1); @@ -150,7 +159,7 @@ static void test_semaphore_nonbinary() sem_post(&semaphore); } return 0; - })); + }))); threads.last().start(); } @@ -163,16 +172,18 @@ static void test_semaphore_nonbinary() VERIFY(sem_trywait(&semaphore) == 0); } VERIFY(sem_trywait(&semaphore) == EAGAIN); + + return {}; } -int main() +ErrorOr serenity_main(Main::Arguments) { - test_once(); - test_mutex(); + TRY(test_once()); + TRY(test_mutex()); - test_semaphore_as_lock(); - test_semaphore_as_event(); - test_semaphore_nonbinary(); + TRY(test_semaphore_as_lock()); + TRY(test_semaphore_as_event()); + TRY(test_semaphore_nonbinary()); return 0; }