TimerManager *TimerManager::getInstance() { TRACE_BEGIN(LOG_LVL_INFO); if (mSingleton == NULL) mSingleton = jh_new TimerManager; return mSingleton; }
void Timer::addTimerNode( const TimerNode& newTimer ) { TRACE_BEGIN( LOG_LVL_NOISE ); int32_t diff; bool inserted = false; // Add new timer node to the list (sorted) for (JetHead::list<TimerNode>::iterator i = mList.begin(); i != mList.end(); ++i) { TimerNode timer = *i; diff = newTimer.mTick - timer.mTick; if ( diff < 0 ) { i.insertBefore(newTimer); inserted = true; break; } } if ( not inserted ) { mList.push_back( newTimer ); } }
void SelectorTest::processFileEvents( int fd, short events, jh_ptr_int_t private_data ) { TRACE_BEGIN( LOG_LVL_INFO ); uint8_t buffer[ 10 ]; LOG_NOTICE( "fd %d, events %x private data %d", fd, events, private_data ); if ( events & POLLNVAL ) { TestFailed( "Recieved POLLNVAL" ); } if ( fd == mPipe[ 0 ] && private_data == 10 && ( events & POLLIN ) ) { mTestState++; } else { TestFailed( "First Listener Bad Params" ); } if ( events & POLLIN ) { int res = read( fd, buffer, 10 ); print_buffer( "READ", buffer, res ); } }
void Timer::removeAgentsByReceiver( void* receiver, IEventDispatcher* dispatcher ) { TRACE_BEGIN( LOG_LVL_NOISE ); TimerNode timer; DebugAutoLock( mMutex ); JetHead::list<TimerNode>::iterator i = mList.begin(); while (i != mList.end()) { // Get the TimerNode timer = *i; if ( timer.mEvent->getEventId() == Event::kAgentEventId and timer.mDispatcher == dispatcher ) { EventAgent* agent = static_cast<EventAgent*>( (Event*)timer.mEvent ); if (agent->getDeliveryTarget() == receiver) { i = i.erase(); continue; } } ++i; } }
void Timer::sendPeriodicEvent( Event *event, IEventDispatcher *dispatcher, uint32_t period ) { TRACE_BEGIN( LOG_LVL_NOISE ); DebugAutoLock( mMutex ); // Calculate the timeout time in ticks uint32_t ticks = ( period + mMsPerTick - 1 ) / mMsPerTick; // Initialize new timer node TimerNode timer; timer.mEvent = event; timer.mDispatcher = dispatcher; timer.mPrivateData = 0; timer.mListener = NULL; timer.mTick = mTicks + ticks; timer.mRepeatMS = period; timer.mRemainingMS = 0; LOG( "timer at %d ticks", timer.mTick ); addTimerNode( timer ); }
void Timer::removeTimedEvent( Event::Id eventId, IEventDispatcher *dispatcher ) { TRACE_BEGIN( LOG_LVL_NOISE ); TimerNode timer; DebugAutoLock( mMutex ); JetHead::list<TimerNode>::iterator i = mList.begin(); while (i != mList.end()) { // Get the TimerNode timer = *i; // Check if the timer node has an event for the dispatcher // specified if ( timer.mDispatcher == dispatcher && timer.mEvent != NULL) { // Now test if the timer node event has a matching id or if the // id is Event::kInvalidEventId. Event::kInvalidEventId is a // special id used to remove all events for a dispatcher if ( timer.mEvent->getEventId() == eventId || eventId == Event::kInvalidEventId ) { i = i.erase(); continue; } } ++i; } }
void Selector::removeListener( int fd, SelectorListener *listener ) { TRACE_BEGIN( LOG_LVL_INFO ); AutoLock l( mLock ); ListenerNode n( fd, NULL ); bool update = false; JetHead::list<ListenerNode*>::iterator i = mList.begin(); while (i != mList.end()) { if (*(*i) == n) { update = true; delete *i; i = i.erase(); } else { ++i; } } if ( update ) { updateListeners(); } }
void Timer::addPeriodicTimer( TimerListener *listener, uint32_t period, uint32_t private_data ) { TRACE_BEGIN( LOG_LVL_NOISE ); DebugAutoLock( mMutex ); if (listener == NULL) abort(); // Calculate the timeout time in ticks uint32_t ticks = ( period + mMsPerTick - 1 ) / mMsPerTick; // Initialize new timer node TimerNode timer; timer.mEvent = NULL; timer.mDispatcher = NULL; timer.mPrivateData = private_data; timer.mListener = listener; timer.mTick = mTicks + ticks; timer.mRepeatMS = period; timer.mRemainingMS = 0; LOG( "timer at %d ticks", timer.mTick ); addTimerNode( timer ); }
int32_t Thread::Start() { TRACE_BEGIN( LOG_LVL_INFO ); int32_t rc = 0; pthread_attr_t attrs; struct sched_param prio; prio.sched_priority = sched_get_priority_max( SCHED_RR ); LOG_INFO( "Creating Thread %s", GetName() ); pthread_attr_init( &attrs ); if ( mPrio > 0 && geteuid() == 0 ) { pthread_attr_setschedpolicy( &attrs, SCHED_RR ); pthread_attr_setschedparam( &attrs, &prio ); } rc = pthread_create( &mThread, &attrs, start_thread, (void *)this ); if ( rc != 0 ) { LOG_ERR( "Error from pthread_create() is [%d]", rc ); } LOG( "Thread create %d", rc ); return rc; }
ErrorCode ComponentManager::GetService( CID cid, IID iid, void **object ) { TRACE_BEGIN( LOG_LVL_NOISE ); ErrorCode result = kNoError; ClassInfo match( cid ); ClassInfo *info = NULL; for (JetHead::list<ClassInfo*>::iterator i = mClasses.begin(); i != mClasses.end(); ++i) { if (*(*i) == match) { info = *i; break; } } LOG( "info is %p", info ); if ( info != NULL ) *object = info->mClass->QueryInterface( iid, &result ); else result = kNoClass; return result; }
EventThread::~EventThread() { TRACE_BEGIN( LOG_LVL_INFO ); EventDispatcher::sendEvent( jh_new Event( Event::kShutdownEventId, PRIORITY_HIGH ) ); LOG_NOISE( "Wait for thread to die" ); mThread.Join(); }
void Timer::handleTick() { TRACE_BEGIN( LOG_LVL_NOISE ); DebugAutoLock( mMutex ); mTicks++; TimerNode timer; while (not mList.empty()) { timer = *mList.begin(); int32_t diff = timer.mTick - mTicks; // List is sorted with nearest items first. Once we find a node // that is in the future we are done. if ( diff > 0 ) break; // Remove the timer from the list now mList.pop_front(); // If the timer doesn't have an event then we call the listener if ( timer.mEvent == NULL ) { timer.mListener->onTimeout( timer.mPrivateData ); } // Otherwise send the event else { // Send the event to the specified dispatcher timer.mDispatcher->sendEvent( timer.mEvent ); } // Check to see if this is a periodic timer. If it is then // we need to re-calculate the tick value for the timer. In // addition we are going to calculate the # of ms that are // lost by the tick calculation and accumulate them so that // over time we line up properly whenever possible. if (timer.mRepeatMS != 0) { unsigned newTicks = (timer.mRepeatMS + mMsPerTick - 1 - timer.mRemainingMS) / mMsPerTick; timer.mTick += newTicks; timer.mRemainingMS = (timer.mRepeatMS + timer.mRemainingMS) % mMsPerTick; addTimerNode(timer); } // If this is a non-periodic then release our reference to // the event if we have one else { timer.mEvent = NULL; } } }
void Timer::reset() { TRACE_BEGIN(LOG_LVL_NOISE); DebugAutoLock(mMutex); mTicks = 0; mList.clear(); }
void Selector::wakeThread() { TRACE_BEGIN( LOG_LVL_NOISE ); char buf[] = "EVNT"; int res = write( mPipe[ PIPE_WRITER ], &buf, 4 ); if ( res != 4 ) LOG_ERR( "write to pipe failed %d", res ); }
int EventTest::Func3( uint32_t p1 ) { TRACE_BEGIN( LOG_LVL_INFO ); SmartPtr<Event3> ev = jh_new Event3( p1 ); mSelector->sendEventSync( ev ); if ( ev->mValid == false ) TestFailed( "Ev had been deleted" ); return ev->mP1; }
TimerManager::TimerManager() : mMutex( true ) { TRACE_BEGIN(LOG_LVL_NOTICE); if (mSingleton != NULL) LOG_ERR_FATAL("Illegal creation of TimerManager, use getInstance()"); mDefaultTimer = jh_new Timer(kMsPerTick, false); };
void EventTest::ProcessFunc2( Event2 *ev ) { TRACE_BEGIN( LOG_LVL_INFO ); LOG_NOTICE( "p1 %d p2 %d", ev->mP1, ev->mP2 ); mTestState++; if ( ev->mP1 != 1 || ev->mP2 != 2 ) TestFailed( "P1 and/or P2 not expected values" ); }
void EventTest::ProcessFunc3( Event3 *ev ) { TRACE_BEGIN( LOG_LVL_INFO ); LOG_NOTICE( "p1 %d", ev->mP1 ); mTestState++; if ( ev->mP1 != 3 ) TestFailed( "P1 not expected values" ); ev->mP1 = 1000; }
extern "C" ErrorCode JHCOM_LibraryEntry( IComponentManager *mgr ) { TRACE_BEGIN( LOG_LVL_NOTICE ); if ( gMaster ) LOG_ERR_FATAL( "Called on master" ); gManager = mgr; return kNoError; }
Timer::~Timer() { TRACE_BEGIN(LOG_LVL_INFO); // Force stop of this timer doStop(); // Remove the Timer from the TimerManager TimerManager::getInstance()->removeTimer(this); }
void Timer::stop() { TRACE_BEGIN(LOG_LVL_NOISE); if (mStoppable == true) { doStop(); } else { LOG_WARN("Attempted to stop Timer %p that is not stoppable", this); } }
void Timer::doStop() { TRACE_BEGIN(LOG_LVL_NOISE); if (mClockThread != NULL) { // First stop the clock thread and join it mClockThread->Stop(); mClockThread->Join(); delete mClockThread; mClockThread = NULL; } }
ErrorCode ComponentManager::AddService( CID cid, ISupports *service ) { TRACE_BEGIN( LOG_LVL_INFO ); ClassInfo *info = jh_new ClassInfo( cid, service ); LOG( "Adding cid %s, ISupports %p", cid.toString(), service ); service->AddRef(); mClasses.push_back( info ); return kNoError; }
void TimerManager::removeTimedEvent( Event *ev ) { TRACE_BEGIN(LOG_LVL_NOISE); // Lock mutex while accessing/modifying mTimers vector AutoLock l(mMutex); for (unsigned i = 0; i < mTimers.size(); ++i) { mTimers[i]->removeTimedEvent(ev); } }
Selector::~Selector() { TRACE_BEGIN( LOG_LVL_INFO ); shutdown(); LOG( "Closing pipes" ); // close the pipes fd's. close( mPipe[ PIPE_WRITER ] ); close( mPipe[ PIPE_READER ] ); if ( mThread == *Thread::GetCurrent() ) LOG_ERR_FATAL( "A selector MUST NOT be deleted by its own thread!" ); }
void Thread::Destroy() { TRACE_BEGIN( LOG_LVL_NOTICE ); mInited = false; //delete Thread::GetCurrent(); Mutex::Destroy(); TimerManager::destroyManager(); #ifdef GCHEAP_ENABLED GCHeap::defaultHeap->~GCHeap(); GCHeap::defaultHeap = NULL; #endif }
void HttpRequest::parseMethod( const char *method ) { TRACE_BEGIN( LOG_LVL_INFO ); for (uint32_t i = 0; i < ARRAY_SIZE( gMethodTypeList ); i++) { //LOG_NOISE( "matching %s", gMethodTypeList[ i ] ); if ( strcmp( method, gMethodTypeList[i] ) == 0 ) { mMethod = ( MethodType ) i; LOG( "Found method %s", gMethodTypeList[ mMethod ] ); return; } } LOG( "Failed to match method \"%s\"", method ); mMethod = kMethodUnknown; }
TimerManager::~TimerManager() { TRACE_BEGIN(LOG_LVL_NOTICE); // Release reference to default timer mDefaultTimer = NULL; // At this point all outstanding timers should be // gone. If we still have a timer in our list then // log a warning but do not do anything to the memory for (unsigned i = 0; i < mTimers.size(); ++i) { LOG_WARN("Possibly leaked Timer %p\n", mTimers[i]); } }
Timer::Timer(int tickTimeMs, bool stoppable) : mClockThread(NULL), mMsPerTick(tickTimeMs), mStoppable(stoppable), mMutex(true), mTicks(0) { TRACE_BEGIN(LOG_LVL_INFO); // If a negative tick time is specified then use 100ms if (mMsPerTick < 0) mMsPerTick = 100; // Start the timer thread running immediately start(); }
Selector::Selector( const char *name ) : mLock( true ), mThread( name == NULL ? "Selector" : name, this, &Selector::threadMain ), mUpdateFds( false ) { TRACE_BEGIN( LOG_LVL_INFO ); int res = pipe( mPipe ); LOG( "pipe reader %d writer %d", mPipe[ PIPE_READER ], mPipe[ PIPE_WRITER ] ); if ( res != 0 ) LOG_ERR_FATAL( "failed to create pipe" ); mRunning = true; mThread.Start(); mShutdown = false; }