2020-11-24 20:55:58 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-24 20:55:58 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2022-03-29 12:48:03 +03:00
|
|
|
#include <LibMain/Main.h>
|
2021-05-22 19:47:42 +03:00
|
|
|
#include <LibThreading/Thread.h>
|
2021-07-06 15:28:19 +03:00
|
|
|
#include <errno.h>
|
2020-11-24 20:55:58 +03:00
|
|
|
#include <pthread.h>
|
2021-07-06 15:28:19 +03:00
|
|
|
#include <semaphore.h>
|
2021-03-12 19:29:37 +03:00
|
|
|
#include <unistd.h>
|
2020-11-24 20:55:58 +03:00
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
static ErrorOr<void> test_once()
|
2020-11-24 20:55:58 +03:00
|
|
|
{
|
|
|
|
constexpr size_t threads_count = 10;
|
|
|
|
|
|
|
|
static Vector<int> v;
|
|
|
|
v.clear();
|
|
|
|
pthread_once_t once = PTHREAD_ONCE_INIT;
|
2023-03-06 16:17:01 +03:00
|
|
|
Vector<NonnullRefPtr<Threading::Thread>, threads_count> threads;
|
2020-11-24 20:55:58 +03:00
|
|
|
|
|
|
|
for (size_t i = 0; i < threads_count; i++) {
|
2022-03-29 12:48:03 +03:00
|
|
|
threads.unchecked_append(TRY(Threading::Thread::try_create([&] {
|
2020-11-24 20:55:58 +03:00
|
|
|
return pthread_once(&once, [] {
|
|
|
|
v.append(35);
|
|
|
|
sleep(1);
|
|
|
|
});
|
2022-03-29 12:48:03 +03:00
|
|
|
})));
|
2023-03-06 16:17:01 +03:00
|
|
|
threads.last()->start();
|
2020-11-24 20:55:58 +03:00
|
|
|
}
|
2022-10-17 01:06:11 +03:00
|
|
|
// clang-format off
|
|
|
|
// It wants to put [[maybe_unused]] on its own line, for some reason.
|
2020-11-24 20:55:58 +03:00
|
|
|
for (auto& thread : threads)
|
2023-03-06 16:17:01 +03:00
|
|
|
[[maybe_unused]] auto res = thread->join();
|
2022-10-17 01:06:11 +03:00
|
|
|
// clang-format on
|
2020-11-24 20:55:58 +03:00
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(v.size() == 1);
|
2022-03-29 12:48:03 +03:00
|
|
|
|
|
|
|
return {};
|
2020-11-24 20:55:58 +03:00
|
|
|
}
|
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
static ErrorOr<void> test_mutex()
|
2021-07-06 20:05:39 +03:00
|
|
|
{
|
|
|
|
constexpr size_t threads_count = 10;
|
|
|
|
constexpr size_t num_times = 100;
|
|
|
|
|
|
|
|
Vector<int> v;
|
2023-03-06 16:17:01 +03:00
|
|
|
Vector<NonnullRefPtr<Threading::Thread>, threads_count> threads;
|
2021-07-06 20:05:39 +03:00
|
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < threads_count; i++) {
|
2022-03-29 12:48:03 +03:00
|
|
|
threads.unchecked_append(TRY(Threading::Thread::try_create([&] {
|
2021-07-06 20:05:39 +03:00
|
|
|
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;
|
2022-03-29 12:48:03 +03:00
|
|
|
})));
|
2023-03-06 16:17:01 +03:00
|
|
|
threads.last()->start();
|
2021-07-06 20:05:39 +03:00
|
|
|
}
|
2022-10-17 01:06:11 +03:00
|
|
|
// clang-format off
|
|
|
|
// It wants to put [[maybe_unused]] on its own line, for some reason.
|
2021-07-06 20:05:39 +03:00
|
|
|
for (auto& thread : threads)
|
2023-03-06 16:17:01 +03:00
|
|
|
[[maybe_unused]] auto res = thread->join();
|
2022-10-17 01:06:11 +03:00
|
|
|
// clang-format on
|
2021-07-06 20:05:39 +03:00
|
|
|
|
|
|
|
VERIFY(v.size() == threads_count * num_times);
|
|
|
|
VERIFY(pthread_mutex_trylock(&mutex) == 0);
|
|
|
|
VERIFY(pthread_mutex_trylock(&mutex) == EBUSY);
|
2022-03-29 12:48:03 +03:00
|
|
|
|
|
|
|
return {};
|
2021-07-06 20:05:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
static ErrorOr<void> test_semaphore_as_lock()
|
2021-07-06 15:28:19 +03:00
|
|
|
{
|
|
|
|
constexpr size_t threads_count = 10;
|
|
|
|
constexpr size_t num_times = 100;
|
|
|
|
|
|
|
|
Vector<int> v;
|
2023-03-06 16:17:01 +03:00
|
|
|
Vector<NonnullRefPtr<Threading::Thread>, threads_count> threads;
|
2021-07-06 15:28:19 +03:00
|
|
|
sem_t semaphore;
|
|
|
|
sem_init(&semaphore, 0, 1);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < threads_count; i++) {
|
2022-03-29 12:48:03 +03:00
|
|
|
threads.unchecked_append(TRY(Threading::Thread::try_create([&] {
|
2021-07-06 15:28:19 +03:00
|
|
|
for (size_t j = 0; j < num_times; j++) {
|
|
|
|
sem_wait(&semaphore);
|
|
|
|
v.append(35);
|
|
|
|
sched_yield();
|
|
|
|
sem_post(&semaphore);
|
|
|
|
sched_yield();
|
|
|
|
}
|
|
|
|
return 0;
|
2022-03-29 12:48:03 +03:00
|
|
|
})));
|
2023-03-06 16:17:01 +03:00
|
|
|
threads.last()->start();
|
2021-07-06 15:28:19 +03:00
|
|
|
}
|
2022-10-17 01:06:11 +03:00
|
|
|
// clang-format off
|
|
|
|
// It wants to put [[maybe_unused]] on its own line, for some reason.
|
2021-07-06 15:28:19 +03:00
|
|
|
for (auto& thread : threads)
|
2023-03-06 16:17:01 +03:00
|
|
|
[[maybe_unused]] auto res = thread->join();
|
2022-10-17 01:06:11 +03:00
|
|
|
// clang-format on
|
2021-07-06 15:28:19 +03:00
|
|
|
|
|
|
|
VERIFY(v.size() == threads_count * num_times);
|
|
|
|
VERIFY(sem_trywait(&semaphore) == 0);
|
2023-05-13 15:06:50 +03:00
|
|
|
VERIFY(sem_trywait(&semaphore) == -1);
|
|
|
|
VERIFY(errno == EAGAIN);
|
2022-03-29 12:48:03 +03:00
|
|
|
|
|
|
|
return {};
|
2021-07-06 15:28:19 +03:00
|
|
|
}
|
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
static ErrorOr<void> test_semaphore_as_event()
|
2021-07-06 15:28:19 +03:00
|
|
|
{
|
|
|
|
Vector<int> v;
|
|
|
|
sem_t semaphore;
|
|
|
|
sem_init(&semaphore, 0, 0);
|
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
auto reader = TRY(Threading::Thread::try_create([&] {
|
2021-07-06 15:28:19 +03:00
|
|
|
sem_wait(&semaphore);
|
|
|
|
VERIFY(v.size() == 1);
|
|
|
|
return 0;
|
2022-03-29 12:48:03 +03:00
|
|
|
}));
|
2021-07-06 15:28:19 +03:00
|
|
|
reader->start();
|
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
auto writer = TRY(Threading::Thread::try_create([&] {
|
2021-07-06 15:28:19 +03:00
|
|
|
sched_yield();
|
|
|
|
v.append(35);
|
|
|
|
sem_post(&semaphore);
|
|
|
|
return 0;
|
2022-03-29 12:48:03 +03:00
|
|
|
}));
|
2021-07-06 15:28:19 +03:00
|
|
|
writer->start();
|
|
|
|
|
|
|
|
[[maybe_unused]] auto r1 = reader->join();
|
|
|
|
[[maybe_unused]] auto r2 = writer->join();
|
|
|
|
|
2023-05-13 15:06:50 +03:00
|
|
|
VERIFY(sem_trywait(&semaphore) == -1);
|
|
|
|
VERIFY(errno == EAGAIN);
|
2022-03-29 12:48:03 +03:00
|
|
|
|
|
|
|
return {};
|
2021-07-06 15:28:19 +03:00
|
|
|
}
|
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
static ErrorOr<void> test_semaphore_nonbinary()
|
2021-07-06 15:28:19 +03:00
|
|
|
{
|
|
|
|
constexpr size_t num = 5;
|
|
|
|
constexpr size_t threads_count = 10;
|
|
|
|
constexpr size_t num_times = 100;
|
|
|
|
|
2023-03-06 16:17:01 +03:00
|
|
|
Vector<NonnullRefPtr<Threading::Thread>, threads_count> threads;
|
2021-07-06 15:28:19 +03:00
|
|
|
sem_t semaphore;
|
|
|
|
sem_init(&semaphore, 0, num);
|
|
|
|
|
|
|
|
Atomic<u32, AK::memory_order_relaxed> value = 0;
|
|
|
|
Atomic<bool, AK::memory_order_relaxed> seen_more_than_two = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < threads_count; i++) {
|
2022-03-29 12:48:03 +03:00
|
|
|
threads.unchecked_append(TRY(Threading::Thread::try_create([&] {
|
2021-07-06 15:28:19 +03:00
|
|
|
for (size_t j = 0; j < num_times; j++) {
|
|
|
|
sem_wait(&semaphore);
|
|
|
|
u32 v = 1 + value.fetch_add(1);
|
|
|
|
VERIFY(v <= num);
|
|
|
|
if (v > 2)
|
|
|
|
seen_more_than_two.store(true);
|
|
|
|
sched_yield();
|
|
|
|
value.fetch_sub(1);
|
|
|
|
sem_post(&semaphore);
|
|
|
|
}
|
|
|
|
return 0;
|
2022-03-29 12:48:03 +03:00
|
|
|
})));
|
2023-03-06 16:17:01 +03:00
|
|
|
threads.last()->start();
|
2021-07-06 15:28:19 +03:00
|
|
|
}
|
2022-10-17 01:06:11 +03:00
|
|
|
// clang-format off
|
|
|
|
// It wants to put [[maybe_unused]] on its own line, for some reason.
|
2021-07-06 15:28:19 +03:00
|
|
|
for (auto& thread : threads)
|
2023-03-06 16:17:01 +03:00
|
|
|
[[maybe_unused]] auto res = thread->join();
|
2022-10-17 01:06:11 +03:00
|
|
|
// clang-format on
|
2021-07-06 15:28:19 +03:00
|
|
|
|
|
|
|
VERIFY(value.load() == 0);
|
|
|
|
VERIFY(seen_more_than_two.load());
|
|
|
|
for (size_t i = 0; i < num; i++) {
|
|
|
|
VERIFY(sem_trywait(&semaphore) == 0);
|
|
|
|
}
|
2023-05-13 15:06:50 +03:00
|
|
|
VERIFY(sem_trywait(&semaphore) == -1);
|
|
|
|
VERIFY(errno == EAGAIN);
|
2022-03-29 12:48:03 +03:00
|
|
|
|
|
|
|
return {};
|
2021-07-06 15:28:19 +03:00
|
|
|
}
|
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments)
|
2020-11-24 20:55:58 +03:00
|
|
|
{
|
2022-03-29 12:48:03 +03:00
|
|
|
TRY(test_once());
|
|
|
|
TRY(test_mutex());
|
2021-07-06 15:28:19 +03:00
|
|
|
|
2022-03-29 12:48:03 +03:00
|
|
|
TRY(test_semaphore_as_lock());
|
|
|
|
TRY(test_semaphore_as_event());
|
|
|
|
TRY(test_semaphore_nonbinary());
|
2021-07-06 15:28:19 +03:00
|
|
|
|
2020-11-24 20:55:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|