mirror of
				https://github.com/LadybirdBrowser/ladybird.git
				synced 2025-10-31 21:30:58 +00:00 
			
		
		
		
	 1682f0b760
			
		
	
	
		1682f0b760
		
	
	
	
	
		
			
			SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
		
			
				
	
	
		
			48 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <LibThread/Thread.h>
 | |
| #include <pthread.h>
 | |
| #include <string.h>
 | |
| #include <unistd.h>
 | |
| 
 | |
| LibThread::Thread::Thread(Function<int()> action, StringView thread_name)
 | |
|     : Core::Object(nullptr)
 | |
|     , m_action(move(action))
 | |
|     , m_thread_name(thread_name.is_null() ? "" : thread_name)
 | |
| {
 | |
|     register_property("thread_name", [&] { return JsonValue { m_thread_name }; });
 | |
|     register_property("tid", [&] { return JsonValue { m_tid }; });
 | |
| }
 | |
| 
 | |
| LibThread::Thread::~Thread()
 | |
| {
 | |
|     if (m_tid) {
 | |
|         dbgln("Destroying thread \"{}\"({}) while it is still running!", m_thread_name, m_tid);
 | |
|         [[maybe_unused]] auto res = join();
 | |
|     }
 | |
| }
 | |
| 
 | |
| void LibThread::Thread::start()
 | |
| {
 | |
|     int rc = pthread_create(
 | |
|         &m_tid,
 | |
|         nullptr,
 | |
|         [](void* arg) -> void* {
 | |
|             Thread* self = static_cast<Thread*>(arg);
 | |
|             int exit_code = self->m_action();
 | |
|             self->m_tid = 0;
 | |
|             return (void*)exit_code;
 | |
|         },
 | |
|         static_cast<void*>(this));
 | |
| 
 | |
|     VERIFY(rc == 0);
 | |
|     if (!m_thread_name.is_empty()) {
 | |
|         rc = pthread_setname_np(m_tid, m_thread_name.characters());
 | |
|         VERIFY(rc == 0);
 | |
|     }
 | |
|     dbgln("Started thread \"{}\", tid = {}", m_thread_name, m_tid);
 | |
| }
 |