mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
test-pthread: Port to LibMain and let local functions return ErrorOr<T>
This ports 'test-pthread' to LibMain and converts the local functions of the program to return ErrorOr<T>.
This commit is contained in:
parent
d69f3aa958
commit
f4aef35e6e
Notes:
sideshowbarker
2024-07-17 17:06:59 +09:00
Author: https://github.com/kennethmyhra Commit: https://github.com/SerenityOS/serenity/commit/f4aef35e6e Pull-request: https://github.com/SerenityOS/serenity/pull/13341 Issue: https://github.com/SerenityOS/serenity/issues/11609 Reviewed-by: https://github.com/kleinesfilmroellchen ✅
@ -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)
|
||||
|
@ -6,13 +6,14 @@
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/NonnullRefPtrVector.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <LibThreading/Thread.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void test_once()
|
||||
static ErrorOr<void> test_once()
|
||||
{
|
||||
constexpr size_t threads_count = 10;
|
||||
|
||||
@ -22,21 +23,23 @@ static void test_once()
|
||||
NonnullRefPtrVector<Threading::Thread, threads_count> 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<void> 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<void> 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<void> test_semaphore_as_event()
|
||||
{
|
||||
Vector<int> 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<void> test_semaphore_nonbinary()
|
||||
{
|
||||
constexpr size_t num = 5;
|
||||
constexpr size_t threads_count = 10;
|
||||
@ -138,7 +147,7 @@ static void test_semaphore_nonbinary()
|
||||
Atomic<bool, AK::memory_order_relaxed> 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<int> 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user