Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

EntryPoint.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_EntryPoint_h_
00003 #define INCLUDED_EntryPoint_h_
00004 
00005 #include "IPC/Thread.h"
00006 #include "Shared/Resource.h"
00007 #include "Shared/WorldStatePool.h"
00008 
00009 //! manages a thread lock to serialize behavior computation and mark whether ::state is being read or written
00010 class EntryPoint : public Resource {
00011 public:
00012   //! pass this to MarkScope when using an EntryPoint over a section which will read ::state
00013   class WorldStateRead : public WorldStatePool::ReadRequest {
00014   public:
00015     //! constructor, hardcoded to use global ::state and non-blocking pool access
00016     WorldStateRead() : WorldStatePool::ReadRequest(state,false) {}
00017   };
00018   WorldStateRead defaultRead; //!< this instance will be used if an empty Data is passed to useResource (only safe to do because we get #lock first, so two threads won't be using the same data at the same time)
00019   
00020   //! pass this to MarkScope when using an EntryPoint over a section which will update ::state
00021   class WorldStateWrite : public WorldStatePool::WriteRequest {
00022   public:
00023     //! constructor, hardcoded to use global ::state and blocking pool access
00024     explicit WorldStateWrite(unsigned int frame_number) : WorldStatePool::WriteRequest(state,true,frame_number) {} 
00025   };
00026   
00027   //! constructor, need to specify the WorldStatePool (presumably it's in a shared memory region...)
00028   explicit EntryPoint(WorldStatePool& wspool) : Resource(), defaultRead(), pool(wspool), lock() {}
00029   
00030   //! an EmptyData implies a WorldStateRead should be passed on to the pool, requesting a write requires a WorldStateWrite to be passed
00031   virtual void useResource(Data& d) {
00032     static_cast<Resource&>(lock).useResource(emptyData); //important to get lock first to make sure using shared defaultRead is safe in multi-threaded env.
00033     pool.useResource(typeid(d)==typeid(Data) ? defaultRead : d);
00034   }
00035   //! an EmptyData implies a WorldStateRead should be passed on to the pool, requesting a write requires a WorldStateWrite to be passed
00036   virtual void releaseResource(Data& d) {
00037     if(WorldStateWrite * wsw=dynamic_cast<WorldStateWrite*>(&d)) {
00038       if(wsw->getComplete() && state!=wsw->src) {
00039         /*for(unsigned int i=0; i<NumPIDJoints; i++) {
00040         pids[i][0]=lastState->pids[i][0];
00041         pids[i][1]=lastState->pids[i][1];
00042         pids[i][2]=lastState->pids[i][2];
00043         }*/
00044         memcpy(state->pids,wsw->src->pids,sizeof(state->pids)); //pids is probably big enough it's better to use memcpy (?)
00045         state->vel_x=wsw->src->vel_x;
00046         state->vel_y=wsw->src->vel_y;
00047         state->vel_a=wsw->src->vel_a;
00048         state->vel_time=wsw->src->vel_time;
00049       }
00050     }
00051     pool.releaseResource(typeid(d)==typeid(Data) ? defaultRead : d);
00052     static_cast<Resource&>(lock).releaseResource(emptyData); //important to release lock last to make sure using shared defaultRead is safe in multi-threaded env.
00053   }
00054   
00055   //! this can be useful when planning to write, get the threadlock to do some initial setup before grabbing an entry from the pool
00056   ThreadNS::Lock& getLock() { return lock; }
00057   
00058 protected:
00059   WorldStatePool& pool; //!< pool which manages which WorldStates are being updated while old copies can still be read
00060   ThreadNS::Lock lock; //!< only one behavior runs at a time
00061 };
00062 
00063 
00064 /*! @file
00065  * @brief 
00066  * @author Ethan Tira-Thompson (ejt) (Creator)
00067  *
00068  * $Author: ejt $
00069  * $Name: tekkotsu-4_0 $
00070  * $Revision: 1.1 $
00071  * $State: Exp $
00072  * $Date: 2006/09/28 20:42:51 $
00073  */
00074 
00075 #endif

Tekkotsu Hardware Abstraction Layer 4.0
Generated Thu Nov 22 01:00:53 2007 by Doxygen 1.5.4