| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef DMLC_THREAD_GROUP_H_ |
| 7 |
#define DMLC_THREAD_GROUP_H_ |
| 8 |
|
| 9 |
#include <memory> |
| 10 |
#include <mutex> |
| 11 |
#include <set> |
| 12 |
#include <string> |
| 13 |
#include <thread> |
| 14 |
#include <unordered_map> |
| 15 |
#include <unordered_set> |
| 16 |
#include <utility> |
| 17 |
|
| 18 |
#include <dmlc/blockingconcurrentqueue.h> |
| 19 |
#include <dmlc/concurrentqueue.h> |
| 20 |
#include <dmlc/logging.h> |
| 21 |
#if defined(DMLC_USE_CXX14) || __cplusplus > 201103L |
| 22 |
#include <shared_mutex> |
| 23 |
#endif |
| 24 |
#include <condition_variable> |
| 25 |
#ifdef __linux__ |
| 26 |
#include <sys/syscall.h> |
| 27 |
#include <unistd.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
namespace dmlc { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ManualEvent { |
| 36 |
public: |
| 37 |
ManualEvent() : signaled_(false) {} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
void wait() { |
| 44 |
std::unique_lock<std::mutex> lock(mutex_); |
| 45 |
if (!signaled_) { |
| 46 |
condition_variable_.wait(lock); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
void signal() { |
| 54 |
signaled_ = true; |
| 55 |
std::unique_lock<std::mutex> lk(mutex_); |
| 56 |
condition_variable_.notify_all(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
void reset() { |
| 63 |
std::unique_lock<std::mutex> lk(mutex_); |
| 64 |
signaled_ = false; |
| 65 |
} |
| 66 |
|
| 67 |
private: |
| 68 |
|
| 69 |
std::mutex mutex_; |
| 70 |
|
| 71 |
std::condition_variable condition_variable_; |
| 72 |
|
| 73 |
std::atomic<bool> signaled_; |
| 74 |
}; |
| 75 |
|
| 76 |
#if defined(DMLC_USE_CXX14) || __cplusplus > 201103L |
| 77 |
|
| 78 |
using SharedMutex = std::shared_timed_mutex; |
| 79 |
|
| 80 |
using WriteLock = std::unique_lock<SharedMutex>; |
| 81 |
|
| 82 |
using ReadLock = std::shared_lock<SharedMutex>; |
| 83 |
#else |
| 84 |
|
| 85 |
using SharedMutex = std::recursive_mutex; |
| 86 |
|
| 87 |
using WriteLock = std::unique_lock<SharedMutex>; |
| 88 |
|
| 89 |
using ReadLock = std::unique_lock<SharedMutex>; |
| 90 |
#endif |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
class ThreadGroup { |
| 97 |
public: |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
class Thread { |
| 103 |
public: |
| 104 |
|
| 105 |
using SharedPtr = std::shared_ptr<Thread>; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
Thread(std::string threadName, ThreadGroup *owner, std::thread *thrd = nullptr) |
| 114 |
: name_(std::move(threadName)), |
| 115 |
thread_(thrd), |
| 116 |
ready_event_(std::make_shared<ManualEvent>()), |
| 117 |
start_event_(std::make_shared<ManualEvent>()), |
| 118 |
owner_(owner), |
| 119 |
shutdown_requested_(false), |
| 120 |
auto_remove_(false) { |
| 121 |
CHECK_NOTNULL(owner); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
virtual ~Thread() { |
| 128 |
const bool self_delete = is_current_thread(); |
| 129 |
if (!self_delete) { |
| 130 |
request_shutdown(); |
| 131 |
internal_join(true); |
| 132 |
} |
| 133 |
WriteLock guard(thread_mutex_); |
| 134 |
if (thread_.load()) { |
| 135 |
std::thread *thrd = thread_.load(); |
| 136 |
thread_ = nullptr; |
| 137 |
if (self_delete) { |
| 138 |
thrd->detach(); |
| 139 |
} |
| 140 |
delete thrd; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
const char *name() const { |
| 151 |
return name_.c_str(); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
template <typename StartFunction, typename... Args> |
| 169 |
static bool launch( |
| 170 |
std::shared_ptr<Thread> pThis, bool autoRemove, StartFunction start_function, Args... args); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
bool is_current_thread() const { |
| 177 |
ReadLock guard(thread_mutex_); |
| 178 |
return thread_.load() ? (thread_.load()->get_id() == std::this_thread::get_id()) : false; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
virtual void request_shutdown() { |
| 187 |
shutdown_requested_ = true; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
virtual bool is_shutdown_requested() const { |
| 197 |
return shutdown_requested_.load(); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
bool is_auto_remove() const { |
| 207 |
return auto_remove_; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
void make_joinable() { |
| 216 |
auto_remove_ = false; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
bool joinable() const { |
| 224 |
if (thread_.load()) { |
| 225 |
CHECK_EQ(auto_remove_, false); |
| 226 |
|
| 227 |
return thread_.load()->joinable(); |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
void join() { |
| 237 |
internal_join(false); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
std::thread::id get_id() const { |
| 245 |
return thread_.load()->get_id(); |
| 246 |
} |
| 247 |
|
| 248 |
private: |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
void internal_join(bool auto_remove_ok) { |
| 254 |
ReadLock guard(thread_mutex_); |
| 255 |
|
| 256 |
|
| 257 |
if (thread_.load() && thread_.load()->get_id() != std::thread::id()) { |
| 258 |
std::thread::id someId; |
| 259 |
if (!auto_remove_ok) { |
| 260 |
CHECK_EQ(auto_remove_, false); |
| 261 |
} |
| 262 |
CHECK_NOTNULL(thread_.load()); |
| 263 |
if (thread_.load()->joinable()) { |
| 264 |
thread_.load()->join(); |
| 265 |
} else { |
| 266 |
LOG(WARNING) << "Thread " << name_ << " ( " << thread_.load()->get_id() |
| 267 |
<< " ) not joinable"; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
template <typename StartFunction, typename... Args> |
| 282 |
static int entry_and_exit_f( |
| 283 |
std::shared_ptr<Thread> pThis, StartFunction start_function, Args... args); |
| 284 |
|
| 285 |
std::string name_; |
| 286 |
|
| 287 |
mutable SharedMutex thread_mutex_; |
| 288 |
|
| 289 |
std::atomic<std::thread *> thread_; |
| 290 |
|
| 291 |
std::shared_ptr<ManualEvent> ready_event_; |
| 292 |
|
| 293 |
std::shared_ptr<ManualEvent> start_event_; |
| 294 |
|
| 295 |
ThreadGroup *owner_; |
| 296 |
|
| 297 |
std::atomic<bool> shutdown_requested_; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
std::atomic<bool> auto_remove_; |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
inline ThreadGroup() : evEmpty_(std::make_shared<ManualEvent>()) { |
| 309 |
evEmpty_->signal(); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
virtual ~ThreadGroup() { |
| 317 |
request_shutdown_all(); |
| 318 |
join_all(); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
inline bool is_this_thread_in() const { |
| 328 |
std::thread::id id = std::this_thread::get_id(); |
| 329 |
ReadLock guard(m_); |
| 330 |
for (auto it = threads_.begin(), end = threads_.end(); it != end; ++it) { |
| 331 |
std::shared_ptr<Thread> thrd = *it; |
| 332 |
if (thrd->get_id() == id) { |
| 333 |
return true; |
| 334 |
} |
| 335 |
} |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
inline bool is_thread_in(std::shared_ptr<Thread> thrd) const { |
| 345 |
if (thrd) { |
| 346 |
std::thread::id id = thrd->get_id(); |
| 347 |
ReadLock guard(m_); |
| 348 |
for (auto it = threads_.begin(), end = threads_.end(); it != end; ++it) { |
| 349 |
std::shared_ptr<Thread> thrd = *it; |
| 350 |
if (thrd->get_id() == id) { |
| 351 |
return true; |
| 352 |
} |
| 353 |
} |
| 354 |
return false; |
| 355 |
} else { |
| 356 |
return false; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
inline bool add_thread(std::shared_ptr<Thread> thrd) { |
| 366 |
if (thrd) { |
| 367 |
WriteLock guard(m_); |
| 368 |
auto iter = name_to_thread_.find(thrd->name()); |
| 369 |
if (iter == name_to_thread_.end()) { |
| 370 |
name_to_thread_.emplace(std::make_pair(thrd->name(), thrd)); |
| 371 |
CHECK_EQ(threads_.insert(thrd).second, true); |
| 372 |
evEmpty_->reset(); |
| 373 |
return true; |
| 374 |
} |
| 375 |
} |
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
inline bool remove_thread(std::shared_ptr<Thread> thrd) { |
| 385 |
if (thrd) { |
| 386 |
WriteLock guard(m_); |
| 387 |
auto iter = threads_.find(thrd); |
| 388 |
if (iter != threads_.end()) { |
| 389 |
name_to_thread_.erase(thrd->name()); |
| 390 |
threads_.erase(iter); |
| 391 |
if (threads_.empty()) { |
| 392 |
evEmpty_->signal(); |
| 393 |
} |
| 394 |
return true; |
| 395 |
} |
| 396 |
} |
| 397 |
return false; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
inline void join_all() { |
| 406 |
CHECK_EQ(!is_this_thread_in(), true); |
| 407 |
do { |
| 408 |
std::unique_lock<std::mutex> lk(join_all_mtx_); |
| 409 |
std::unordered_set<std::shared_ptr<Thread>> working_set; |
| 410 |
{ |
| 411 |
ReadLock guard(m_); |
| 412 |
for (auto iter = threads_.begin(), e_iter = threads_.end(); iter != e_iter; ++iter) { |
| 413 |
if (!(*iter)->is_auto_remove()) { |
| 414 |
working_set.emplace(*iter); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
while (!working_set.empty()) { |
| 421 |
std::shared_ptr<Thread> thrd; |
| 422 |
thrd = *working_set.begin(); |
| 423 |
if (thrd->joinable()) { |
| 424 |
thrd->join(); |
| 425 |
} |
| 426 |
remove_thread(thrd); |
| 427 |
working_set.erase(working_set.begin()); |
| 428 |
thrd.reset(); |
| 429 |
} |
| 430 |
|
| 431 |
} while (0); |
| 432 |
evEmpty_->wait(); |
| 433 |
CHECK_EQ(threads_.size(), 0); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
inline void request_shutdown_all(const bool make_all_joinable = true) { |
| 441 |
std::unique_lock<std::mutex> lk(join_all_mtx_); |
| 442 |
ReadLock guard(m_); |
| 443 |
for (auto &thread : threads_) { |
| 444 |
if (make_all_joinable) { |
| 445 |
thread->make_joinable(); |
| 446 |
} |
| 447 |
thread->request_shutdown(); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
inline size_t size() const { |
| 456 |
ReadLock guard(m_); |
| 457 |
return threads_.size(); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
inline bool empty() const { |
| 465 |
ReadLock guard(m_); |
| 466 |
return threads_.size() == 0; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
template <typename StartFunction, typename ThreadType = Thread, typename... Args> |
| 485 |
inline bool create( |
| 486 |
const std::string &threadName, bool auto_remove, StartFunction start_function, Args... args) { |
| 487 |
typename ThreadType::SharedPtr newThread(new ThreadType(threadName, this)); |
| 488 |
return Thread::launch(newThread, auto_remove, start_function, args...); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
inline std::shared_ptr<Thread> thread_by_name(const std::string &name) { |
| 497 |
ReadLock guard(m_); |
| 498 |
auto iter = name_to_thread_.find(name); |
| 499 |
if (iter != name_to_thread_.end()) { |
| 500 |
return iter->second; |
| 501 |
} |
| 502 |
return nullptr; |
| 503 |
} |
| 504 |
|
| 505 |
private: |
| 506 |
|
| 507 |
mutable SharedMutex m_; |
| 508 |
|
| 509 |
mutable std::mutex join_all_mtx_; |
| 510 |
|
| 511 |
std::unordered_set<std::shared_ptr<Thread>> threads_; |
| 512 |
|
| 513 |
std::shared_ptr<ManualEvent> evEmpty_; |
| 514 |
|
| 515 |
std::unordered_map<std::string, std::shared_ptr<Thread>> name_to_thread_; |
| 516 |
}; |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
template <typename ObjectType, ObjectType quit_item> |
| 525 |
class BlockingQueueThread : public ThreadGroup::Thread { |
| 526 |
using BQT = BlockingQueueThread<ObjectType, quit_item>; |
| 527 |
|
| 528 |
public: |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
BlockingQueueThread( |
| 536 |
const std::string &name, dmlc::ThreadGroup *owner, std::thread *thrd = nullptr) |
| 537 |
: ThreadGroup::Thread(name, owner, thrd), shutdown_in_progress_(false) {} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
~BlockingQueueThread() override { |
| 543 |
|
| 544 |
ThreadGroup::Thread::request_shutdown(); |
| 545 |
request_shutdown(); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
void request_shutdown() override { |
| 556 |
shutdown_in_progress_ = true; |
| 557 |
while (queue_->size_approx() > 0 && !ThreadGroup::Thread::is_shutdown_requested()) { |
| 558 |
std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 559 |
} |
| 560 |
ThreadGroup::Thread::request_shutdown(); |
| 561 |
queue_->enqueue(quit_item); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
void enqueue(const ObjectType &item) { |
| 569 |
if (!shutdown_in_progress_) { |
| 570 |
queue_->enqueue(item); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
size_t size_approx() const { |
| 579 |
return queue_->size_approx(); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
template <typename SecondaryFunction> |
| 593 |
static bool launch_run(std::shared_ptr<BQT> pThis, SecondaryFunction secondary_function) { |
| 594 |
return ThreadGroup::Thread::launch( |
| 595 |
pThis, true, |
| 596 |
[](std::shared_ptr<BQT> pThis, SecondaryFunction secondary_function) { |
| 597 |
return pThis->run(secondary_function); |
| 598 |
}, |
| 599 |
pThis, secondary_function); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
template <typename OnItemFunction> |
| 609 |
inline int run(OnItemFunction on_item_function) { |
| 610 |
int rc = 0; |
| 611 |
do { |
| 612 |
ObjectType item; |
| 613 |
queue_->wait_dequeue(item); |
| 614 |
if (item == quit_item) { |
| 615 |
break; |
| 616 |
} |
| 617 |
rc = on_item_function(item); |
| 618 |
if (rc) { |
| 619 |
break; |
| 620 |
} |
| 621 |
} while (true); |
| 622 |
return rc; |
| 623 |
} |
| 624 |
|
| 625 |
private: |
| 626 |
|
| 627 |
std::shared_ptr<dmlc::moodycamel::BlockingConcurrentQueue<ObjectType>> queue_ |
| 628 |
= std::make_shared<dmlc::moodycamel::BlockingConcurrentQueue<ObjectType>>(); |
| 629 |
|
| 630 |
std::atomic<bool> shutdown_in_progress_; |
| 631 |
}; |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
template <typename Duration> |
| 638 |
class TimerThread : public ThreadGroup::Thread { |
| 639 |
using ThreadGroup::Thread::is_shutdown_requested; |
| 640 |
|
| 641 |
public: |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
TimerThread(const std::string &name, ThreadGroup *owner) : Thread(name, owner) {} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
~TimerThread() override { |
| 653 |
request_shutdown(); |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
template <typename SecondaryFunction> |
| 667 |
static bool launch_run( |
| 668 |
std::shared_ptr<TimerThread<Duration>> pThis, SecondaryFunction secondary_function) { |
| 669 |
return ThreadGroup::Thread::launch( |
| 670 |
pThis, true, |
| 671 |
[](std::shared_ptr<TimerThread<Duration>> pThis, SecondaryFunction secondary_function) { |
| 672 |
return pThis->run(secondary_function); |
| 673 |
}, |
| 674 |
pThis, secondary_function); |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
template <typename Function> |
| 687 |
static void start( |
| 688 |
std::shared_ptr<TimerThread> timer_thread, Duration duration, Function function) { |
| 689 |
timer_thread->duration_ = duration; |
| 690 |
launch_run(timer_thread, function); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
template <typename OnTimerFunction> |
| 700 |
inline int run(OnTimerFunction on_timer_function) { |
| 701 |
int rc = 0; |
| 702 |
while (!is_shutdown_requested()) { |
| 703 |
std::this_thread::sleep_for(duration_); |
| 704 |
if (!is_shutdown_requested()) { |
| 705 |
rc = on_timer_function(); |
| 706 |
} |
| 707 |
} |
| 708 |
return rc; |
| 709 |
} |
| 710 |
|
| 711 |
private: |
| 712 |
Duration duration_; |
| 713 |
}; |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
template <typename StartFunction, typename... Args> |
| 719 |
inline int ThreadGroup::Thread::entry_and_exit_f( |
| 720 |
std::shared_ptr<Thread> pThis, StartFunction start_function, Args... args) { |
| 721 |
int rc; |
| 722 |
if (pThis) { |
| 723 |
|
| 724 |
pThis->ready_event_->signal(); |
| 725 |
|
| 726 |
pThis->start_event_->wait(); |
| 727 |
|
| 728 |
pThis->start_event_->reset(); |
| 729 |
|
| 730 |
if (!pThis->is_shutdown_requested()) { |
| 731 |
rc = start_function(args...); |
| 732 |
} else { |
| 733 |
rc = -1; |
| 734 |
} |
| 735 |
|
| 736 |
if (pThis->is_auto_remove()) { |
| 737 |
pThis->owner_->remove_thread(pThis); |
| 738 |
} |
| 739 |
|
| 740 |
pThis.reset(); |
| 741 |
} else { |
| 742 |
LOG(ERROR) << "Null pThis thread pointer"; |
| 743 |
rc = EINVAL; |
| 744 |
} |
| 745 |
return rc; |
| 746 |
} |
| 747 |
|
| 748 |
template <typename StartFunction, typename... Args> |
| 749 |
inline bool ThreadGroup::Thread::launch( |
| 750 |
std::shared_ptr<Thread> pThis, bool autoRemove, StartFunction start_function, Args... args) { |
| 751 |
WriteLock guard(pThis->thread_mutex_); |
| 752 |
CHECK_EQ(!pThis->thread_.load(), true); |
| 753 |
CHECK_NOTNULL(pThis->owner_); |
| 754 |
|
| 755 |
pThis->auto_remove_ = autoRemove; |
| 756 |
|
| 757 |
pThis->thread_ = new std::thread( |
| 758 |
Thread::template entry_and_exit_f<StartFunction, Args...>, pThis, start_function, args...); |
| 759 |
|
| 760 |
|
| 761 |
if (!pThis->owner_->add_thread(pThis)) { |
| 762 |
pThis->request_shutdown(); |
| 763 |
LOG(ERROR) << "Duplicate thread name within the same thread group is not allowed"; |
| 764 |
} |
| 765 |
|
| 766 |
pThis->ready_event_->wait(); |
| 767 |
|
| 768 |
pThis->start_event_->signal(); |
| 769 |
|
| 770 |
return pThis->thread_.load() != nullptr; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
template <typename Duration, typename TimerFunction> |
| 784 |
inline bool CreateTimer(const std::string &timer_name, const Duration &duration, ThreadGroup *owner, |
| 785 |
TimerFunction timer_function) { |
| 786 |
std::shared_ptr<dmlc::TimerThread<Duration>> timer_thread |
| 787 |
= std::make_shared<dmlc::TimerThread<Duration>>(timer_name, owner); |
| 788 |
dmlc::TimerThread<Duration>::start(timer_thread, duration, timer_function); |
| 789 |
return timer_thread != nullptr; |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
#endif |