MCPcopy Create free account
hub / github.com/Tencent/mars / WaitMessage

Function WaitMessage

mars/comm/messagequeue/message_queue.cc:478–533  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

476}
477
478bool WaitMessage(const MessagePost_t& _message, long _timeoutInMs) {
479 bool is_in_mq = Handler2Queue(Post2Handler(_message)) == CurrentThreadMessageQueue();
480
481 ScopedLock lock(sg_messagequeue_map_mutex);
482 const MessageQueue_t& id = Handler2Queue(Post2Handler(_message));
483 std::map<MessageQueue_t, MessageQueueContent>::iterator pos = sg_messagequeue_map.find(id);
484 if (sg_messagequeue_map.end() == pos) return false;
485 MessageQueueContent& content = pos->second;
486
487 auto find_it = std::find_if(content.lst_message.begin(), content.lst_message.end(),
488 [&_message](const MessageWrapper * const &_v) {
489 return _message == _v->postid;
490 });
491
492 if (find_it == content.lst_message.end()) {
493 auto find_it = std::find_if(content.lst_runloop_info.begin(), content.lst_runloop_info.end(),
494 [&_message](const RunLoopInfo& _v){ return _message == _v.runing_message_id; });
495
496 if (find_it != content.lst_runloop_info.end()) {
497 if (is_in_mq) return false;
498
499 boost::shared_ptr<Condition> runing_cond = find_it->runing_cond;
500 if(_timeoutInMs < 0) {
501 runing_cond->wait(lock);
502 } else {
503 int ret = runing_cond->wait(lock, _timeoutInMs);
504 return ret==0;
505 }
506 }
507 } else {
508
509 if (is_in_mq) {
510 lock.unlock();
511 RunLoop( [&_message](){
512 MessageQueueContent& content = sg_messagequeue_map[Handler2Queue(Post2Handler(_message))];
513 return content.lst_message.end() == std::find_if(content.lst_message.begin(), content.lst_message.end(),
514 [&_message](const MessageWrapper * const &_v) {
515 return _message == _v->postid;
516 });
517 }).Run();
518
519 } else {
520 if (!((*find_it)->wait_end_cond))(*find_it)->wait_end_cond = boost::make_shared<Condition>();
521
522 boost::shared_ptr<Condition> wait_end_cond = (*find_it)->wait_end_cond;
523 if(_timeoutInMs < 0) {
524 wait_end_cond->wait(lock);
525 } else {
526 int ret = wait_end_cond->wait(lock, _timeoutInMs);
527 return ret==0;
528 }
529 }
530 }
531
532 return true;
533}
534
535bool FoundMessage(const MessagePost_t& _message) {

Callers 4

__ReleaseMethod · 0.85
TESTFunction · 0.85
message_queue.hFile · 0.85
WaitInvokeFunction · 0.85

Calls 8

RunLoopClass · 0.85
findMethod · 0.45
endMethod · 0.45
beginMethod · 0.45
waitMethod · 0.45
unlockMethod · 0.45
RunMethod · 0.45

Tested by 1

TESTFunction · 0.68