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);
91 DAWNWARN(
"worker thread stack size %zu rejected (%d), using default\n",
103 ret = pthread_getschedparam(pthread_self(), &policy, ¶m);
106 DAWNERR(
"Failed to read creator thread scheduler (%d)\n", ret);
115 minPrio = sched_get_priority_min(policy);
116 maxPrio = sched_get_priority_max(policy);
117 if (minPrio < 0 || maxPrio < 0)
121 err = errno != 0 ? errno : EINVAL;
122 DAWNERR(
"Failed to query priority range for scheduler %d\n", policy);
126 priority = param.sched_priority;
131 else if (priority < minPrio || priority > maxPrio)
136 if (priority < minPrio || priority > maxPrio)
138 DAWNERR(
"Worker thread priority %d is outside scheduler %d range [%d, %d]\n",
146 param.sched_priority = priority;
148 ret = pthread_attr_setschedpolicy(&attr, policy);
151 DAWNERR(
"Failed to set worker thread scheduler %d (%d)\n", policy, ret);
155 ret = pthread_attr_setschedparam(&attr, ¶m);
158 DAWNERR(
"Failed to set worker thread priority %d (%d)\n", priority, ret);
162 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
165 DAWNERR(
"Failed to enable explicit worker thread scheduling (%d)\n", ret);
182 DAWNERR(
"Thread already running\n");
195 DAWNERR(
"Thread function not set before calling threadStart()\n");
202 ret = buildThreadAttr(attr, needsDestroy);
209 destroyRet = pthread_attr_destroy(&attr);
212 DAWNERR(
"Failed to destroy worker thread attributes (%d)\n", destroyRet);
221 ret = pthread_create(&th, needsDestroy ? &attr :
nullptr, &CThreadedObject::threadEntry,
this);
227 destroyRet = pthread_attr_destroy(&attr);
230 DAWNERR(
"Failed to destroy worker thread attributes (%d)\n", destroyRet);
236 DAWNERR(
"Failed to create worker thread (%d)\n", ret);
267void CThreadedObject::threadWrapper()
277void *CThreadedObject::threadEntry(
void *arg)
284 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).