| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2020-01-24 16:45:29 +03:00
										 |  |  |  * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 18:55:56 +03:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												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
										 |  |  | #include <AK/DistinctNumeric.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-25 18:55:56 +03:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
											
												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
										 |  |  | #include <AK/Result.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-07 12:49:05 -07:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/Object.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-30 17:46:13 -06:00
										 |  |  | #include <pthread.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-25 18:55:56 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace LibThread { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-26 19:09:04 +02:00
										 |  |  | TYPEDEF_DISTINCT_ORDERED_ID(intptr_t, ThreadError); | 
					
						
							| 
									
										
										
											
												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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | class Thread final : public Core::Object { | 
					
						
							| 
									
										
										
										
											2019-08-25 18:55:56 +03:00
										 |  |  |     C_OBJECT(Thread); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     virtual ~Thread(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void start(); | 
					
						
							| 
									
										
										
											
												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
										 |  |  | 
 | 
					
						
							|  |  |  |     template<typename T = void> | 
					
						
							|  |  |  |     Result<T, ThreadError> join(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     String thread_name() const { return m_thread_name; } | 
					
						
							| 
									
										
										
										
											2020-09-26 11:23:49 +03:00
										 |  |  |     pthread_t tid() const { return m_tid; } | 
					
						
							| 
									
										
										
										
											2019-08-25 18:55:56 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-04-26 19:09:04 +02:00
										 |  |  |     explicit Thread(Function<intptr_t()> action, StringView thread_name = nullptr); | 
					
						
							|  |  |  |     Function<intptr_t()> m_action; | 
					
						
							| 
									
										
										
										
											2020-08-16 15:28:56 -07:00
										 |  |  |     pthread_t m_tid { 0 }; | 
					
						
							| 
									
										
										
										
											2019-12-07 12:49:05 -07:00
										 |  |  |     String m_thread_name; | 
					
						
							| 
									
										
										
										
											2019-08-25 18:55:56 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												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
										 |  |  | template<typename T> | 
					
						
							|  |  |  | Result<T, ThreadError> Thread::join() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void* thread_return = nullptr; | 
					
						
							|  |  |  |     int rc = pthread_join(m_tid, &thread_return); | 
					
						
							|  |  |  |     if (rc != 0) { | 
					
						
							|  |  |  |         return ThreadError { rc }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_tid = 0; | 
					
						
							| 
									
										
										
										
											2021-04-10 18:29:06 +04:30
										 |  |  |     if constexpr (IsVoid<T>) | 
					
						
							| 
									
										
										
											
												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
										 |  |  |         return {}; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         return { static_cast<T>(thread_return) }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 18:55:56 +03:00
										 |  |  | } |