Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ProcessID.cc

Go to the documentation of this file.
00001 #include "ProcessID.h"
00002 
00003 #include "Shared/WorldState.h"
00004 
00005 #ifdef PLATFORM_APERIOS
00006 #  include <iostream>
00007 using namespace stacktrace;
00008 #endif
00009 
00010 using namespace std;
00011 
00012 namespace ProcessID {
00013   ProcessID_t ID=NumProcesses; //!< holds ID number
00014   
00015 #ifndef PLATFORM_APERIOS
00016   ProcessID_t getUnhookedID() { return ID; }
00017   void setUnhookedID(ProcessID_t id) { ID=id; }
00018   
00019   ProcessID_t (*getIDHook)()=&getUnhookedID; //!< hook allows overriding the ID system, set with ProcessID::setIDHooks
00020   void (*setIDHook)(ProcessID_t id)=&setUnhookedID; //!< hook allows overriding the ID system, set with ProcessID::setIDHooks
00021   
00022   // on "normal" platforms we can trust the id specified by the process to stay consistent
00023   ProcessID_t getID() { return (*getIDHook)(); }
00024   
00025   void setID(ProcessID_t id) { (*setIDHook)(id); }
00026 
00027   void setIDHooks(ProcessID_t (*customGetID)(), void (*customSetID)(ProcessID_t id)) {
00028     getIDHook = (customGetID==NULL) ? &getUnhookedID : customGetID;
00029     setIDHook = (customSetID==NULL) ? &setUnhookedID : customSetID;
00030   }
00031 
00032 #else
00033   // but on the Aibo, we have to unroll the stack to see which thread it is
00034 
00035   //! array of StackFrame structures, one per #NumProcesses, set by setMap()
00036   StackFrame* frames=NULL;
00037   
00038   //! 
00039   void setMap(stacktrace::StackFrame idmap[]) {
00040     frames=idmap;
00041   }
00042   
00043   stacktrace::StackFrame* getMapFrame() {
00044     if(frames==NULL) //setMap hasn't been called yet
00045       return NULL;
00046     if(ID==NumProcesses) // ID hasn't been set
00047       return NULL;
00048     return &frames[ID];
00049   }
00050 
00051 
00052   ProcessID_t getID() {
00053     if(frames==NULL) { //setMap hasn't been called yet
00054       //cerr << "getID() called before setMap() id==" << ID << endl;
00055       //displayCurrentStackTrace();
00056       return ID;
00057     }
00058     StackFrame f;
00059 #ifdef DEBUG_STACKTRACE
00060     f.debug=(state!=NULL)?(state->buttons[LFrPawOffset]>.1):1;
00061     if(f.debug)
00062       fprintf(stderr,"getID() for %d: ",ID);
00063 #endif
00064     getCurrentStackFrame(&f);
00065     while(unrollStackFrame(&f,&f)) {}
00066     for(unsigned int i=0; i<NumProcesses; i++) {
00067       if(frames[i].gp==f.gp) {
00068 #ifdef DEBUG_STACKTRACE
00069         if(i!=(unsigned int)ID || f.debug)
00070           cout << "getID() from " << ID << " is " << i << endl;
00071 #endif
00072         return static_cast<ProcessID_t>(i);
00073       }
00074     }
00075     cerr << "ERROR: Unknown entry point (sp=" << f.sp << ",ra=" << (void*)f.ra << ",gp=" << (void*)f.gp << "), implied process " << ID << endl;
00076     displayCurrentStackTrace();
00077     cerr << "Map:" << endl;
00078     for(unsigned int i=0; i<NumProcesses; i++)
00079       cerr << "  " << i << " (sp=" << frames[i].sp << ",ra=" << (void*)frames[i].ra << ",gp=" << (void*)frames[i].gp << ")" << endl;
00080     return ID;
00081   }
00082   
00083   void setID(ProcessID_t id) { ID=id; }
00084 
00085 #endif
00086 }
00087 
00088 
00089 
00090 /*! @file
00091  * @brief Declares the static ProcessID::ID, that's all
00092  * @author ejt (Creator)
00093  *
00094  * $Author: ejt $
00095  * $Name: tekkotsu-4_0 $
00096  * $Revision: 1.7 $
00097  * $State: Exp $
00098  * $Date: 2007/03/15 04:14:21 $
00099  */
00100 

Tekkotsu v4.0
Generated Thu Nov 22 00:54:55 2007 by Doxygen 1.5.4