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 | unsigned int threadID_; |
---|
48 | Tcl::interpreter* interpreter_; |
---|
49 | boost::thread* thread_; |
---|
50 | boost::mutex* evalMutex_; |
---|
51 | boost::mutex* stateMutex_; |
---|
52 | enum State |
---|
53 | { |
---|
54 | Ready, |
---|
55 | Busy, |
---|
56 | Finished, |
---|
57 | Error |
---|
58 | }* state_; |
---|
59 | }; |
---|
60 | |
---|
61 | class _CoreExport TclThreadManager : public Tickable |
---|
62 | { |
---|
63 | public: |
---|
64 | static TclThreadManager& getInstance(); |
---|
65 | |
---|
66 | static void tclthread(unsigned int threadID, const std::string& command); |
---|
67 | static unsigned int create(); |
---|
68 | static void destroy(unsigned int threadID); |
---|
69 | static void execute(unsigned int threadID, const std::string& command); |
---|
70 | static std::string query(unsigned int threadID, const std::string& command); |
---|
71 | static void status(); |
---|
72 | static void dump(unsigned int threadID); |
---|
73 | |
---|
74 | static std::string tcl_query(int id, Tcl::object const &args); |
---|
75 | static void tcl_execute(Tcl::object const &args); |
---|
76 | |
---|
77 | Tcl::interpreter* createTclInterpreter(unsigned int threadID) const; |
---|
78 | bool createTclThread(); |
---|
79 | bool destroyTclThread(unsigned int threadID); |
---|
80 | |
---|
81 | void setState(TclThread* tclThread, TclThread::State state); |
---|
82 | TclThread::State getState(TclThread* tclThread); |
---|
83 | |
---|
84 | void pushCommandBack(const std::string& command); |
---|
85 | std::string popCommandFront(); |
---|
86 | bool isEmpty(); |
---|
87 | |
---|
88 | void pushCommandBack(unsigned int threadID, const std::string& command); |
---|
89 | std::string popCommandFront(unsigned int threadID); |
---|
90 | bool isEmpty(unsigned int threadID); |
---|
91 | |
---|
92 | std::string eval(const std::string& command); |
---|
93 | std::string eval(unsigned int threadID, const std::string& command); |
---|
94 | |
---|
95 | virtual void tick(float dt); |
---|
96 | |
---|
97 | private: |
---|
98 | TclThreadManager(); |
---|
99 | |
---|
100 | bool isReady_; |
---|
101 | bool isQuerying_; |
---|
102 | unsigned int queryID_; |
---|
103 | |
---|
104 | unsigned int IDcount_; |
---|
105 | std::map<unsigned int, TclThread*> threads_; |
---|
106 | std::map<unsigned int, std::pair<std::queue<std::string>, boost::condition*> > threadQueues_; |
---|
107 | std::queue<std::string> orxonoxQueue_; |
---|
108 | |
---|
109 | boost::try_mutex orxonoxQueueMutex_; |
---|
110 | boost::try_mutex threadQueuesMutex_; |
---|
111 | boost::try_mutex threadsMutex_; |
---|
112 | boost::try_mutex orxonoxStateMutex_; |
---|
113 | boost::try_mutex orxonoxQueryMutex_; |
---|
114 | |
---|
115 | boost::condition orxonoxQueueCondition_; |
---|
116 | boost::condition orxonoxEvalCondition_; |
---|
117 | }; |
---|
118 | |
---|
119 | _CoreExport void tclThreadLoop(TclThread* tclThread); |
---|
120 | } |
---|
121 | |
---|
122 | #endif /* _TclThreadManager_H__ */ |
---|