// enters the event_base loop -- will only exit when forced to bool EventBase::loop() { VLOG(5) << "EventBase(): Starting loop."; int res = 0; bool ranLoopCallbacks; int nonBlocking; loopThread_.store(pthread_self(), std::memory_order_release); #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12) if (!name_.empty()) { pthread_setname_np(pthread_self(), name_.c_str()); } #endif int64_t prev = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now().time_since_epoch()).count(); int64_t idleStart = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now().time_since_epoch()).count(); // TODO: Read stop_ atomically with an acquire barrier. while (!stop_) { ++nextLoopCnt_; // nobody can add loop callbacks from within this thread if // we don't have to handle anything to start with... nonBlocking = (loopCallbacks_.empty() ? 0 : EVLOOP_NONBLOCK); res = event_base_loop(evb_, EVLOOP_ONCE | nonBlocking); ranLoopCallbacks = runLoopCallbacks(); int64_t busy = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now().time_since_epoch()).count() - startWork_; int64_t idle = startWork_ - idleStart; avgLoopTime_.addSample(idle, busy); maxLatencyLoopTime_.addSample(idle, busy); if (observer_) { if (observerSampleCount_++ == observer_->getSampleRate()) { observerSampleCount_ = 0; observer_->loopSample(busy, idle); } } VLOG(11) << "EventBase " << this << " did not timeout " " loop time guess: " << busy + idle << " idle time: " << idle << " busy time: " << busy << " avgLoopTime: " << avgLoopTime_.get() << " maxLatencyLoopTime: " << maxLatencyLoopTime_.get() << " maxLatency_: " << maxLatency_ << " nothingHandledYet(): "<< nothingHandledYet(); // see if our average loop time has exceeded our limit if ((maxLatency_ > 0) && (maxLatencyLoopTime_.get() > double(maxLatency_))) { maxLatencyCob_(); // back off temporarily -- don't keep spamming maxLatencyCob_ // if we're only a bit over the limit maxLatencyLoopTime_.dampen(0.9); } // Our loop run did real work; reset the idle timer idleStart = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now().time_since_epoch()).count(); // If the event loop indicate that there were no more events, and // we also didn't have any loop callbacks to run, there is nothing left to // do. if (res != 0 && !ranLoopCallbacks) { // Since Notification Queue is marked 'internal' some events may not have // run. Run them manually if so, and continue looping. // if (getNotificationQueueSize() > 0) { fnRunner_->handlerReady(0); } else { break; } } VLOG(5) << "EventBase " << this << " loop time: " << getTimeDelta(&prev); } // Reset stop_ so loop() can be called again stop_ = false; if (res < 0) { LOG(ERROR) << "EventBase: -- error in event loop, res = " << res; return false; } else if (res == 1) { VLOG(5) << "EventBase: ran out of events (exiting loop)!"; } else if (res > 1) { LOG(ERROR) << "EventBase: unknown event loop result = " << res; return false; } loopThread_.store(0, std::memory_order_release); VLOG(5) << "EventBase(): Done with loop."; return true; }
VC3 AlignUnits::getMovedPosition(const VC3 &position, IStorm3D_Camera &camera, bool *updated) const { VC3 result = position; float factor = .005f; bool useGrid = false; if((GetKeyState(VK_SHIFT) & 0x80)) factor = .2f; if((GetKeyState(VK_CONTROL) & 0x80) == 0) useGrid = true; factor *= getTimeDelta(); VC3 y = camera.GetTarget() - camera.GetPosition(); y.y = 0; y.Normalize(); VC3 x = VC3(0.f, 1.f, 0.f).GetCrossWith(y); #ifdef PROJECT_AOV x = VC3(1, 0, 0); y = VC3(0, 0, 1); #endif y *= 40.f; x *= 40.f; // new: if ctrl was not down, use grid to move objects static int lastGridMoveTime = 0; long curTime = timeGetTime(); if (useGrid) { if (fabsf(x.x) > fabsf(x.z)) { x = VC3(x.x,0,0); y = VC3(0,0,y.z); } else { x = VC3(0,0,x.z); y = VC3(y.x,0,0); } if (x.GetSquareLength() > 0.0001f) { x.Normalize(); x *= data->gridX; } else { x = VC3(0,0,0); } if (y.GetSquareLength() > 0.0001f) { y.Normalize(); y *= data->gridY; } else { y = VC3(0,0,0); } if((GetKeyState(VK_SHIFT) & 0x80)) { //#ifdef PROJECT_AOV // x *= 10.0f; // y *= 10.0f; //#else x *= 10.0f; y *= 10.0f; //#endif } if (curTime < lastGridMoveTime + 100) { factor = 0.0f; } else { factor = 1.0f; } } // end of new grid thingy if(updated) *updated = false; if (factor != 0.0f) { if((GetKeyState(VK_UP) & 0x80)) { result += y * factor; if(updated) *updated = true; lastGridMoveTime = curTime; } if((GetKeyState(VK_DOWN) & 0x80)) { result -= y * factor; if(updated) *updated = true; lastGridMoveTime = curTime; } if((GetKeyState(VK_RIGHT) & 0x80)) { result += x * factor; if(updated) *updated = true; lastGridMoveTime = curTime; } if((GetKeyState(VK_LEFT) & 0x80)) { result -= x * factor; if(updated) *updated = true; lastGridMoveTime = curTime; } } return result; }
bool EventBase::loopBody(int flags) { VLOG(5) << "EventBase(): Starting loop."; DCHECK(!invokingLoop_) << "Your code just tried to loop over an event base from inside another " << "event base loop. Since libevent is not reentrant, this leads to " << "undefined behavior in opt builds. Please fix immediately. For the " << "common case of an inner function that needs to do some synchronous " << "computation on an event-base, replace getEventBase() by a new, " << "stack-allocated EvenBase."; invokingLoop_ = true; SCOPE_EXIT { invokingLoop_ = false; }; int res = 0; bool ranLoopCallbacks; bool blocking = !(flags & EVLOOP_NONBLOCK); bool once = (flags & EVLOOP_ONCE); // time-measurement variables. std::chrono::steady_clock::time_point prev; int64_t idleStart = 0; int64_t busy; int64_t idle; loopThread_.store(pthread_self(), std::memory_order_release); if (!name_.empty()) { setThreadName(name_); } if (enableTimeMeasurement_) { prev = std::chrono::steady_clock::now(); idleStart = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now().time_since_epoch()).count(); } while (!stop_.load(std::memory_order_acquire)) { applyLoopKeepAlive(); ++nextLoopCnt_; // Run the before loop callbacks LoopCallbackList callbacks; callbacks.swap(runBeforeLoopCallbacks_); while(!callbacks.empty()) { auto* item = &callbacks.front(); callbacks.pop_front(); item->runLoopCallback(); } // nobody can add loop callbacks from within this thread if // we don't have to handle anything to start with... if (blocking && loopCallbacks_.empty()) { res = event_base_loop(evb_, EVLOOP_ONCE); } else { res = event_base_loop(evb_, EVLOOP_ONCE | EVLOOP_NONBLOCK); } ranLoopCallbacks = runLoopCallbacks(); if (enableTimeMeasurement_) { busy = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now().time_since_epoch()).count() - startWork_; idle = startWork_ - idleStart; avgLoopTime_.addSample(idle, busy); maxLatencyLoopTime_.addSample(idle, busy); if (observer_) { if (observerSampleCount_++ == observer_->getSampleRate()) { observerSampleCount_ = 0; observer_->loopSample(busy, idle); } } VLOG(11) << "EventBase " << this << " did not timeout " " loop time guess: " << busy + idle << " idle time: " << idle << " busy time: " << busy << " avgLoopTime: " << avgLoopTime_.get() << " maxLatencyLoopTime: " << maxLatencyLoopTime_.get() << " maxLatency_: " << maxLatency_ << " notificationQueueSize: " << getNotificationQueueSize() << " nothingHandledYet(): "<< nothingHandledYet(); // see if our average loop time has exceeded our limit if ((maxLatency_ > 0) && (maxLatencyLoopTime_.get() > double(maxLatency_))) { maxLatencyCob_(); // back off temporarily -- don't keep spamming maxLatencyCob_ // if we're only a bit over the limit maxLatencyLoopTime_.dampen(0.9); } // Our loop run did real work; reset the idle timer idleStart = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now().time_since_epoch()).count(); } else { VLOG(11) << "EventBase " << this << " did not timeout"; } // If the event loop indicate that there were no more events, and // we also didn't have any loop callbacks to run, there is nothing left to // do. if (res != 0 && !ranLoopCallbacks) { // Since Notification Queue is marked 'internal' some events may not have // run. Run them manually if so, and continue looping. // if (getNotificationQueueSize() > 0) { fnRunner_->handlerReady(0); } else { break; } } if (enableTimeMeasurement_) { VLOG(5) << "EventBase " << this << " loop time: " << getTimeDelta(&prev).count(); } if (once) { break; } } // Reset stop_ so loop() can be called again stop_ = false; if (res < 0) { LOG(ERROR) << "EventBase: -- error in event loop, res = " << res; return false; } else if (res == 1) { VLOG(5) << "EventBase: ran out of events (exiting loop)!"; } else if (res > 1) { LOG(ERROR) << "EventBase: unknown event loop result = " << res; return false; } loopThread_.store({}, std::memory_order_release); VLOG(5) << "EventBase(): Done with loop."; return true; }
void main(void) { // WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer initClock(); initLFXT(); SCB_CPACR = 0xf << 20; //enable the fpu DIO->rPADIR.b.bP2DIR = 0x07; //set onboard rgb led pins to output DIO->rPAOUT.b.bP2OUT = 0x00; //set onboard rgb led pins to off DIO->rPADIR.b.bP1DIR &= ~0x02; //button DIO->rPAREN.b.bP1REN |= 0x02; //button DIO->rPAOUT.b.bP1OUT |= 0x02; //button DIO->rPAOUT.b.bP2OUT |= 0x01; setUpUART(); // testInitDMA(); // PMAP->rKEYID = 0x2D52; // PMAP->rP2MAP23 = PM_UCA1SIMO; // PMAP->rP3MAP23 |= PM_UCA2SIMO; // PMAP->rKEYID = 0x00; initDisplay(); setDisplayCurrentLimit(1.6f); ADC_init(); ADC_setNumberOfChannels(4); ADC_setChannelSource(0, 15); ADC_setChannelSource(1, 14); ADC_setChannelSource(2, 13); ADC_setChannelSource(3, 8); initRandom(); // uint16_t testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); // testRandom = getRandomMessageNumber(); uint8_t lastButtonState = 0; uint16_t increment = 0; calcPalette(); initTimer(); getTimeDelta(); float time = 0; float scrollRate = 25; float scrollStart = 0.0; int messageNumber = getRandomMessageNumber(); int scrollTick = 0; int scrollDiv = 1; int scrollPosition = GFX_WIDTH; int staticHatIndex = 0; float textBrightness = 0.3; messageColours[staticHatIndex].h = 0.0; messageColours[staticHatIndex].s = 1.0; messageColours[staticHatIndex++].v = textBrightness; messageColours[staticHatIndex].h = 0.2; messageColours[staticHatIndex].s = 1.0; messageColours[staticHatIndex++].v = textBrightness; messageColours[staticHatIndex].h = 0.4; messageColours[staticHatIndex].s = 1.0; messageColours[staticHatIndex++].v = textBrightness; messageColours[staticHatIndex].h = 0.6; messageColours[staticHatIndex].s = 1.0; messageColours[staticHatIndex++].v = textBrightness; messageColours[staticHatIndex].h = 0.8; messageColours[staticHatIndex].s = 1.0; messageColours[staticHatIndex++].v = textBrightness; messageColours[staticHatIndex].h = 0.5; messageColours[staticHatIndex].s = 1.0; messageColours[staticHatIndex++].v = 0.0; getRandomColour(); int showMessage = 0; int currentMessageColour = rand() % MESSAGE_COLOR_COUNT; while (1) { time += getTimeDelta(); clearDisplay(); // drawFlatBackground(0.5, 1.0, 0.1, 2.0); drawPlasma(time, 1.0, 0.05); // int16_t fontX = 0; if (showMessage) { setColourHSVf(randomColour.h, randomColour.s, randomColour.v); scrollTick++; if (scrollTick % scrollDiv == 0) { scrollPosition--; scrollTick = 0; } int textXPos = scrollPosition; textXPos = drawString(messages[messageNumber], textXPos, 0); if (textXPos < 0) { // scrollStart = time; scrollPosition = GFX_WIDTH; scrollTick = 0; messageNumber = (messageNumber + 1) % MESSGAE_COUNT; // messageNumber = getRandomMessageNumber(); //skip empty ones while (messages[messageNumber][0] == '\0') { // messageNumber = getRandomMessageNumber(); messageNumber = (messageNumber + 1) % MESSGAE_COUNT; } currentMessageColour = rand() % MESSAGE_COLOR_COUNT; getRandomColour(); } } normaliseAndSwapBuffer(); // EUSCI_A1->rTXBUF.a.bTXBUF = 'f'; if (time > 600) { time = 0; // scrollStart = 0; } // increment = (increment + 1) % 2; // // if (increment == 0) { // ; // } // colourIndex = (colourIndex + 1) & 0x3f; // DIO->rPAOUT.b.bP2OUT ^= 0x01; uint8_t button = readButton(); if( button && !lastButtonState){ //transition on DIO->rPAOUT.b.bP2OUT ^= 0x01; DIO->rPAOUT.b.bP2OUT ^= 0x02; showMessage ^= 0x01; }else if( !button && lastButtonState){ //transition off; } lastButtonState = button; } }