| 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 | #include <map> |
|---|
| 34 | #include <list> |
|---|
| 35 | |
|---|
| 36 | #include <boost/thread/mutex.hpp> |
|---|
| 37 | #include <boost/thread/condition.hpp> |
|---|
| 38 | #include <boost/thread/thread.hpp> |
|---|
| 39 | |
|---|
| 40 | #include "CorePrereqs.h" |
|---|
| 41 | #include "Tickable.h" |
|---|
| 42 | #include "cpptcl/CppTcl.h" |
|---|
| 43 | |
|---|
| 44 | namespace orxonox |
|---|
| 45 | { |
|---|
| 46 | class boost::thread; |
|---|
| 47 | |
|---|
| 48 | struct _CoreExport TclInterpreterBundle |
|---|
| 49 | { |
|---|
| 50 | unsigned int id_; |
|---|
| 51 | |
|---|
| 52 | std::list<std::string> queue_; |
|---|
| 53 | boost::mutex queueMutex_; |
|---|
| 54 | |
|---|
| 55 | Tcl::interpreter* interpreter_; |
|---|
| 56 | std::string interpreterName_; |
|---|
| 57 | boost::try_mutex interpreterMutex_; |
|---|
| 58 | |
|---|
| 59 | std::list<unsigned int> queriers_; |
|---|
| 60 | boost::mutex queriersMutex_; |
|---|
| 61 | |
|---|
| 62 | bool running_; |
|---|
| 63 | boost::mutex runningMutex_; |
|---|
| 64 | |
|---|
| 65 | bool finished_; |
|---|
| 66 | boost::mutex finishedMutex_; |
|---|
| 67 | boost::condition finishedCondition_; |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | class _CoreExport TclThreadManager : public Tickable |
|---|
| 71 | { |
|---|
| 72 | public: |
|---|
| 73 | static TclThreadManager& getInstance(); |
|---|
| 74 | |
|---|
| 75 | static unsigned int create(); |
|---|
| 76 | static unsigned int createID(unsigned int threadID); |
|---|
| 77 | static void destroy(unsigned int threadID); |
|---|
| 78 | static void execute(unsigned int threadID, const std::string& command); |
|---|
| 79 | static std::string query(unsigned int threadID, const std::string& command); |
|---|
| 80 | static void status(); |
|---|
| 81 | static void dump(unsigned int threadID); |
|---|
| 82 | static void flush(unsigned int threadID); |
|---|
| 83 | |
|---|
| 84 | static void tcl_execute(Tcl::object const &args); |
|---|
| 85 | static std::string tcl_query(int querierID, Tcl::object const &args); |
|---|
| 86 | static std::string tcl_crossquery(int querierID, int threadID, Tcl::object const &args); |
|---|
| 87 | static bool tcl_running(int threadID); |
|---|
| 88 | |
|---|
| 89 | Tcl::interpreter* createNewTclInterpreter(const std::string& threadID); |
|---|
| 90 | TclInterpreterBundle* getInterpreterBundle(unsigned int threadID); |
|---|
| 91 | std::string dumpList(const std::list<unsigned int>& list); |
|---|
| 92 | void error(const std::string& error); |
|---|
| 93 | void debug(const std::string& error); |
|---|
| 94 | |
|---|
| 95 | void pushCommandToQueue(const std::string& command); |
|---|
| 96 | void forceCommandToFrontOfQueue(const std::string& command); |
|---|
| 97 | std::string popCommandFromQueue(); |
|---|
| 98 | bool queueIsEmpty(); |
|---|
| 99 | |
|---|
| 100 | void pushCommandToQueue(unsigned int threadID, const std::string& command); |
|---|
| 101 | std::string popCommandFromQueue(unsigned int threadID); |
|---|
| 102 | bool queueIsEmpty(unsigned int threadID); |
|---|
| 103 | |
|---|
| 104 | bool updateQueriersList(TclInterpreterBundle* querier, TclInterpreterBundle* target); |
|---|
| 105 | |
|---|
| 106 | std::string evalQuery(unsigned int querierID, const std::string& command); |
|---|
| 107 | std::string evalQuery(unsigned int querierID, unsigned int threadID, const std::string& command); |
|---|
| 108 | |
|---|
| 109 | virtual void tick(float dt); |
|---|
| 110 | |
|---|
| 111 | private: |
|---|
| 112 | TclThreadManager(); |
|---|
| 113 | TclThreadManager(const TclThreadManager& other); |
|---|
| 114 | ~TclThreadManager() {} |
|---|
| 115 | |
|---|
| 116 | unsigned int threadCounter_; |
|---|
| 117 | TclInterpreterBundle orxonoxInterpreterBundle_; |
|---|
| 118 | std::map<unsigned int, TclInterpreterBundle*> interpreterBundles_; |
|---|
| 119 | boost::mutex bundlesMutex_; |
|---|
| 120 | boost::condition fullQueueCondition_; |
|---|
| 121 | boost::condition orxonoxEvalCondition_; |
|---|
| 122 | #if (BOOST_VERSION >= 103500) |
|---|
| 123 | boost::thread::id threadID_; |
|---|
| 124 | #else |
|---|
| 125 | boost::thread threadID_; |
|---|
| 126 | #endif |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | _CoreExport void tclThread(TclInterpreterBundle* interpreterBundle, std::string command); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | #endif /* _TclThreadManager_H__ */ |
|---|