当前位置: 首页>>代码示例>>C++>>正文


C++ TaskScheduler::assignImmediateFunc方法代码示例

本文整理汇总了C++中TaskScheduler::assignImmediateFunc方法的典型用法代码示例。如果您正苦于以下问题:C++ TaskScheduler::assignImmediateFunc方法的具体用法?C++ TaskScheduler::assignImmediateFunc怎么用?C++ TaskScheduler::assignImmediateFunc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TaskScheduler的用法示例。


在下文中一共展示了TaskScheduler::assignImmediateFunc方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: workerThread

OSTHREAD_FUNC TCPDatagramCommunication::workerThread(void *parm){
  TCPDatagramCommunication *tdc = (TCPDatagramCommunication *) parm;
  TaskScheduler *ts = tgetTaskScheduler();
  int epfd = epoll_create1(0); assert(epfd != -1);
  eventfd_t eventdummy;
  int myworkerno = gContext.indexWithinClass(TCLASS_WORKER, tgetThreadNo());
  
  tsetSharedSpace(THREADCONTEXT_SPACE_TCPDATAGRAM, tdc);
  tsetSharedSpace(THREADCONTEXT_SPACE_TCPDATAGRAM_WORKER,
                  (void*)(long long) epfd);
  
  ts->assignImmediateFunc(IMMEDIATEFUNC_ADDIPPORTFD, immediateFuncAddIPPortFd);
  ts->assignImmediateFunc(IMMEDIATEFUNC_SEND, immediateFuncSend);

  tdc->startupWorkerThread(); // invokes startup code

  int i, n, res;
  struct epoll_event *epevents=0;
  TCPStreamState *tss=0;
  TCPStreamState sleepeventtss;
  epevents = new epoll_event[MAX_EPOLL_EVENTS];
  int nread=0;

  int sleepeventfd = ts->getSleepEventFd();
  struct epoll_event ev;
  sleepeventtss.fd = sleepeventfd;
  ev.events = EPOLLIN;
  ev.data.ptr = (void*) &sleepeventtss;
  res = epoll_ctl(epfd, EPOLL_CTL_ADD, sleepeventfd, &ev); assert(res==0);
  
  int something;
  int timeout=0;

  tdc->workerInitSync.signal(); // indicate that we have initialized
  
  while (!tdc->ForceEndThreads){
    something = ts->runOnce(); // run event schedule loop once
                  // this will handle immediate functions to add new
                  // things to the epoll set:
                  //     after adding, must receive anything that already exists

    // go through pendingsends and send anything for which we did not get
    // EAGAIN before
    if (!tdc->PendingSendsBeforeEpoll[myworkerno].empty()){
      SetNode<TCPStreamStatePtr> *it;
      TCPStreamState *tssptr;
      for (it = tdc->PendingSendsBeforeEpoll[myworkerno].getFirst();
           it != tdc->PendingSendsBeforeEpoll[myworkerno].getLast();
           it = tdc->PendingSendsBeforeEpoll[myworkerno].getNext(it)){
        tssptr = it->key.tssptr;
        assert(!tssptr->sendeagain);
        tdc->sendTss(tssptr);
      }
      tdc->PendingSendsBeforeEpoll[myworkerno].clear();
    }
    
    if (!something){ // start sleep cycle
      ts->setAsleep(1);
      timeout = ts->findSleepTimeout();
      //printf("Going to sleep for %d\n", timeout);
    } else timeout = 0;
    
    n = epoll_wait(epfd, epevents, MAX_EPOLL_EVENTS, timeout);
    if (!something) ts->setAsleep(0);
      
    for (i=0; i < n; ++i){
      tss = (TCPStreamState*) epevents[i].data.ptr;
      if (tss->fd == sleepeventfd){ // used just to wake us up
        //printf("Woken from sleep\n");
        eventfd_read(sleepeventfd, &eventdummy);
        continue;
      }
          
      if (epevents[i].events & EPOLLIN){ // read available
        //if (ts->checkSendQueuesAlmostFull()){
          // **!** if internal send queues are almost full, do not receive
          // TCP packets
          // do something here
        //}
        
        while (1){
          nread = read(tss->fd, tss->rstate.Ptr,
                       tss->rstate.Buflen - tss->rstate.Filled);
          if (nread < 0){
            if (errno == EAGAIN || errno == EWOULDBLOCK) break;
          } else if (nread == 0) break;
          else {
            // update the state of this stream (and if entire message received,
            // invoke handler)
            tdc->updateState(tss->handlerid, tss->rstate, tss->ipport, nread);
          }
        }
      }
      if (epevents[i].events & EPOLLOUT){ // write available
        tdc->sendTss(tss);
      }
      if (epevents[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)){
        // problem with fd
        close(tss->fd);
      }
//.........这里部分代码省略.........
开发者ID:mkaguilera,项目名称:yesquel,代码行数:101,代码来源:tcpdatagram.cpp


注:本文中的TaskScheduler::assignImmediateFunc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。