📄 heartbeatimpl.cpp
字号:
//// This file is part of the "More for C++" library//// Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)//// The "More for C++" library is free software; you can redistribute it and/or// modify it under the terms of the license that comes with this package.//// Read "license.txt" for more details.//// THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES// OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#include <more/synchronized.hpp>#include "heartbeatimpl.hpp"using namespace more;using namespace more::os;using namespace more::util;////////////////////////////////////////////////////////////////////////////////HeartbeatImpl::HeartbeatImpl( ): m_pMutex( Mutex::create( ) ), m_pHandles( CREATE Vector<p<HandleImpl> >( ) ), m_nInitCounter( 0 ){ Singleton<HeartbeatImpl>::setInstance( this );}////////////////////////////////////////////////////////////////////////////////void HeartbeatImpl::destroy( ){ Singleton<HeartbeatImpl>::setInstance( 0 );}////////////////////////////////////////////////////////////////////////////////void HeartbeatImpl::init( ){ synchronized( m_pMutex ) { m_nInitCounter++; if( m_pHeartbeatThread == 0 ) { m_pHeartbeatThread = Thread::create( ); m_pHeartbeatThread -> execute( CREATE HeartbeatRunner( this ) ); } } end_synchronized}////////////////////////////////////////////////////////////////////////////////void HeartbeatImpl::shutDown( ){ synchronized( m_pMutex ) { if( m_nInitCounter > 0 ) { m_nInitCounter--; } if( m_nInitCounter == 0 && m_pHeartbeatThread != 0 ) { m_pHeartbeatThread -> terminate( ); m_pHeartbeatThread = 0; } } end_synchronized}////////////////////////////////////////////////////////////////////////////////p<Heartbeat::Handle> HeartbeatImpl::addHandler( const p<Heartbeat::Handler>& pHandler){ p<HandleImpl> pResult = CREATE HandleImpl( pHandler ); synchronized( m_pMutex ) { m_pHandles -> add( pResult ); } end_synchronized return pResult.out( );}////////////////////////////////////////////////////////////////////////////////void HeartbeatImpl::run( const p<Thread::Controller>& pController){ while( pController -> scheduled( ) ) { if( m_pHandles -> getSize( ) == 0 ) { Thread::sleep( 100 ); } else { Array<p<HandleImpl> > pHandles = m_pHandles -> toArray( ); for( size_t i = 0; i < pHandles.getLength( ); i++ ) { if( pHandles[i] -> removed( ) ) { m_pHandles -> remove( pHandles[i] ); } else { try { pHandles[i] -> passHeartbeat( ); } catch( ... ) { } } Thread::sleep( 1000 / pHandles.getLength( ) ); } } }}////////////////////////////////////////////////////////////////////////////////HeartbeatImpl::HandleImpl::HandleImpl( const p<Heartbeat::Handler>& pHandler): m_pMutex( Mutex::create( ) ), m_pHandler( pHandler ), m_bRemoved( false ){}////////////////////////////////////////////////////////////////////////////////void HeartbeatImpl::HandleImpl::passHeartbeat( ){ if( !removed( ) ) { synchronized( m_pMutex ) { if( !removed( ) ) { try { m_pHandler -> onHeartbeat( ); } catch( ... ) { } } } end_synchronized }}////////////////////////////////////////////////////////////////////////////////bool HeartbeatImpl::HandleImpl::removed( ){ return m_bRemoved;}////////////////////////////////////////////////////////////////////////////////void HeartbeatImpl::HandleImpl::removeHandler( ){ if( !removed( ) ) { synchronized( m_pMutex ) { if( !removed( ) ) { m_bRemoved = true; } } end_synchronized }}////////////////////////////////////////////////////////////////////////////////HeartbeatImpl::HeartbeatRunner::HeartbeatRunner( const p<HeartbeatImpl>& pHeartbeatImpl): m_pHeartbeatImpl( pHeartbeatImpl ){}////////////////////////////////////////////////////////////////////////////////void HeartbeatImpl::HeartbeatRunner::run( const p<Thread::Controller>& pController){ m_pHeartbeatImpl -> run( pController );}////////////////////////////////////////////////////////////////////////////////SingletonCreator<HeartbeatImpl> HeartbeatImpl::singletonCreator;////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -