/** * Event is removed from internal queue * TODO: should not be blocking * Virtual */ RawEvent EventMonitorMac::nextEvent() { if ( numEvents() == 0 ) waitForEvents(); RawEvent re = events.front(); events.pop_front(); return re; }
void Window::update() { swapBuffer(); waitForEvents(); glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); }
bool SchedulerBase::Run() { uint32_t iter=0; TimeSpec timeout; TimeSpec immediate; bool gotEvents; if (!LogVerify(IsMainThread())) return false; m_isStarted = true; // Start with a quick event check. timeout = immediate; while (true) { iter++; if (m_wantsShutdown) break; // // Get event, or timeout. // gLog.Optional(Log::TimerDetail, "checking events (%u)", iter); gotEvents = waitForEvents(timeout); // By default the next event check is immediately. timeout = immediate; // // High priority timers, if any, get handled now // while (!m_wantsShutdown && expireTimer(Timer::Priority::Hi)) { //nothing } if (m_wantsShutdown) break; // // Handle any events. // if (gotEvents) { int socketId; gLog.Optional(Log::TimerDetail, "Handling events (%u)", iter); while (-1 != (socketId = getNextSocketEvent())) { // we have a socket event .. is it a socket or a signal? SocketItemHashMap::iterator foundSocket; SignalItemHashMap::iterator foundSignal; if (m_sockets.end() != (foundSocket = m_sockets.find(socketId))) { if (LogVerify(foundSocket->second.callback != NULL)) foundSocket->second.callback(socketId, foundSocket->second.userdata); } else if (m_signals.end() != (foundSignal = m_signals.find(socketId))) { if (LogVerify(foundSignal->second.callback != NULL)) { // 'Drain' the pipe. char drain[128]; int result; size_t reads = 0; while ( 0 < (result = ::read(socketId, drain, sizeof(drain)))) reads++; if (reads == 0 && result < 0) gLog.LogError("Failed to read from pipe %d: %s", socketId, strerror(errno)); else if (result == 0) gLog.LogError("Signaling pipe write end for %d closed", socketId); foundSignal->second.callback(foundSignal->second.fdWrite, foundSignal->second.userdata); } } else { gLog.Optional(Log::TimerDetail, "Socket (%d) signaled with no handler (%u).", socketId, iter); } if (m_wantsShutdown) break; } if (m_wantsShutdown) break; } // // Handle a low priority timer if there are no events. // TODO: starvation is a potential problem for low priority timers. // if (!gotEvents && !expireTimer(Timer::Priority::Low)) { // No events and no more timers, so we are ready to sleep again. timeout = getNextTimerTimeout(); } if (m_wantsShutdown) break; } // while true return true; }
int StateMachine::Run() { //initialize database ptDb->init(); connectionCount = false; int statusCode = 0; // Run the program until SIG_KILL is not received do { //initialize the state machine statusCode = initializeStateMachine(); //set next state setNextState(statusCode); if(statusCode == SUCCESS) { //turn the timer off --- as here objects will reconstruct themselves StateMachine::isTimerEvent = false; // Run the program until connection is alive. do { // poll for bro events. PollData tempQueue = waitForEvents(); if(signalHandler->gotExitSignal()) { broker::message temp; currentEvent = SIG_KILL_EVENT; //pass kill signal to clean up the resources extractAndProcessEvents(currentEvent,temp); } else if(!ptBCM->isConnectionAlive()) { broker::message temp; extractAndProcessEvents(CONNECTION_BROKEN_EVENT,temp); } // bro events processing else if(!tempQueue.empty()) { for(auto& msg : tempQueue) { if(signalHandler->gotExitSignal()) { currentEvent = SIG_KILL_EVENT; extractAndProcessEvents(currentEvent,msg); break; } else { auto ev = broker::to_string(msg[0]); statusCode = extractAndProcessEvents( stringToEvent(ev),msg); if(statusCode == SIG_KILL_EVENT) { break; } } } } // if timer is up. else if(isTimerEvent) { broker::message temp; extractAndProcessEvents(TIMER_EVENT,temp); } }while(ptBCM->isConnectionAlive() && !signalHandler->gotExitSignal()); //if disconnected then break the connection. LOG(WARNING) <<"Connection Broken"; if(signalHandler->gotExitSignal()) { doActionsForKillSignalEvent(); } } }while(!signalHandler->gotExitSignal()); return SUCCESS; }