2020-11-24 20:55:58 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-24 20:55:58 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
|
|
|
#include <LibThread/Thread.h>
|
|
|
|
#include <pthread.h>
|
2021-03-12 17:29:37 +01:00
|
|
|
#include <unistd.h>
|
2020-11-24 20:55:58 +03:00
|
|
|
|
|
|
|
static void test_once()
|
|
|
|
{
|
|
|
|
constexpr size_t threads_count = 10;
|
|
|
|
|
|
|
|
static Vector<int> v;
|
|
|
|
v.clear();
|
|
|
|
pthread_once_t once = PTHREAD_ONCE_INIT;
|
|
|
|
NonnullRefPtrVector<LibThread::Thread, threads_count> threads;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < threads_count; i++) {
|
|
|
|
threads.append(LibThread::Thread::construct([&] {
|
|
|
|
return pthread_once(&once, [] {
|
|
|
|
v.append(35);
|
|
|
|
sleep(1);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
threads.last().start();
|
|
|
|
}
|
|
|
|
for (auto& thread : threads)
|
LibThread: Improve semantics of Thread::join, and remove Thread::quit.
Thread::quit was created before the pthread_create_helper in pthread.cpp
that automagically calls pthread_exit from all pthreads after the user's
thread function exits. It is unused, and unecessary now.
Cleanup some logging, and make join return a Result<T, ThreadError>.
This also adds a new type, LibThread::ThreadError as an
AK::DistinctNumeric. Hopefully, this will make it possible to have a
Result<int, ThreadError> and have it compile? It also makes it clear
that the int there is an error at the call site.
By default, the T on join is void, meaning the caller doesn't care about
the return value from the thread.
As Result is a [[nodiscard]] type, also change the current caller of
join to explicitly ignore it.
Move the logging out of join as well, as it's the user's
responsibility whether to log or not.
2020-12-31 20:56:04 -07:00
|
|
|
[[maybe_unused]] auto res = thread.join();
|
2020-11-24 20:55:58 +03:00
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(v.size() == 1);
|
2020-11-24 20:55:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
test_once();
|
|
|
|
return 0;
|
|
|
|
}
|