xoreos  0.0.5
requests.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/error.h"
26 #include "src/common/util.h"
27 #include "src/common/threads.h"
28 
29 #include "src/events/requests.h"
30 #include "src/events/events.h"
31 
33 
35 
36 namespace Events {
37 
39  clearList();
40 }
41 
43  clearList();
44 
45  if (!createThread("RequestManager"))
46  throw Common::Exception("Failed to create requests thread: %s", SDL_GetError());
47 }
48 
50  destroyThread();
51 
52  clearList();
53 }
54 
57 
58  if ((*request)->_dispatched)
59  // We are already waiting for an answer
60  return;
61 
62  // Set state
63  (*request)->_dispatched = true;
64 
65  // And send the event
66  if (!EventMan.pushEvent((*request)->_event))
67  throw Common::Exception("Failed dispatching request");
68 
70  // If we're currently in the main thread, to avoid a dead-lock, process events now
71  EventMan.processEvents();
72 }
73 
75  // Locking our use mutex, to prevent race conditions
76  _mutexUse.lock();
77 
78  if (!(*request)->_dispatched) {
79  // The request wasn't yet dispatched
80 
81  _mutexUse.unlock();
82  return;
83  }
84 
85  // We don't need our use mutex now
86  _mutexUse.unlock();
87 
88  // Wait for a reply
89  (*request)->_hasReply.lock();
90 
91  // Got a reply
92 
93  // Now we need the use mutex again
94  _mutexUse.lock();
95 
96  // Copy the reply (if any) to the reply memory
97  (*request)->copyToReply();
98 
99  // And mark the request as garbage, so that it may be collected
100  (*request)->setGarbage();
101 
102  // And finally give the use mutex free again
103  _mutexUse.unlock();
104 }
105 
108 
109  (*request)->setGarbage();
110 }
111 
113  dispatch(request);
114  waitReply(request);
115 }
116 
118  dispatch(request);
119  forget(request);
120 }
121 
124 
125  dispatchAndWait(syncID);
126 }
127 
130 
131  (*rID)->_glContainer.glContainer = &glContainer;
132 
133  return rID;
134 }
135 
138 
139  (*rID)->_glContainer.glContainer = &glContainer;
140 
141  return rID;
142 }
143 
146 
147  _requests.push_back(new Request(type));
148 
149  return --_requests.end();
150 }
151 
154 
155  (*rID)->_callInMainThread.caller = &caller;
156 
157  dispatchAndWait(rID);
158 }
159 
162 
163  _requests.clear();
164 }
165 
166 static bool requestIsGarbage(const Request * const request) {
167  return !request || request->isGarbage();
168 }
169 
172 
174 }
175 
177  while (!_killThread.load(boost::memory_order_relaxed)) {
178  collectGarbage();
179  EventMan.delay(100);
180  }
181 }
182 
185 }
186 
187 } // End of namespace Events
Generic image decoder interface.
Inter-thread request events.
Request the rebuilding of a GL container.
Definition: types.h:73
T callInMainThread(const MainThreadFunctor< T > &f)
Call this function in the main thread.
Definition: requests.h:88
The request manager, handling all requests.
Definition: requests.h:65
bool destroyThread()
Definition: thread.cpp:64
bool isMainThread()
Returns true if called from the main thread, false otherwise.
Definition: threads.cpp:54
boost::atomic< bool > _killThread
Definition: thread.h:53
void dispatchAndWait(RequestID request)
Dispatch a request and wait for the answer.
Definition: requests.cpp:112
static bool requestIsGarbage(const Request *const request)
Definition: requests.cpp:166
Exception that provides a stack of explanations.
Definition: error.h:36
Common::Mutex _mutexUse
The mutex locking the use of the manager.
Definition: requests.h:108
Basic exceptions to throw.
Threading system helpers.
bool createThread(const UString &name="")
Definition: thread.cpp:44
Utility templates and functions.
bool isGarbage() const
void remove_if(Predicate pred)
Definition: ptrlist.h:104
RequestID rebuild(Graphics::GLContainer &glContainer)
Request that a GL container shall be rebuilt.
Definition: requests.cpp:128
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
The global events manager.
ITCEvent
Specific type of the inter-thread communication.
Definition: types.h:70
StackException Exception
Definition: error.h:59
Request the destruction of a GL container.
Definition: types.h:74
RequestList::iterator RequestID
Definition: requests.h:48
static void destroy()
Definition: requests.cpp:183
static void destroy()
Definition: singleton.h:104
boost::function< void()> MainThreadCallerFunctor
Definition: types.h:346
Convenience class that locks a mutex on creation and unlocks it on destruction.
Definition: mutex.h:71
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
void dispatch(RequestID request)
Dispatch a request.
Definition: requests.cpp:55
RequestList _requests
All currently active requests.
Definition: requests.h:110
void sync()
Request a sync, letting all prior requests finish.
Definition: requests.cpp:122
RequestID newRequest(ITCEvent type)
Create a new, empty request of that type.
Definition: requests.cpp:144
void clear()
Definition: ptrlist.h:47
void lock()
Definition: mutex.cpp:41
A request, carrying inter-thread communication.
Definition: requesttypes.h:52
A container of OpenGL elements.
Definition: glcontainer.h:35
void forget(RequestID request)
Ignore any answer we get.
Definition: requests.cpp:106
void unlock()
Definition: mutex.cpp:47
void dispatchAndForget(RequestID request)
Dispatch a request and ignore the answer.
Definition: requests.cpp:117
void waitReply(RequestID request)
Wait for a request to be answered.
Definition: requests.cpp:74
Request to call a function in the main thread.
Definition: types.h:72
Request a sync, letting all prior requests finish.
Definition: types.h:71