[complement-svn] SF.net SVN: complement: [1598] trunk/complement/explore/test/virtual_time
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2007-07-04 10:40:55
|
Revision: 1598 http://svn.sourceforge.net/complement/?rev=1598&view=rev Author: complement Date: 2007-07-04 03:40:41 -0700 (Wed, 04 Jul 2007) Log Message: ----------- development Modified Paths: -------------- trunk/complement/explore/test/virtual_time/vtime.cc trunk/complement/explore/test/virtual_time/vtime.h Modified: trunk/complement/explore/test/virtual_time/vtime.cc =================================================================== --- trunk/complement/explore/test/virtual_time/vtime.cc 2007-07-03 09:56:08 UTC (rev 1597) +++ trunk/complement/explore/test/virtual_time/vtime.cc 2007-07-04 10:40:41 UTC (rev 1598) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/05/24 10:24:22 ptr> +// -*- C++ -*- Time-stamp: <07/06/27 01:04:38 ptr> #include "vtime.h" @@ -105,6 +105,8 @@ void VTmess::pack( std::ostream& s ) const { + __pack( s, code ); + __pack( s, src ); gvt.pack( s ); __pack( s, grp ); __pack( s, mess ); @@ -112,6 +114,8 @@ void VTmess::net_pack( std::ostream& s ) const { + __net_pack( s, code ); + __net_pack( s, src ); gvt.net_pack( s ); __net_pack( s, grp ); __net_pack( s, mess ); @@ -119,6 +123,8 @@ void VTmess::unpack( std::istream& s ) { + __unpack( s, code ); + __unpack( s, src ); gvt.unpack( s ); __unpack( s, grp ); __unpack( s, mess ); @@ -126,6 +132,8 @@ void VTmess::net_unpack( std::istream& s ) { + __net_unpack( s, code ); + __net_unpack( s, src ); gvt.net_unpack( s ); __net_unpack( s, grp ); __net_unpack( s, mess ); @@ -277,6 +285,159 @@ return *this; } +bool vtime_obj_rec::deliver( const VTmess& m ) +{ + // cout << self_id() << " " << ev.value().mess << endl; + + // cout << ev.value().gvt.gvt << endl; + + if ( order_correct( m ) ) { + cout << "Order correct" << endl; + lvt[m.src] += m.gvt.gvt; + lvt[m.src][m.grp][m.src] = vt.gvt[m.grp][m.src] + 1; + vt.gvt[m.grp] = vt::max( vt.gvt[m.grp], lvt[m.src][m.grp] ); + cout << vt.gvt << endl; + return true; + } + + cout << "Order not correct" << endl; + return false; +} + +bool vtime_obj_rec::order_correct( const VTmess& m ) +{ + gvtime gvt( m.gvt ); + + if ( vt.gvt[m.grp][m.src] + 1 != gvt[m.grp][m.src] ) { + cerr << "1" << endl; + cerr << vt.gvt[m.grp][m.src] << "\n" + << gvt[m.grp][m.src] + << endl; + return false; + } + + vtime xvt = lvt[m.src][m.grp] + gvt[m.grp]; + xvt[m.src] = 0; + + if ( !(xvt <= vt[m.grp]) ) { + cerr << "2" << endl; + cerr << xvt << "\n\n" + << vt[m.grp] << endl; + return false; + } + + for ( groups_container_type::const_iterator l = groups.begin(); l != groups.end(); ++l ) { + if ( *l != m.grp ) { + xvt = lvt[m.src][*l] + gvt[*l]; + if ( !(xvt <= vt[*l]) ) { + cerr << "3" << endl; + cerr << "group " << *l << xvt << "\n\n" + << vt[*l] << endl; + return false; + } + } + } + + return true; +} + +void VTDispatcher::VTDispatch( const VTmess& m ) +{ + gid_map_type::const_iterator g = grmap.find( m.grp ); + if ( g != grmap.end() ) { + for ( std::list<oid_type>::const_iterator o = g->second.begin(); o != g->second.end(); ++o ) { + vt_map_type::iterator i = vtmap.find( *o ); + if ( i != vtmap.end() ) { + if ( i->second.deliver( m ) ) { + stem::Event ev( m.code ); + ev.dest(i->first); + ev.value() = m.mess; + Send( ev ); + } + } + } + } +} + +DEFINE_RESPONSE_TABLE( VTDispatcher ) + EV_T_( ST_NULL, MESS, VTDispatch, VTmess ) +END_RESPONSE_TABLE + +char *Init_buf[128]; +VTDispatch *VTHandler::_vtdsp = 0; +static int *_rcount = 0; + +void VTHandler::Init::__at_fork_prepare() +{ +} + +void VTHandler::Init::__at_fork_child() +{ + if ( *_rcount != 0 ) { + VTHandler::_vtdsp->~VTDispatch(); + VTHandler::_vtdsp = new( VTHandler::_vtdsp ) VTDispatcher(); + } +} + +void VTHandler::Init::__at_fork_parent() +{ +} + +void VTHandler::Init::_guard( int direction ) +{ + static xmt::MutexRS _init_lock; + static int _count = 0; + + if ( direction ) { + if ( _count++ == 0 ) { +#ifdef _PTHREADS + _rcount = &_count; + pthread_atfork( __at_fork_prepare, __at_fork_parent, __at_fork_child ); +#endif + VTHandler::_vtdsp = new VTDispatcher(); + } + } else { + --_count; + if ( _count == 0 ) { + delete VTHandler::_vtdsp; + VTHandler::_vtdsp = 0; + } + } +} + +VTHandler::Init::Init() +{ _guard( 1 ); } + +VTHandler::Init::~Init() +{ _guard( 0 ); } + +void VTHandler::VTSend( const stem::Event& ev ) +{ +} + +VTHandler::VTHandler() : + EventHandler() +{ + new( Init_buf ) Init(); +} + +VTHandler::VTHandler( const char *info ) : + EventHandler( info ) +{ + new( Init_buf ) Init(); +} + +VTHandler::VTHandler( stem::addr_type id, const char *info ) : + EventHandler( id, info ) +{ + new( Init_buf ) Init(); +} + +VTHandler::~VTHandler() +{ + ((Init *)Init_buf)->~Init(); +} + void Proc::mess( const stem::Event_base<VTmess>& ev ) { cout << self_id() << " " << ev.value().mess << endl; Modified: trunk/complement/explore/test/virtual_time/vtime.h =================================================================== --- trunk/complement/explore/test/virtual_time/vtime.h 2007-07-03 09:56:08 UTC (rev 1597) +++ trunk/complement/explore/test/virtual_time/vtime.h 2007-07-04 10:40:41 UTC (rev 1598) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/05/18 00:26:00 ptr> +// -*- C++ -*- Time-stamp: <07/06/27 00:59:45 ptr> #ifndef __vtime_h #define __vtime_h @@ -129,19 +129,108 @@ void unpack( std::istream& s ); void net_unpack( std::istream& s ); - VTmess() - { } - VTmess( const VTmess& _gvt ) : - gvt( _gvt.gvt ), - mess( _gvt.mess ) - { } + VTmess() + { } + VTmess( const VTmess& _gvt ) : + gvt( _gvt.gvt ), + mess( _gvt.mess ) + { } - gvtime gvt; - group_type grp; - std::string mess; + stem::code_type code; + oid_type src; + gvtime gvt; + group_type grp; + std::string mess; }; +class vtime_obj_rec +{ + public: + typedef std::list<group_type> groups_container_type; + typedef std::hash_map<oid_type, gvtime_type> delta_vtime_type; + + void add_group( group_type g ) + { groups.push_back( g ); } + + void add_group_member( group_type g, oid_type p ) + { vt[g][p]; } + + bool deliver( const VTmess& ev ); + + stem::addr_type addr; // stem address of object + delta_vtime_type lvt; // last seen VT of neighbours + gvtime vt; // VT of object + groups_container_type groups; // member of groups + // delay pool should be here + + private: + bool order_correct( const VTmess& ); +}; + +class VTDispatcher : + public stem::EventHandler +{ + public: + VTDispatcher() + { } + + VTDispatcher( stem::addr_type id ) : + stem::EventHandler( id ) + { } + + void VTDispatch( const VTmess& ); + + void VTSend( const Event& e ); + + private: + typedef std::hash_map<oid_type, vtime_obj_rec> vt_map_type; + typedef std::hash_map<group_type, std::list<oid_type> > gid_map_type; + // oid_type map_gid( group_type ); + // gid_type -> (oid_type, oid_type, ...) + + // in our case we can use gid = hi bits | oid + + vt_map_type vtmap; + gid_map_type grmap; + + DECLARE_RESPONSE_TABLE( VTDispatcher, stem::EventHandler ); +}; + +class VTHandler : + public stem::EventHandler +{ + public: + class Init + { + public: + Init(); + ~Init(); + private: + static void _guard( int ); + static void __at_fork_prepare(); + static void __at_fork_child(); + static void __at_fork_parent(); + }; + + public: + VTHandler(); + explicit VTHandler( const char *info ); + explicit VTHandler( stem::addr_type id, const char *info = 0 ); + virtual ~VTHandler(); + + void VTSend( const Event& e ); + + template <class D> + void VTSend( const stem::Event_base<D>& e ) + { VTHandler::VTSend( stem::Event_convert<D>()( e ) ); } + + + private: + static class VTDispatcher *_vtdsp; +}; + + class Proc : public stem::EventHandler { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |