Revision: 7931
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7931&view=rev
Author:   rtv
Date:     2009-06-30 22:03:37 +0000 (Tue, 30 Jun 2009)

Log Message:
-----------
more STLization and cleaner update_list & thread model

Modified Paths:
--------------
    code/stage/trunk/libstage/model.cc
    code/stage/trunk/libstage/model_getset.cc
    code/stage/trunk/libstage/model_load.cc
    code/stage/trunk/libstage/stage.hh
    code/stage/trunk/libstage/world.cc
    code/stage/trunk/libstage/worldgui.cc
    code/stage/trunk/worlds/fasr.world

Modified: code/stage/trunk/libstage/model.cc
===================================================================
--- code/stage/trunk/libstage/model.cc  2009-06-29 22:58:45 UTC (rev 7930)
+++ code/stage/trunk/libstage/model.cc  2009-06-30 22:03:37 UTC (rev 7931)
@@ -209,7 +209,7 @@
         log_state(false),
     map_resolution(0.1),
     mass(0),
-    on_update_list( false ),
+       //   on_update_list( false ),
     on_velocity_list( false ),
     parent(parent),
     pose(),
@@ -224,15 +224,16 @@
     thread_safe( false ),
     trail( g_array_new( false, false, sizeof(stg_trail_item_t) )),
     type(type),        
+       update_list_num( -1 ),
     used(false),
     velocity(),
     watts(0.0),
-        watts_give(0.0),
-        watts_take(0.0),        
+       watts_give(0.0),
+       watts_take(0.0),         
     wf(NULL),
     wf_entity(0),
     world(world),
-        world_gui( dynamic_cast<WorldGui*>( world ) )
+       world_gui( dynamic_cast<WorldGui*>( world ) )
 {
   //assert( modelsbyid );
   assert( world );
@@ -291,17 +292,17 @@
 
 void Model::StartUpdating()
 {
-  if( ! on_update_list )
-    {
-      on_update_list = true;
-      world->StartUpdatingModel( this );
-    }
+  if( update_list_num < 0 )
+       update_list_num = world->UpdateListAdd( this );
 }
 
 void Model::StopUpdating()
 {
-  on_update_list = false;
-  world->StopUpdatingModel( this );
+  if( update_list_num >= 0 )
+       {       
+         world->UpdateListRemove( this );
+         update_list_num = -1;
+       }
 }
 
 // this should be called after all models have loaded from the

Modified: code/stage/trunk/libstage/model_getset.cc
===================================================================
--- code/stage/trunk/libstage/model_getset.cc   2009-06-29 22:58:45 UTC (rev 
7930)
+++ code/stage/trunk/libstage/model_getset.cc   2009-06-30 22:03:37 UTC (rev 
7931)
@@ -212,16 +212,17 @@
   
   if( on_velocity_list && vel.IsZero() )        
     {
-      world->StopUpdatingModelPose( this );
+      world->VelocityListRemove( this );
       on_velocity_list = false;
     }
 
   if( (!on_velocity_list) && (!vel.IsZero()) )          
     {
-      world->StartUpdatingModelPose( this );
+      world->VelocityListAdd( this );
       on_velocity_list = true;
     }
 
+  // todo ?
   //CallCallbacks( &this->velocity );
 }
 

Modified: code/stage/trunk/libstage/model_load.cc
===================================================================
--- code/stage/trunk/libstage/model_load.cc     2009-06-29 22:58:45 UTC (rev 
7930)
+++ code/stage/trunk/libstage/model_load.cc     2009-06-30 22:03:37 UTC (rev 
7931)
@@ -201,11 +201,11 @@
                LoadDataBaseEntries( wf, wf_entity );
 
   if (vis.gravity_return)
-  {
-    Velocity vel = GetVelocity();
-    this->SetVelocity( vel );
-    world->StartUpdatingModel( this );
-  }
+       {
+         Velocity vel = GetVelocity();
+         this->SetVelocity( vel );
+         StartUpdating();
+       }
 
   if( wf->PropertyExists( wf_entity, "friction" ))
   {

Modified: code/stage/trunk/libstage/stage.hh
===================================================================
--- code/stage/trunk/libstage/stage.hh  2009-06-29 22:58:45 UTC (rev 7930)
+++ code/stage/trunk/libstage/stage.hh  2009-06-30 22:03:37 UTC (rev 7931)
@@ -233,6 +233,9 @@
   /** Obtain the components of a color */
   void stg_color_unpack( stg_color_t col, 
                                                                 double* r, 
double* g, double* b, double* a );
+  
+  //typedef std::vector<Model*> ModelPtrVec;
+  //typedef std::vector<Model&> ModelRefVec;
 
   /** specify a rectangular size */
   class Size
@@ -823,19 +826,19 @@
     friend class Canvas; // allow Canvas access to our private members
         
   protected:
-        std::vector<Model*> children;
+       std::vector<Model*> children;
     bool debug;
-        GList* puck_list;
+       GList* puck_list;
     char* token;
-        
+       
         void Load( Worldfile* wf, int section );
         void Save( Worldfile* wf, int section );
         
   public:
-       
+       
     /** get the children of the this element */
-        std::vector<Model*>& GetChildren(){ return children;}
-     
+       std::vector<Model*>& GetChildren(){ return children;}
+    
     /** recursively call func( model, arg ) for each descendant */
     void ForEachDescendant( stg_model_callback_t func, void* arg );
         
@@ -952,19 +955,18 @@
         bool show_clock; ///< iff true, print the sim time on stdout
         unsigned int show_clock_interval; ///< updates between clock xoutputs
     GMutex* thread_mutex; ///< protect the worker thread management stuff
-    //GThreadPool *threadpool; ///<worker threads for updating some sensor 
models in parallel
     int total_subs; ///< the total number of subscriptions to all models
-    GList* velocity_list; ///< Models with non-zero velocity and should have 
their poses updated
+       std::vector<Model*> velocity_list; ///< Models with non-zero velocity 
and should have their poses updated
        
        unsigned int worker_threads; ///< the number of worker threads to use
        unsigned int threads_working; ///< the number of worker threads not yet 
finished
     GCond* threads_start_cond; ///< signalled to unblock worker threads
     GCond* threads_done_cond; ///< signalled by last worker thread to unblock 
main thread
-               
-               /** Keep a list of all models with detectable fiducials. This
-                               avoids searching the whole world for fiducials. 
*/
-               std::set<Model*> models_with_fiducials;
-               
+       
+       /** Keep a list of all models with detectable fiducials. This
+               avoids searching the whole world for fiducials. */
+       std::set<Model*> models_with_fiducials;
+       
   protected:    
 
         std::list<std::pair<stg_world_callback_t,void*> > cb_list; ///< List 
of callback functions and arguments
@@ -978,8 +980,7 @@
         std::map<stg_point_int_t,SuperRegion*> superregions;
     SuperRegion* sr_cached; ///< The last superregion looked up by this world
        
-       std::vector<Model*> nonreentrant_update_list; ///< It is NOT safe to 
call these model's Update() in parallel
-       std::vector<std::vector<Model*> > reentrant_update_lists;  ///< It is 
safe to call these model's Update() in parallel
+       std::vector<std::vector<Model*> > update_lists;  
         
     long unsigned int updates; ///< the number of simulated time steps 
executed so far
     Worldfile* wf; ///< If set, points to the worldfile used to create this 
world
@@ -1098,13 +1099,13 @@
     /** Returns true iff the current time is greater than the time we
                  should quit */
     bool PastQuitTime();
-        
-    void StartUpdatingModel( Model* mod );  
-        void StopUpdatingModel( Model* mod );
+       
+    int UpdateListAdd( Model* mod );  
+       void UpdateListRemove( Model* mod );
     
-    void StartUpdatingModelPose( Model* mod );
-        void StopUpdatingModelPose( Model* mod );
-    
+       void VelocityListAdd( Model* mod );
+       void VelocityListRemove( Model* mod );
+
     static gpointer update_thread_entry( std::pair<World*,int>* info );
         
   public:
@@ -1808,7 +1809,7 @@
         bool log_state; ///< iff true, model state is logged
         stg_meters_t map_resolution;
         stg_kg_t mass;
-        bool on_update_list;
+       //bool on_update_list;
         bool on_velocity_list;
 
         /** Pointer to the parent of this model, possibly NULL. */
@@ -1868,6 +1869,9 @@
         bool thread_safe;
         GArray* trail;
         stg_model_type_t type;  
+       /** The index into the world's vector of update lists. Initially
+           -1, to indicate that it is not on a list yet. */
+       int update_list_num; 
         bool used;   ///< TRUE iff this model has been returned by 
GetUnusedModelOfType()  
         Velocity velocity;
         stg_watts_t watts;///< power consumed by this model

Modified: code/stage/trunk/libstage/world.cc
===================================================================
--- code/stage/trunk/libstage/world.cc  2009-06-29 22:58:45 UTC (rev 7930)
+++ code/stage/trunk/libstage/world.cc  2009-06-30 22:03:37 UTC (rev 7931)
@@ -91,7 +91,7 @@
   show_clock_interval( 100 ), // 10 simulated seconds using defaults
   thread_mutex( g_mutex_new() ),
   total_subs( 0 ), 
-  velocity_list( NULL ),
+  velocity_list(),
   worker_threads( 0 ),
   threads_working( 0 ),
   threads_start_cond( g_cond_new() ),
@@ -109,8 +109,7 @@
   sim_time( 0 ),
   superregions(),
   sr_cached(NULL),
-  nonreentrant_update_list(),
-  reentrant_update_lists(1),
+  update_lists(1),
   updates( 0 ),
   wf( NULL ),
   paused( false )
@@ -179,7 +178,7 @@
          //puts( "worker thread awakes" );
          
          // loop over the list of rentrant models for this thread
-         FOR_EACH( it, world->reentrant_update_lists[thread_instance] )
+         FOR_EACH( it, world->update_lists[thread_instance] )
                 {
                        Model* mod = *it;
                        //printf( "thread %d updating model %s (%p)\n", 
thread_instance, mod->Token(), mod );
@@ -343,12 +342,12 @@
          
          if( worker_threads > 0 )
                {
-                 reentrant_update_lists.resize( worker_threads );
+                 update_lists.resize( worker_threads + 1 );
 
                  // kick off count threads.
                  for( unsigned int t=0; t<worker_threads; t++ )
                        {
-                         std::pair<World*,int> *p = new std::pair<World*,int>( 
this, t );
+                         std::pair<World*,int> *p = new std::pair<World*,int>( 
this, t+1 );
                          g_thread_create( 
(GThreadFunc)World::update_thread_entry, 
                                                           p,
                                                           false, 
@@ -406,9 +405,8 @@
  
   g_hash_table_remove_all( models_by_name );
                
-  reentrant_update_lists.clear();
-  nonreentrant_update_list.clear();
-
+  update_lists.resize(1);
+  
   g_list_free( ray_list );
   ray_list = NULL;
 
@@ -533,31 +531,24 @@
        return false;
 
   dirty = true; // need redraw 
-
-  // upate all positions first
-  LISTMETHOD( velocity_list, Model*, UpdatePose );
   
+  // upate all positions first
+  FOR_EACH( it, velocity_list )
+       (*it)->UpdatePose();
+
   // test all models that supply charge to see if they are touching
   // something that takes charge
   LISTMETHOD( charge_list, Model*, UpdateCharge );
   
   // then update all models on the update lists
-  FOR_EACH( it, nonreentrant_update_list )
+  FOR_EACH( it, update_lists[0] )
         {
                //printf( "thread MAIN updating model %s\n", (*it)->Token() );
                (*it)->UpdateIfDue();
         }
   
-  //printf( "nonre list length %d\n", g_list_length( nonreentrant_update_list 
) );
-  //printf( "re list length %d\n", g_list_length( reentrant_update_list ) );
-
-  if( worker_threads == 0 ) // do all the work in this thread
+  if( worker_threads > 0 )
     {
-         FOR_EACH( it, reentrant_update_lists[0] )
-               (*it)->UpdateIfDue();
-    }
-  else // use worker threads
-    {
          g_mutex_lock( thread_mutex );
          threads_working = worker_threads; 
          // unblock the workers - they are waiting on this condition var
@@ -576,11 +567,11 @@
 
                // TODO: allow threadsafe callbacks to be called in worker
                // threads
-
+         
                // now call all the callbacks in each list
-               FOR_EACH( it1, reentrant_update_lists )
-                 FOR_EACH( it2, *it1 )
-                 (*it2)->CallUpdateCallbacks();
+         for( unsigned int i=1; i<update_lists.size(); i++ )
+               FOR_EACH( it, update_lists[i] )
+                 (*it)->CallUpdateCallbacks();
     }
   
   if( show_clock && ((this->updates % show_clock_interval) == 0) )
@@ -1076,45 +1067,36 @@
   g_hash_table_insert( option_table, (void*)opt->htname, opt );
 }
 
-void World::StartUpdatingModel( Model* mod )
-{ 
-  //printf( "Adding model %s to update list ", mod->Token() );
+int World::UpdateListAdd( Model* mod )
+{   
+  int index = 0;
   
-  int index = worker_threads > 0 ? mod->id%worker_threads : 0;
+  if( mod->thread_safe && worker_threads > 0 )
+       index = (random() % worker_threads) + 1;
   
+  update_lists[index].push_back( mod );
 
-  //   if( mod->thread_safe )
-  //   printf( "reentrant[ %d ]\n", index );
-  //   else
-  //   printf( " nonreentrant\n" );
+  //printf( "update_list[%d] added model %s\n", index, mod->Token() );
 
-  // choose the right update list
-  std::vector<Model*>& vec = mod->thread_safe ? reentrant_update_lists[index] 
: nonreentrant_update_list;  
-  // and add the model if not in the list already
-  if( find( vec.begin(), vec.end(), mod ) == vec.end() )                 
-       vec.push_back( mod );
+  return index;
 }
 
-void World::StopUpdatingModel( Model* mod )
+void World::UpdateListRemove( Model* mod )
 { 
-  int index = worker_threads > 0 ? mod->id%worker_threads : 0;
-
   // choose the right update list
-  std::vector<Model*>& vec = mod->thread_safe ? reentrant_update_lists[index] 
: nonreentrant_update_list;
+  std::vector<Model*>& vec = update_lists[ mod->update_list_num ];
   // and erase the model from it
   vec.erase( remove( vec.begin(), vec.end(), mod ));
 }
 
-void World::StartUpdatingModelPose( Model* mod )
+void World::VelocityListAdd( Model* mod )
 { 
-  if( ! g_list_find( velocity_list, mod ) )
-       velocity_list = g_list_append( velocity_list, mod ); 
+  velocity_list.push_back( mod );
 }
 
-void World::StopUpdatingModelPose( Model* mod )
+void World::VelocityListRemove( Model* mod )
 { 
-  // TODO XX figure out how to handle velcoties a bit better
-  //velocity_list = g_list_remove( velocity_list, mod ); 
+  velocity_list.erase( remove( velocity_list.begin(), velocity_list.end(), mod 
));
 }
 
 stg_usec_t World::SimTimeNow(void)

Modified: code/stage/trunk/libstage/worldgui.cc
===================================================================
--- code/stage/trunk/libstage/worldgui.cc       2009-06-29 22:58:45 UTC (rev 
7930)
+++ code/stage/trunk/libstage/worldgui.cc       2009-06-30 22:03:37 UTC (rev 
7931)
@@ -856,18 +856,13 @@
   std::set<Option*, Option::optComp> options;
   std::vector<Option*> modOpts;
   
-  FOR_EACH( it1, reentrant_update_lists )
+  FOR_EACH( it1, update_lists )
        FOR_EACH( it2, (*it1) )
        {
          modOpts = (*it2)->getOptions();
          options.insert( modOpts.begin(), modOpts.end() );     
        }
   
-  FOR_EACH( it, nonreentrant_update_list )
-        {
-               modOpts = (*it)->getOptions();
-               options.insert( modOpts.begin(), modOpts.end() );       
-        }
   
   drawOptions.assign( options.begin(), options.end() );
   

Modified: code/stage/trunk/worlds/fasr.world
===================================================================
--- code/stage/trunk/worlds/fasr.world  2009-06-29 22:58:45 UTC (rev 7930)
+++ code/stage/trunk/worlds/fasr.world  2009-06-30 22:03:37 UTC (rev 7931)
@@ -11,7 +11,7 @@
 paused 1
 
 # time to pause (in GUI mode) or quit (in headless mode) the simulation
-quit_time 360 # 1 hour of simulated time
+quit_time 3600 # 1 hour of simulated time
 
 resolution 0.02
 
@@ -21,11 +21,11 @@
 # configure the GUI window
 window
 ( 
-  size [ 788.000 842.000 ] 
+  size [ 600.000 600.000 ] 
 
-  center [ -0.067 -0.907 ] 
+  center [ 0.250 -0.680 ] 
   rotate [ 0 0 ]
-  scale 40.786 
+  scale 30.643 
   
   show_data 1
   show_flags 1
@@ -133,7 +133,7 @@
 
 define autorob pioneer2dx                
 (               
- sicklaser( samples 320 range_max 5 laser_return 2 watts 30 )
+ sicklaser( samples 32 range_max 5 laser_return 2 watts 30 )
  ctrl "fasr"
  joules 100000 
  joules_capacity 400000 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to