MCPcopy Create free account
hub / github.com/AGenUI/AGenUI / workerThreadLoop

Method workerThreadLoop

core/src/module/agenui_message_thread.cpp:118–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

116}
117
118void MessageThread::workerThreadLoop() {
119 // Set thread name for debugger identification
120#if defined(__APPLE__)
121 pthread_setname_np(_name.c_str());
122#elif defined(__linux__) || defined(__ANDROID__) || defined(HARMONY)
123 pthread_setname_np(pthread_self(), _name.c_str());
124#endif
125
126 AGENUI_LOG("[%s] worker loop started", _name.c_str());
127
128 while (!_shouldStop) {
129 std::function<void()> task;
130 bool hasTask = false;
131
132 // Dequeue a task
133 {
134 std::unique_lock<std::mutex> lock(_queueMutex);
135
136 // Promote expired delayed tasks
137 processDelayedTasks();
138
139 // Compute wait duration
140 std::chrono::milliseconds waitTime(100); // default 100ms
141 if (!_delayedTaskQueue.empty()) {
142 auto now = std::chrono::steady_clock::now();
143 auto nextTaskTime = _delayedTaskQueue.top().executeTime;
144 if (nextTaskTime > now) {
145 waitTime = std::chrono::duration_cast<std::chrono::milliseconds>(nextTaskTime - now);
146 }
147 }
148
149 // Wait for a task or stop signal
150 if (_taskQueue.empty() && !_shouldStop) {
151 _condition.wait_for(lock, waitTime, [this] {
152 return !_taskQueue.empty() || _shouldStop;
153 });
154 }
155
156 // Exit if stopped and all queues are empty
157 if (_shouldStop && _taskQueue.empty() && _delayedTaskQueue.empty()) {
158 break;
159 }
160
161 // Dequeue next task
162 if (!_taskQueue.empty()) {
163 task = _taskQueue.front();
164 _taskQueue.pop();
165 hasTask = true;
166 }
167 }
168
169 // Execute
170 if (hasTask && task) {
171 task();
172 }
173 }
174
175 AGENUI_LOG("[%s] worker loop stopped", _name.c_str());

Callers

nothing calls this directly

Calls 2

c_strMethod · 0.80
emptyMethod · 0.80

Tested by

no test coverage detected