[1230] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef _TclThreadManager_H__ |
---|
| 30 | #define _TclThreadManager_H__ |
---|
| 31 | |
---|
| 32 | #include <queue> |
---|
| 33 | |
---|
| 34 | #include <boost/thread/mutex.hpp> |
---|
| 35 | #include <boost/thread/condition.hpp> |
---|
| 36 | |
---|
| 37 | #include "CorePrereqs.h" |
---|
| 38 | #include "Tickable.h" |
---|
| 39 | #include "cpptcl/CppTcl.h" |
---|
| 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
| 43 | class boost::thread; |
---|
| 44 | |
---|
| 45 | struct TclThread |
---|
| 46 | { |
---|
| 47 | Tcl::interpreter* interpreter_; |
---|
| 48 | boost::thread* thread_; |
---|
| 49 | boost::try_mutex* mutex_; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | class _CoreExport TclThreadManager : public Tickable |
---|
| 53 | { |
---|
| 54 | public: |
---|
| 55 | static TclThreadManager& getInstance(); |
---|
| 56 | |
---|
| 57 | static void tclthread(unsigned int threadID, const std::string& command); |
---|
| 58 | static unsigned int create(); |
---|
| 59 | static void destroy(unsigned int threadID); |
---|
| 60 | static void execute(unsigned int threadID, const std::string& command); |
---|
| 61 | static std::string query(unsigned int threadID, const std::string& command); |
---|
| 62 | |
---|
| 63 | static void tcl_execute(Tcl::object const &args); |
---|
| 64 | static std::string tcl_orxonox(Tcl::object const &args); |
---|
| 65 | |
---|
| 66 | Tcl::interpreter* createTclInterpreter(unsigned int threadID) const; |
---|
| 67 | bool createTclThread(); |
---|
| 68 | bool destroyTclThread(unsigned int threadID); |
---|
| 69 | |
---|
| 70 | void pushCommandBack(const std::string& command); |
---|
| 71 | std::string popCommandFront(); |
---|
| 72 | bool isEmpty(); |
---|
| 73 | |
---|
| 74 | void pushCommandBack(unsigned int threadID, const std::string& command); |
---|
| 75 | std::string popCommandFront(unsigned int threadID); |
---|
| 76 | bool isEmpty(unsigned int threadID); |
---|
| 77 | |
---|
| 78 | std::string eval(const std::string& command); |
---|
| 79 | std::string eval(unsigned int threadID, const std::string& command); |
---|
| 80 | |
---|
| 81 | virtual void tick(float dt); |
---|
| 82 | |
---|
| 83 | private: |
---|
| 84 | TclThreadManager(); |
---|
| 85 | |
---|
| 86 | bool isReady_; |
---|
| 87 | |
---|
| 88 | unsigned int IDcount_; |
---|
| 89 | std::map<unsigned int, TclThread> threads_; |
---|
| 90 | std::map<unsigned int, std::queue<std::string> > threadQueues_; |
---|
| 91 | std::queue<std::string> orxonoxQueue_; |
---|
| 92 | |
---|
| 93 | boost::mutex orxonoxQueueMutex_; |
---|
| 94 | boost::mutex threadQueuesMutex_; |
---|
| 95 | boost::mutex threadsMutex_; |
---|
| 96 | boost::mutex orxonoxEvalMutex_; |
---|
| 97 | |
---|
| 98 | boost::condition orxonoxQueueCondition_; |
---|
| 99 | boost::condition orxonoxEvalCondition_; |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | _CoreExport void tclThreadLoop(Tcl::interpreter* interpreter, boost::try_mutex* mutex, unsigned int threadID); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | #endif /* _TclThreadManager_H__ */ |
---|