docs
Loading...
Searching...
No Matches
Thread.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
7
8namespace ultralight {
9
10///
11/// Unique id of the thread, used for referencing the created thread later.
12/// * on Windows this should be the thread identifier (not the HANDLE that _beginthreadex()
13/// returns): the id _beginthreadex() writes through its `thrdaddr` out-parameter, or
14/// GetCurrentThreadId() called on the new thread
15/// * on POSIX this can be whatever unique id you want
16///
17typedef uint32_t ThreadId;
18
19///
20/// Platform-specific handle
21/// * on Windows this is HANDLE
22/// * on POSIX this is pthread_t
23///
24typedef uint64_t ThreadHandle;
25
26///
27/// Entry point for the thread, this function should be called by the thread once it is active
28/// and should be passed entry_point_data as the argument.
29///
30typedef void (*ThreadEntryPoint)(void*);
31
32///
33/// The type of thread, you can choose to optionally handle these for better performance.
34///
44
45///
46/// Result of creating a new thread.
47///
48/// This struct is used to return the id and handle of the created thread.
49///
51 ThreadId id; ///< The unique id of the thread. @see ThreadId
52 ThreadHandle handle; ///< The platform-specific handle of the thread. @see ThreadHandle
53};
54
55///
56/// User-defined factory for creating new threads.
57///
58/// You can implement this interface so that the library will use your own implementation for
59/// creating threads (useful for tracking thread creation, setting thread names, etc).
60///
61/// The factory covers the threads that run page content: script workers, the JavaScript compiler
62/// and garbage collector, networking, and resource loading. The library starts a few threads
63/// outside the factory (media playback and some background rendering work), and the libraries it
64/// links start their own, so a factory won't account for every thread in the process.
65///
66/// ## Default Implementation
67///
68/// When no factory is defined, the library will create threads using the default platform-specific
69/// thread creation functions (eg, `_beginthreadex()` on Windows, `pthread_create()` on POSIX).
70///
71/// ## Setting the Thread Factory
72///
73/// To provide your own custom ThreadFactory implementation, you should inherit from this class,
74/// handle the virtual member functions, and then pass an instance to
75/// Platform::set_thread_factory().
76///
78 public:
79 virtual ~ThreadFactory() = default;
80
81 ///
82 /// Create a new thread.
83 ///
84 /// @param name The name of the thread (can be nullptr).
85 /// @param type The type of thread.
86 /// @param entry_point The entry point for the thread.
87 /// @param entry_point_data The data to pass to the entry point.
88 /// @param result The resulting id and handle of the thread creation.
89 ///
90 /// @return Returns whether or not the thread was created successfully.
91 ///
92 virtual bool CreateThread(const char* name, ThreadType type, ThreadEntryPoint entry_point,
93 void* entry_point_data, CreateThreadResult& result) = 0;
94};
95
96} // namespace ultralight
#define UExport
Definition Exports.h:22
User-defined factory for creating new threads.
Definition Thread.h:77
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
Root namespace for every public Ultralight type, function, and enumeration.
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:30
uint32_t ThreadId
Unique id of the thread, used for referencing the created thread later.
Definition Thread.h:17
uint64_t ThreadHandle
Platform-specific handle.
Definition Thread.h:24
ThreadType
The type of thread, you can choose to optionally handle these for better performance.
Definition Thread.h:35
@ Compiler
Definition Thread.h:38
@ JavaScript
Definition Thread.h:37
@ Audio
Definition Thread.h:42
@ Graphics
Definition Thread.h:41
@ Network
Definition Thread.h:40
@ GarbageCollection
Definition Thread.h:39
Result of creating a new thread.
Definition Thread.h:50
ThreadId id
The unique id of the thread.
Definition Thread.h:51
ThreadHandle handle
The platform-specific handle of the thread.
Definition Thread.h:52