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