MCPcopy Create free account
hub / github.com/baidu/tera / ThreadProc

Method ThreadProc

src/common/thread_pool.h:181–234  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

179 return NULL;
180 }
181 void ThreadProc() {
182 while (true) {
183 Task task;
184 MutexLock lock(&mutex_, "ThreadProc");
185 while (time_queue_.empty() && queue_.empty() && !stop_) {
186 work_cv_.Wait("ThreadProcWait");
187 }
188 if (stop_) {
189 break;
190 }
191 // Timer task
192 if (!time_queue_.empty()) {
193 int64_t now_time = get_micros();
194 BGItem bg_item = time_queue_.top();
195 int64_t wait_time = bg_item.exe_time - now_time; // in us
196 if (wait_time <= 0) {
197 time_queue_.pop();
198 BGMap::iterator it = latest_.find(bg_item.id);
199 if (it != latest_.end() && it->second.exe_time == bg_item.exe_time) {
200 schedule_cost_sum_ += now_time - bg_item.exe_time;
201 schedule_count_++;
202 task = bg_item.task;
203 latest_.erase(it);
204 running_task_ids_.insert(bg_item.id);
205 mutex_.Unlock();
206 task(bg_item.id);
207 mutex_.Lock("ThreadProcRelock");
208 task_cost_sum_ += get_micros() - now_time;
209 task_count_++;
210 running_task_ids_.erase(bg_item.id);
211 }
212 continue;
213 } else if (queue_.empty() && !stop_) {
214 work_cv_.TimeWaitInUs(wait_time, "ThreadProcTimeWait");
215 continue;
216 }
217 }
218 // Normal task;
219 if (!queue_.empty()) {
220 task = queue_.front().task;
221 int64_t exe_time = queue_.front().exe_time;
222 queue_.pop_front();
223 --pending_num_;
224 int64_t start_time = get_micros();
225 schedule_cost_sum_ += start_time - exe_time;
226 schedule_count_++;
227 mutex_.Unlock();
228 task(0);
229 mutex_.Lock("ThreadProcRelock2");
230 task_cost_sum_ += get_micros() - start_time;
231 task_count_++;
232 }
233 }
234 }
235
236 private:
237 struct BGItem {

Callers 1

ThreadWrapperMethod · 0.80

Calls 7

insertMethod · 0.80
TimeWaitInUsMethod · 0.80
get_microsFunction · 0.70
emptyMethod · 0.45
WaitMethod · 0.45
UnlockMethod · 0.45
LockMethod · 0.45

Tested by

no test coverage detected