Loading...
Searching...
No Matches
Thread.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2024 Ultralight, Inc. *
7 **************************************************************************************************/
8#pragma once
10
11namespace ultralight {
12
13///
14/// Unique id of the thread, used for referencing the created thread later.
15/// * on Windows this should match the thread identifier returned by either _beginthreadex()
16/// or GetCurrentThreadId()
17/// * on POSIX this can be whatever unique id you want
18///
19typedef uint32_t ThreadId;
20
21///
22/// Platform-specific handle
23/// * on Windows this is HANDLE
24/// * on POSIX this is pthread_t
25///
26typedef uint64_t ThreadHandle;
27
28///
29/// Entry point for the thread, this function should be called by the thread once it is active
30/// and should be passed entry_point_data as the argument.
31///
32typedef void (*ThreadEntryPoint)(void*);
33
34///
35/// The type of thread, you can choose to optionally handle these for better performance.
36///
37enum class ThreadType : uint8_t {
38 Unknown = 0,
42 Network,
44 Audio,
45};
46
47///
48/// Result of creating a new thread.
49///
50/// This struct is used to return the id and handle of the created thread.
51///
53 ThreadId id; ///< The unique id of the thread. @see ThreadId
54 ThreadHandle handle; ///< The platform-specific handle of the thread. @see ThreadHandle
55};
56
57///
58/// User-defined factory for creating new threads.
59///
60/// You can implement this interface so that the library will use your own implementation for
61/// creating threads (useful for tracking thread creation, setting thread names, etc).
62///
63/// ## Default Implementation
64///
65/// When no factory is defined, the library will create threads using the default platform-specific
66/// thread creation functions (eg, `_beginthreadex()` on Windows, `pthread_create()` on POSIX).
67///
68/// ## Setting the Thread Factory
69///
70/// To provide your own custom ThreadFactory implementation, you should inherit from this class,
71/// handle the virtual member functions, and then pass an instance to
72/// Platform::set_thread_factory().
73///
75 public:
76 virtual ~ThreadFactory() = default;
77
78 ///
79 /// Create a new thread.
80 ///
81 /// @param name The name of the thread (can be nullptr).
82 /// @param type The type of thread.
83 /// @param entry_point The entry point for the thread.
84 /// @param entry_point_data The data to pass to the entry point.
85 /// @param result The resulting id and handle of the thread creation.
86 ///
87 /// @return Returns whether or not the thread was created successfully.
88 ///
89 virtual bool CreateThread(const char* name, ThreadType type, ThreadEntryPoint entry_point,
90 void* entry_point_data, CreateThreadResult& result) = 0;
91};
92
93} // namespace ultralight
#define UExport
Definition Exports.h:25
User-defined factory for creating new threads.
Definition Thread.h:74
virtual bool CreateThread(const char *name, ThreadType type, ThreadEntryPoint entry_point, void *entry_point_data, CreateThreadResult &result)=0
Create a new thread.
virtual ~ThreadFactory()=default
Definition App.h:14
uint32_t ThreadId
Unique id of the thread, used for referencing the created thread later.
Definition Thread.h:19
uint64_t ThreadHandle
Platform-specific handle.
Definition Thread.h:26
ThreadType
The type of thread, you can choose to optionally handle these for better performance.
Definition Thread.h:37
void(* ThreadEntryPoint)(void *)
Entry point for the thread, this function should be called by the thread once it is active and should...
Definition Thread.h:32
Result of creating a new thread.
Definition Thread.h:52
ThreadId id
The unique id of the thread.
Definition Thread.h:53
ThreadHandle handle
The platform-specific handle of the thread.
Definition Thread.h:54