Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ProcessID.cc

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

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:49 2016 by Doxygen 1.6.3