Revision: 2441
          http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=2441&view=rev
Author:   rorthomas
Date:     2012-02-04 00:37:06 +0000 (Sat, 04 Feb 2012)
Log Message:
-----------
new threading basics, WIP

Modified Paths:
--------------
    trunk/source/main/CMakeLists.txt
    trunk/source/main/RoRVersion.h
    trunk/source/main/gameplay/BeamEngine.cpp
    trunk/source/main/gameplay/Savegame.cpp
    trunk/source/main/physics/Beam.cpp
    trunk/source/main/physics/BeamFactory.cpp

Added Paths:
-----------
    trunk/source/main/physics/threading/
    trunk/source/main/physics/threading/BeamThread.cpp
    trunk/source/main/physics/threading/BeamThread.h
    trunk/source/main/physics/threading/BeamWorker.cpp
    trunk/source/main/physics/threading/BeamWorker.h
    trunk/source/main/physics/threading/BeamWorkerManager.cpp
    trunk/source/main/physics/threading/BeamWorkerManager.h

Removed Paths:
-------------
    trunk/source/main/physics/BeamWaitAndLock.h

Modified: trunk/source/main/CMakeLists.txt
===================================================================
--- trunk/source/main/CMakeLists.txt    2012-02-03 20:02:17 UTC (rev 2440)
+++ trunk/source/main/CMakeLists.txt    2012-02-04 00:37:06 UTC (rev 2441)
@@ -108,6 +108,7 @@
   add_sub_dir(${BINNAME} physics/flex)
   add_sub_dir(${BINNAME} physics/input_output)
   add_sub_dir(${BINNAME} physics/mplatform)
+  add_sub_dir(${BINNAME} physics/threading)
   add_sub_dir(${BINNAME} physics/utils)
   add_sub_dir(${BINNAME} physics/water)
   if(ROR_USE_ANGELSCRIPT)

Modified: trunk/source/main/RoRVersion.h
===================================================================
--- trunk/source/main/RoRVersion.h      2012-02-03 20:02:17 UTC (rev 2440)
+++ trunk/source/main/RoRVersion.h      2012-02-04 00:37:06 UTC (rev 2441)
@@ -33,7 +33,7 @@
 #define SVN_ID "$Id$"
 
 #define ROR_VERSION_STRING_SHORT      "0.39"
-#define ROR_VERSION_STRING            "0.39.6"
-#define ROR_VERSION_STRING_WIN         0,39,6,0
+#define ROR_VERSION_STRING            "0.39.7"
+#define ROR_VERSION_STRING_WIN         0,39,7,0
 
 #endif //RORVERSION_H__

Modified: trunk/source/main/gameplay/BeamEngine.cpp
===================================================================
--- trunk/source/main/gameplay/BeamEngine.cpp   2012-02-03 20:02:17 UTC (rev 
2440)
+++ trunk/source/main/gameplay/BeamEngine.cpp   2012-02-04 00:37:06 UTC (rev 
2441)
@@ -71,7 +71,6 @@
        , shiftval(0)
        , shiftclock(0)
        , postshiftclock(0)
