random_utils/src/ThreadSafeCanvas.cpp

Go to the documentation of this file.
00001     
00002 #include "random_utils/ThreadSafeCanvas.h"
00003 
00004 namespace random_utils
00005 {
00006 
00007 ThreadSafeCanvas* ThreadSafeCanvas::m_instance = 0;
00008 
00009 ThreadSafeCanvas::ThreadSafeCanvas(const std::string& p_windowName,
00010                                    unsigned int p_height, 
00011                                    unsigned int p_width,
00012                                    int  p_properties)
00013     : VRS::GlutCanvas(p_windowName, p_height, p_width, p_properties),
00014       ThreadSafeObject()
00015 {
00016     m_instance              = this;
00017     m_sleepRequested        = false;
00018     m_breakOut              = false;
00019 
00020     m_finishedRenderingCondition = SDL_CreateCond();
00021     m_finishedRenderingMutex = SDL_CreateMutex();
00022     m_wakeUpCondition = SDL_CreateCond();
00023     m_requestMutex = SDL_CreateMutex();
00024 }
00025 
00026 ThreadSafeCanvas::~ThreadSafeCanvas()
00027 {
00028     SDL_DestroyCond(m_finishedRenderingCondition);
00029     SDL_DestroyMutex(m_finishedRenderingMutex);
00030     SDL_DestroyCond(m_wakeUpCondition);
00031     SDL_DestroyMutex(m_requestMutex);
00032 }
00033 
00034 void
00035 ThreadSafeCanvas::sleep()
00036 {
00037     if(SDL_LockMutex(m_requestMutex) == -1)
00038         std::cerr << "ERROR: cannot lock request mutex" << std::endl;
00039 
00040     m_sleepRequested = true;
00041 
00042     if(SDL_CondWait(m_finishedRenderingCondition, m_requestMutex) == -1)
00043         std::cerr << "ERROR: cannot wait for render condition" << std::endl;
00044 
00045     if(!m_breakOut)
00046     {
00047         if(SDL_UnlockMutex(m_requestMutex) == -1)
00048             std::cerr << "ERROR: cannot unlock request mutex" << std::endl;
00049 
00050         if(SDL_LockMutex(m_finishedRenderingMutex) == -1)
00051             std::cerr << "ERROR: cannot lock render mutex" << std::endl;
00052         if(SDL_UnlockMutex(m_finishedRenderingMutex) == -1)
00053             std::cerr << "ERROR: cannot unlock render mutex" << std::endl;
00054     }
00055 }
00056 
00057 void
00058 ThreadSafeCanvas::finishRedisplay()
00059 {
00060     VRS::GlutCanvas::finishRedisplay();
00061 
00062     if(SDL_LockMutex(m_requestMutex) == -1)
00063         std::cerr << "ERROR: cannot lock request mutex" << std::endl;
00064 
00065     if(m_sleepRequested)
00066     {
00067         m_sleepRequested = false;
00068 
00069         if(SDL_LockMutex(m_finishedRenderingMutex) == -1)
00070             std::cerr << "ERROR: cannot lock render mutex" << std::endl;;
00071         
00072         if(SDL_CondSignal(m_finishedRenderingCondition) == -1)
00073             std::cerr << "ERROR: cannot signal render condition" << std::endl;
00074 
00075         if(SDL_UnlockMutex(m_requestMutex) == -1)
00076             std::cerr << "ERROR: cannot unlock request mutex" << std::endl;;
00077 
00078         if(SDL_CondWait(m_wakeUpCondition, m_finishedRenderingMutex) == -1)
00079             std::cerr << "ERROR: cannot wait for wakeup" << std::endl;;
00080 
00081         if(SDL_UnlockMutex(m_finishedRenderingMutex) == -1)
00082             std::cerr << "ERROR: cannot unlock render mutex" << std::endl;;
00083     }
00084     else
00085         if(SDL_UnlockMutex(m_requestMutex) == -1)
00086             std::cerr << "ERROR: cannot unlock request mutex" << std::endl;
00087 }
00088 
00089 void
00090 ThreadSafeCanvas::wakeUp()
00091 {
00092     if(SDL_CondSignal(m_wakeUpCondition) == -1)
00093         std::cerr << "ERROR: cannot signal wake up" << std::endl;
00094 }
00095 
00096 void
00097 ThreadSafeCanvas::breakOut()
00098 {
00099     m_breakOut = true;
00100 
00101     if(SDL_CondSignal(m_wakeUpCondition) == -1)
00102         std::cerr << "ERROR: cannot signal wake up" << std::endl;
00103     if(SDL_CondSignal(m_finishedRenderingCondition) == -1)
00104         std::cerr << "ERROR: cannot signal render condition" << std::endl;    
00105 }
00106 
00107 }

Generated on Fri May 11 21:01:58 2007 for Random Racer by  doxygen 1.5.1