6#include "dawn/common/thread.hxx"
12#include "dawn/debug.hxx"
27int CThreadedObject::joinThread()
36 ret = pthread_join(th,
nullptr);
39 DAWNERR(
"Failed to join worker thread (%d)\n", ret);
44 std::memset(&th, 0,
sizeof(th));
48int CThreadedObject::buildThreadAttr(pthread_attr_t &attr,
bool &needsDestroy)
const
50 struct sched_param param;
61 DAWNERR(
"Invalid worker thread priority %d\n", threadConfig.
priority);
67 DAWNERR(
"Invalid worker thread scheduler %d\n", threadConfig.
scheduler);
71 ret = pthread_attr_init(&attr);
74 DAWNERR(
"Failed to initialize worker thread attributes (%d)\n", ret);
82 ret = pthread_attr_setstacksize(&attr, threadConfig.
stackSize);
86 "Failed to set worker thread stack size to %zu (%d)\n", threadConfig.
stackSize, ret);
97 ret = pthread_getschedparam(pthread_self(), &policy, ¶m);
100 DAWNERR(
"Failed to read creator thread scheduler (%d)\n", ret);
109 minPrio = sched_get_priority_min(policy);
110 maxPrio = sched_get_priority_max(policy);
111 if (minPrio < 0 || maxPrio < 0)
115 err = errno != 0 ? errno : EINVAL;
116 DAWNERR(
"Failed to query priority range for scheduler %d\n", policy);
120 priority = param.sched_priority;
125 else if (priority < minPrio || priority > maxPrio)
130 if (priority < minPrio || priority > maxPrio)
132 DAWNERR(
"Worker thread priority %d is outside scheduler %d range [%d, %d]\n",
140 param.sched_priority = priority;
142 ret = pthread_attr_setschedpolicy(&attr, policy);
145 DAWNERR(
"Failed to set worker thread scheduler %d (%d)\n", policy, ret);
149 ret = pthread_attr_setschedparam(&attr, ¶m);
152 DAWNERR(
"Failed to set worker thread priority %d (%d)\n", priority, ret);
156 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
159 DAWNERR(
"Failed to enable explicit worker thread scheduling (%d)\n", ret);
176 DAWNERR(
"Thread already running\n");
189 DAWNERR(
"Thread function not set before calling threadStart()\n");
196 ret = buildThreadAttr(attr, needsDestroy);
203 destroyRet = pthread_attr_destroy(&attr);
206 DAWNERR(
"Failed to destroy worker thread attributes (%d)\n", destroyRet);
215 ret = pthread_create(&th, needsDestroy ? &attr :
nullptr, &CThreadedObject::threadEntry,
this);
221 destroyRet = pthread_attr_destroy(&attr);
224 DAWNERR(
"Failed to destroy worker thread attributes (%d)\n", destroyRet);
230 DAWNERR(
"Failed to create worker thread (%d)\n", ret);
261void CThreadedObject::threadWrapper()
271void *CThreadedObject::threadEntry(
void *arg)
278 self->threadWrapper();
Portable thread owner abstraction for Dawn components.
bool isRunning() const
Check if the worker thread is running.
int threadStop()
Stop the worker thread.
virtual ~CThreadedObject()
Destructor - cleans up thread resources.
static int THREAD_SCHEDULER_DEFAULT
Default scheduler behavior.
int threadStart()
Start the worker thread.
static int THREAD_PRIORITY_DEFAULT
Default thread priority behavior.
Out-of-tree user-extension hooks for Dawn.
int priority
Requested thread priority (0 = creator default).
size_t stackSize
Requested stack size in bytes (0 = OS default).
int scheduler
Requested scheduler policy (-1 = creator default).