-       , status() // alternative: std::fill(status, status + 256, 0);
 {
        gearsRatio[0] = -gearsRatio[0];
        for(std::vector< float >::iterator it = gearsRatio.begin(); it != 
gearsRatio.end(); ++it)

Modified: trunk/source/main/gameplay/Savegame.cpp
===================================================================
--- trunk/source/main/gameplay/Savegame.cpp     2012-02-03 20:02:17 UTC (rev 
2440)
+++ trunk/source/main/gameplay/Savegame.cpp     2012-02-04 00:37:06 UTC (rev 
2441)
@@ -23,7 +23,7 @@
 #include "Savegame.h"
 
 #include "BeamFactory.h"
-#include "BeamWaitAndLock.h"
+//#include "BeamWaitAndLock.h"
 #include "RoRVersion.h"
 #include <Ogre.h>
 
@@ -56,7 +56,7 @@
        FILE *f = fopen(filename.c_str(), "wb");
 
        // wait for engine sync
-       BEAMLOCK();
+       //BEAMLOCK();
 
        // TODO: show error
        if(!f)
@@ -224,7 +224,7 @@
        FILE *f = fopen(filename.c_str(), "rb");
 
        // wait for engine sync
-       BEAMLOCK();
+       //BEAMLOCK();
 
        if(!f)
        {

Modified: trunk/source/main/physics/Beam.cpp
===================================================================
--- trunk/source/main/physics/Beam.cpp  2012-02-03 20:02:17 UTC (rev 2440)
+++ trunk/source/main/physics/Beam.cpp  2012-02-04 00:37:06 UTC (rev 2441)
@@ -26,7 +26,7 @@
 #include "BeamEngine.h"
 #include "BeamFactory.h"
 #include "BeamStats.h"
-#include "BeamWaitAndLock.h"
+//#include "BeamWaitAndLock.h"
 #include "buoyance.h"
 #include "CmdKeyInertia.h"
 #include "collisions.h"
@@ -611,7 +611,7 @@
        }
 
        // wait for the thread(s) to be ready
-       BEAMLOCK();
+       //BEAMLOCK();
 
        // all finished? so start network stuff
        if (networked)
@@ -2098,7 +2098,7 @@
                {
                        //block until all threads are done
                        {
-                               BEAMLOCK();
+                               //BEAMLOCK();
                                for (int t=0; t<numtrucks; t++)
                                {
                                        if (!trucks[t]) continue;
@@ -2159,7 +2159,7 @@
 
 void Beam::prepareShutdown()
 {
-       BEAMLOCK();
+       //BEAMLOCK();
 }
 
 void Beam::sendStreamSetup()

Modified: trunk/source/main/physics/BeamFactory.cpp
===================================================================
--- trunk/source/main/physics/BeamFactory.cpp   2012-02-03 20:02:17 UTC (rev 
2440)
+++ trunk/source/main/physics/BeamFactory.cpp   2012-02-04 00:37:06 UTC (rev 
2441)
@@ -29,13 +29,14 @@
 #include "collisions.h"
 #include "Settings.h"
 #include "BeamEngine.h"
+#include "BeamWorker.h"
 
 #ifdef USE_MYGUI
 #include "gui_mp.h"
 #include "gui_menu.h"
 #include "DashBoardManager.h"
 #endif // USE_MYGUI
-#include "BeamWaitAndLock.h"
+//#include "BeamWaitAndLock.h"
 
 using namespace Ogre;
 
@@ -77,6 +78,11 @@
 
        if (BSETTING("2DReplay", false))
                tdr = new TwoDReplay();
+
+
+       // TEST for Beamworker, add two instances
+       //new BeamWorker();
+       //new BeamWorker();
 }
 
 BeamFactory::~BeamFactory()
@@ -342,7 +348,7 @@
 bool BeamFactory::syncRemoteStreams()
 {
        // block until all threads done
-       BEAMLOCK();
+       //BEAMLOCK();
 
        // we override this here, so we know if something changed and could 
update the player list
        // we delete and add trucks in there, so be sure that nothing runs as 
we delete them ...
@@ -566,7 +572,7 @@
 
        // block until all threads done
        {
-               BEAMLOCK();
+               //BEAMLOCK();
 
                // synced delete
                trucks[b->trucknum] = 0;
@@ -581,25 +587,6 @@
 #endif // USE_MYGUI
 }
 
-void BeamFactory::_waitForSyncAndLock()
-{
-       // block until all threads done
-       if (thread_mode > THREAD_SINGLE)
-       {
-               MUTEX_LOCK(&done_count_mutex);
-               while (done_count>0)
-                       pthread_cond_wait(&done_count_cv, &done_count_mutex);
-               MUTEX_UNLOCK(&done_count_mutex);
-       }
-
-       MUTEX_LOCK(&vehicles_mutex);
-}
-
-void BeamFactory::_ReleaseLock()
-{
-       MUTEX_UNLOCK(&vehicles_mutex);
-}
-
 void BeamFactory::removeCurrentTruck()
 {
        removeTruck(current_truck);
@@ -759,3 +746,86 @@
        }
 #endif // USE_MYGUI
 }
+
+#if 0
+void *threadstart()
+{
+#ifdef USE_CRASHRPT
+       if(BSETTING("NoCrashRpt", true))
+       {
+               // add the crash handler for this thread
+               CrThreadAutoInstallHelper cr_thread_install_helper;
+               MYASSERT(cr_thread_install_helper.m_nInstallStatus==0);
+       }
+#endif //USE_CRASHRPT
+
+       // 64 bit systems does have longer addresses!
+       long int id;
+       id=(long int)vid;
+       Beam *beam=threadbeam[id];
+
+       try
+       {
+               //additional exception handler required, otherwise RoR just 
crashes upon exception
+               while (1)
+               {
+                       // sync threads
+                       //beamLock->syncBeamThreads();
+
+                       //do work
+                       beam->threadentry(id);
+               }
+       } catch(Ogre::Exception& e)
+       {
+               // try to shutdown input system upon an error
+               if(InputEngine::singletonExists()) // this prevents the 
creating of it, if not existing
+                       INPUTENGINE.prepareShutdown();
+
+               String url = 
"http://wiki.rigsofrods.com/index.php?title=Error_"; + 
TOSTRING(e.getNumber())+"#"+e.getSource();
+               showOgreWebError("An exception has occurred!", 
e.getFullDescription(), url);
+       }
+
+       //if(beamLock) delete(beamLock);
+       pthread_exit(NULL);
+       return NULL;
+}
+void BeamFactory::_workThread()
+{
+       ttdt=tdt;
+       tdt=dt;
+       float dtperstep=dt/(Real)steps;
+
+       for (int i=0; i<steps; i++)
+       {
+               for (int t=0; t<numtrucks; t++)
+               {
+                       if(!trucks[t]) continue;
+
+                       if (trucks[t]->state!=SLEEPING && 
trucks[t]->state!=NETWORKED && trucks[t]->state!=RECYCLE)
+                               trucks[t]->calcForcesEuler(i==0, dtperstep, i, 
steps);
+               }
+               truckTruckCollisions(dtperstep);
+       }
+
+       for (int t=0; t<numtrucks; t++)
+       {
+               if(!trucks[t]) continue;
+
+               if (trucks[t]->reset_requested)
+                       trucks[t]->SyncReset();
+
+               if (trucks[t]->state!=SLEEPING && trucks[t]->state!=NETWORKED 
&& trucks[t]->state!=RECYCLE)
+               {
+                       trucks[t]->lastlastposition=trucks[t]->lastposition;
+                       trucks[t]->lastposition=trucks[t]->position;
+                       trucks[t]->updateTruckPosition();
+               }
+               if (floating_origin_enable && 
trucks[t]->nodes[0].RelPosition.length()>100.0)
+                       trucks[t]->moveOrigin(trucks[t]->nodes[0].RelPosition);
+       }
+
+       ffforce=affforce/steps;
+       ffhydro=affhydro/steps;
+       if (free_hydro) ffhydro=ffhydro/free_hydro;
+}
+#endif // 0
\ No newline at end of file

Deleted: trunk/source/main/physics/BeamWaitAndLock.h
===================================================================
--- trunk/source/main/physics/BeamWaitAndLock.h 2012-02-03 20:02:17 UTC (rev 
2440)
+++ trunk/source/main/physics/BeamWaitAndLock.h 2012-02-04 00:37:06 UTC (rev 
2441)
@@ -1,92 +0,0 @@
-/*
-This source file is part of Rigs of Rods
-Copyright 2005-2012 Pierre-Michel Ricordel
-Copyright 2007-2012 Thomas Fischer
-
-For more information, see http://www.rigsofrods.com/
-
-Rigs of Rods is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License version 3, as
-published by the Free Software Foundation.
-
-Rigs of Rods is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
-*/
-#ifndef __BeamLocker_H__
-#define __BeamLocker_H__
-
-#include "RoRPrerequisites.h"
-#include "OgrePrerequisites.h"
-
-#include "BeamFactory.h"
-
-// helper macro that logs the locking place
-
-#ifdef FEAT_DEBUG_MUTEX
-# define BEAMLOCK() BeamWaitAndLock sync(__FILE__, __FUNCTION__, __LINE__)
-#else //!FEAT_DEBUG_MUTEX
-# define BEAMLOCK()
-#endif //FEAT_DEBUG_MUTEX
-
-
-/**
- * @brief helper class that locks the vehicles array during its lifetime
- */
-class BeamWaitAndLock
-{
-public:
-       /**
-        *      constructor
-        */
-       BeamWaitAndLock()
-       {
-               BeamFactory::getSingleton()._waitForSyncAndLock();
-       }
-
-       /**
-        *      constructor with logging
-        */
-       BeamWaitAndLock(const char *fileName, const char *functionName, int 
line)
-       {
-               LOG("Beam data locking | " + String(functionName) + " | " + 
String(fileName) + ":" + TOSTRING(line));
-               BeamFactory::getSingleton()._waitForSyncAndLock();
-       }
-
-       /**
-        * destructor
-        */
-       ~BeamWaitAndLock()
-       {
-               BeamFactory::getSingleton()._ReleaseLock();
-       }
-};
-
-/**
- * @brief helper class that locks the vehicles array during its lifetime
- */
-class BeamWaitNoLock
-{
-public:
-       /**
-        *      constructor
-        */
-       BeamWaitNoLock()
-       {
-               BeamFactory::getSingleton()._waitForSyncAndLock();
-               BeamFactory::getSingleton()._ReleaseLock();
-       }
-
-       /**
-        * destructor
-        */
-       ~BeamWaitNoLock()
-       {
-       }
-};
-
-#endif //__BeamLocker_H__

Added: trunk/source/main/physics/threading/BeamThread.cpp
===================================================================
--- trunk/source/main/physics/threading/BeamThread.cpp                          
(rev 0)
+++ trunk/source/main/physics/threading/BeamThread.cpp  2012-02-04 00:37:06 UTC 
(rev 2441)
@@ -0,0 +1,31 @@
+/*
+This source file is part of Rigs of Rods
+Copyright 2005-2012 Pierre-Michel Ricordel
+Copyright 2007-2012 Thomas Fischer
+
+For more information, see http://www.rigsofrods.com/
+
+Rigs of Rods is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3, as
+published by the Free Software Foundation.
+
+Rigs of Rods is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "BeamThread.h"
+
+BeamThread::BeamThread()
+{
+       BeamWorkerManager::getSingleton().addThread(this);
+}
+
+BeamThread::~BeamThread()
+{
+       BeamWorkerManager::getSingleton().removeThread(this);
+}

Added: trunk/source/main/physics/threading/BeamThread.h
===================================================================
--- trunk/source/main/physics/threading/BeamThread.h                            
(rev 0)
+++ trunk/source/main/physics/threading/BeamThread.h    2012-02-04 00:37:06 UTC 
(rev 2441)
@@ -0,0 +1,47 @@
+/*
+This source file is part of Rigs of Rods
+Copyright 2005-2012 Pierre-Michel Ricordel
+Copyright 2007-2012 Thomas Fischer
+
+For more information, see http://www.rigsofrods.com/
+
+Rigs of Rods is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3, as
+published by the Free Software Foundation.
+
+Rigs of Rods is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef __BeamThread_H__
+#define __BeamThread_H__
+
+#include "RoRPrerequisites.h"
+#include "OgrePrerequisites.h"
+
+#include "BeamWorkerManager.h"
+#include "BeamFactory.h"
+#include "Singleton.h"
+
+// this class should be derived by every thread that wants to access the Beam 
array
+// it will automatically register with the thread manager upon initialization
+
+// IMPORTANT: before the actual access to the array, the thread MUST call 
syncBeamThreads
+// this function is able to pause the current thread to modify the array.
+class BeamThread
+{
+public:
+       BeamThread();
+       ~BeamThread();
+
+       inline void syncBeamThreads()
+       {
+               BeamWorkerManager::getSingleton().syncThreads(this);
+       }
+};
+
+#endif // __BeamThread_H__
\ No newline at end of file

Added: trunk/source/main/physics/threading/BeamWorker.cpp
===================================================================
--- trunk/source/main/physics/threading/BeamWorker.cpp                          
(rev 0)
+++ trunk/source/main/physics/threading/BeamWorker.cpp  2012-02-04 00:37:06 UTC 
(rev 2441)
@@ -0,0 +1,127 @@
+/*
+This source file is part of Rigs of Rods
+Copyright 2005-2012 Pierre-Michel Ricordel
+Copyright 2007-2012 Thomas Fischer
+
+For more information, see http://www.rigsofrods.com/
+
+Rigs of Rods is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3, as
+published by the Free Software Foundation.
+
+Rigs of Rods is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "BeamWorker.h"
+
+#include <Ogre.h>
+#include "Settings.h"
+
+#include "BeamThread.h"
+#include "errorutils.h"
+#include "InputEngine.h"
+
+using namespace Ogre;
+
+
+
+void *threadWorkerEntry(void *beamWorkerPtr)
+{
+       // call worker method in class
+       BeamWorker *worker = (BeamWorker*)beamWorkerPtr;
+       worker->_startWorkerLoop();
+       pthread_exit(NULL);
+       return NULL;    
+}
+
+BeamWorker::BeamWorker()
+{
+       pthread_create(&thread, NULL, threadWorkerEntry, (void*)this);
+}
+
+BeamWorker::~BeamWorker()
+{
+       // TODO: make sure the thread is gone!
+}
+
+void BeamWorker::_startWorkerLoop()
+{
+#ifdef USE_CRASHRPT
+       if(BSETTING("NoCrashRpt", true))
+       {
+               // add the crash handler for this thread
+               CrThreadAutoInstallHelper cr_thread_install_helper;
+               MYASSERT(cr_thread_install_helper.m_nInstallStatus==0);
+       }
+#endif //USE_CRASHRPT
+
+       try
+       {
+               // additional exception handler required, otherwise RoR just 
crashes upon exception
+               while (1)
+               {
+                       // sync threads
+                       syncBeamThreads();
+
+                       //do work
+                       _doWork();
+               }
+       } catch(Ogre::Exception& e)
+       {
+               // try to shutdown input system upon an error
+               if(InputEngine::singletonExists()) // this prevents the 
creating of it, if not existing
+                       INPUTENGINE.prepareShutdown();
+
+               String url = 
"http://wiki.rigsofrods.com/index.php?title=Error_"; + 
TOSTRING(e.getNumber())+"#"+e.getSource();
+               showOgreWebError("An exception has occurred!", 
e.getFullDescription(), url);
+       }
+}
+
+void BeamWorker::_doWork()
+{
+       LOG("BeamWorker doing work: " + TOSTRING((unsigned 
int)pthread_self().p));
+
+       Sleep(rand() * 5000);
+#if 0
+       float dtperstep=dt/(Real)steps;
+
+       for (int i=0; i<steps; i++)
+       {
+               for (int t=0; t<numtrucks; t++)
+               {
+                       if(!trucks[t]) continue;
+
+                       if (trucks[t]->state!=SLEEPING && 
trucks[t]->state!=NETWORKED && trucks[t]->state!=RECYCLE)
+                               trucks[t]->calcForcesEuler(i==0, dtperstep, i, 
steps);
+               }
+               truckTruckCollisions(dtperstep);
+       }
+
+       for (int t=0; t<numtrucks; t++)
+       {
+               if(!trucks[t]) continue;
+
+               if (trucks[t]->reset_requested)
+                       trucks[t]->SyncReset();
+
+               if (trucks[t]->state!=SLEEPING && trucks[t]->state!=NETWORKED 
&& trucks[t]->state!=RECYCLE)
+               {
+                       trucks[t]->lastlastposition=trucks[t]->lastposition;
+                       trucks[t]->lastposition=trucks[t]->position;
+                       trucks[t]->updateTruckPosition();
+               }
+               if (floating_origin_enable && 
trucks[t]->nodes[0].RelPosition.length()>100.0)
+                       trucks[t]->moveOrigin(trucks[t]->nodes[0].RelPosition);
+       }
+
+       ffforce=affforce/steps;
+       ffhydro=affhydro/steps;
+       if (free_hydro) ffhydro=ffhydro/free_hydro;
+#endif // 0
+}
\ No newline at end of file

Added: trunk/source/main/physics/threading/BeamWorker.h
===================================================================
--- trunk/source/main/physics/threading/BeamWorker.h                            
(rev 0)
+++ trunk/source/main/physics/threading/BeamWorker.h    2012-02-04 00:37:06 UTC 
(rev 2441)
@@ -0,0 +1,48 @@
+/*
+This source file is part of Rigs of Rods
+Copyright 2005-2012 Pierre-Michel Ricordel
+Copyright 2007-2012 Thomas Fischer
+
+For more information, see http://www.rigsofrods.com/
+
+Rigs of Rods is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3, as
+published by the Free Software Foundation.
+
+Rigs of Rods is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __BEAMWORKER_H_
+#define __BEAMWORKER_H_
+
+#include "RoRPrerequisites.h"
+#include "Beam.h"
+#include "BeamWorkerManager.h"
+#include "BeamThread.h"
+
+// this class wraps a thread that acts as worker thread.
+// it will request work and sync with the others
+class BeamWorker : public BeamThread
+{
+protected:
+       pthread_t thread;
+public:
+       BeamWorker();
+       ~BeamWorker();
+       // called from the new thread, do not execute manually
+       void _startWorkerLoop();
+protected:
+       float tdt;
+       float ttdt;
+
+       // method to work off one beam
+       void _doWork();
+};
+
+#endif // __BEAMWORKER_H_

Added: trunk/source/main/physics/threading/BeamWorkerManager.cpp
===================================================================
--- trunk/source/main/physics/threading/BeamWorkerManager.cpp                   
        (rev 0)
+++ trunk/source/main/physics/threading/BeamWorkerManager.cpp   2012-02-04 
00:37:06 UTC (rev 2441)
@@ -0,0 +1,126 @@
+/*
+This source file is part of Rigs of Rods
+Copyright 2005-2012 Pierre-Michel Ricordel
+Copyright 2007-2012 Thomas Fischer
+
+For more information, see http://www.rigsofrods.com/
+
+Rigs of Rods is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3, as
+published by the Free Software Foundation.
+
+Rigs of Rods is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// created by Thomas Fischer thomas{AT}thomasfischer{DOT}biz, 24th of August 
2009
+
+#include "BeamWorkerManager.h"
+#include "BeamWorker.h"
+
+#include <Ogre.h>
+#include "Settings.h"
+
+
+using namespace Ogre;
+
+void *threadWorkerManagerEntry(void* ptr)
+{
+       BeamWorkerManager *btlm = (BeamWorkerManager*)ptr;
+       btlm->_startWorkerLoop();
+       pthread_exit(NULL);
+       return NULL;    
+}
+
+BeamWorkerManager::BeamWorkerManager() :
+             threads()
+               , threadsSize(0)
+               , done_count(0)
+               , work_mutex()
+               , work_cv()
+               , done_count_mutex()
+               , done_count_cv()
+
+{
+       pthread_mutex_init(&work_mutex, NULL);
+       pthread_cond_init(&work_cv, NULL);
+       pthread_mutex_init(&done_count_mutex, NULL);
+       pthread_cond_init(&done_count_cv, NULL);
+       // start worker thread:
+       pthread_create(&workerThread, NULL, threadWorkerManagerEntry, 
(void*)this);
+}
+
+
+BeamWorkerManager::~BeamWorkerManager()
+{
+}
+
+void BeamWorkerManager::_startWorkerLoop()
+{
+       while (1)
+       {
+               MUTEX_LOCK(&done_count_mutex);
+               while (done_count < threadsSize)
+               {
+                       LOG("worker loop continued, waiting for more threads | 
threads waiting: " + TOSTRING(done_count) + " / " + TOSTRING(threadsSize));
+                       pthread_cond_wait(&done_count_cv, &done_count_mutex);
+               }
+               MUTEX_UNLOCK(&done_count_mutex);
+               LOG("worker loop lock aquired, all threads sleeping...");
+
+               // we got through, all threads should be stopped by now
+
+               // TODO: make modifications on truck array in here
+               Sleep(5000);
+
+               // reset counter, continue all threads via signal
+               LOG("worker loop: unpausing all threads");
+               MUTEX_LOCK(&done_count_mutex);
+               done_count=0;
+               // send signals to the threads
+               pthread_cond_signal(&work_cv);
+               MUTEX_UNLOCK(&done_count_mutex);
+       }
+}
+
+void BeamWorkerManager::addThread(BeamThread *bthread)
+{
+       threads.push_back(bthread);
+       threadsSize=threads.size();
+}
+
+void BeamWorkerManager::removeThread(BeamThread *bthread)
+{
+       for(std::vector<BeamThread*>::iterator it = threads.begin(); it != 
threads.end(); ++it)
+       {
+               if(*it == bthread)
+               {
+                       threads.erase(it);
+                       threadsSize=threads.size();
+                       return;
+               }
+       }
+}
+
+void BeamWorkerManager::syncThreads(BeamThread *bthread)
+{
+       LOG("Thread visiting syncing point| class: " + TOSTRING((unsigned 
long)bthread) + " / thread: " + TOSTRING((unsigned long)pthread_self().p));
+       
+       MUTEX_LOCK(&work_mutex);
+
+       // count thread counter up:
+       MUTEX_LOCK(&done_count_mutex);
+       done_count++;
+       pthread_cond_signal(&done_count_cv);
+       MUTEX_UNLOCK(&done_count_mutex);
+
+       // then wait for the signal that we can continue
+       pthread_cond_wait(&work_cv, &work_mutex);
+       MUTEX_UNLOCK(&work_mutex);
+       // return to continue to do work
+}

Added: trunk/source/main/physics/threading/BeamWorkerManager.h
===================================================================
--- trunk/source/main/physics/threading/BeamWorkerManager.h                     
        (rev 0)
+++ trunk/source/main/physics/threading/BeamWorkerManager.h     2012-02-04 
00:37:06 UTC (rev 2441)
@@ -0,0 +1,57 @@
+/*
+This source file is part of Rigs of Rods
+Copyright 2005-2012 Pierre-Michel Ricordel
+Copyright 2007-2012 Thomas Fischer
+
+For more information, see http://www.rigsofrods.com/
+
+Rigs of Rods is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3, as
+published by the Free Software Foundation.
+
+Rigs of Rods is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Rigs of Rods.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef __BeamWorkerManager_H__
+#define __BeamWorkerManager_H__
+
+#include "RoRPrerequisites.h"
+#include "OgrePrerequisites.h"
+
+#include "BeamFactory.h"
+#include "Singleton.h"
+
+class BeamThread;
+
+// Manager that manages all threads and the beam locking
+class BeamWorkerManager : public RoRSingleton<BeamWorkerManager>
+{
+       friend class BeamThread;
+       friend class RoRSingleton<BeamWorkerManager>;
+protected:
+       std::vector<BeamThread*> threads;
+       int threadsSize;
+       int done_count;
+       pthread_mutex_t work_mutex;
+       pthread_cond_t work_cv;
+       pthread_mutex_t done_count_mutex;
+       pthread_cond_t done_count_cv;
+       pthread_t workerThread;
+
+       BeamWorkerManager();
+       ~BeamWorkerManager();
+
+       void addThread(BeamThread *bthread);
+       void removeThread(BeamThread *bthread);
+       void syncThreads(BeamThread *bthread);
+
+public:
+       void _startWorkerLoop();
+};
+
+#endif //__BeamWorkerManager_H__

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


------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Rigsofrods-devel mailing list
Rigsofrods-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel

Reply via email to