Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F126731
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/events.cpp b/util/events.cpp
index c9968a17..575c2d5d 100644
--- a/util/events.cpp
+++ b/util/events.cpp
@@ -1,46 +1,61 @@
#ifdef USE_SDL
#include <SDL.h>
#endif
#include "events.h"
#include "shutdown_exception.h"
+#include "thread.h"
+#include "funcs.h"
namespace Util{
EventManager::EventManager(){
}
#ifdef USE_SDL
void EventManager::runSDL(){
SDL_Event event;
if (SDL_PollEvent(&event) == 1){
switch (event.type){
case SDL_QUIT : {
dispatch(CloseWindow);
break;
}
default : {
break;
}
}
}
}
#endif
void EventManager::run(){
#ifdef USE_SDL
runSDL();
#endif
}
+/* kill the program if the user requests */
+void EventManager::waitForThread(Thread & thread){
+ while (!thread.isRunning()){
+ try{
+ run();
+ } catch (const ShutdownException & death){
+ thread.kill();
+ throw death;
+ }
+ Util::rest(10);
+ }
+}
+
EventManager::~EventManager(){
}
void EventManager::dispatch(Event type){
switch (type){
case CloseWindow : {
throw ShutdownException();
}
}
}
}
diff --git a/util/events.h b/util/events.h
index 9855f6f9..3b19da37 100644
--- a/util/events.h
+++ b/util/events.h
@@ -1,31 +1,34 @@
#ifndef _paintown_events_h
#define _paintown_events_h
/* handles global events from the system such as
* window manager events (press X button)
* keyboard/mouse/joystick input (for some backends like SDL)
*/
namespace Util{
+class Thread;
+
class EventManager{
public:
EventManager();
virtual void run();
+ virtual void waitForThread(Thread & thread);
virtual ~EventManager();
private:
enum Event{
CloseWindow
};
virtual void dispatch(Event type);
#ifdef USE_SDL
virtual void runSDL();
#endif
};
}
#endif
diff --git a/util/thread.cpp b/util/thread.cpp
index 15e6ed26..aacafcce 100644
--- a/util/thread.cpp
+++ b/util/thread.cpp
@@ -1,32 +1,53 @@
#include <pthread.h>
#include "thread.h"
namespace Util{
Thread::Thread():
done(false){
pthread_mutex_init(&doneLock, NULL);
}
+Thread::Thread(void * (*thread)(void*), void * arg){
+ pthread_mutex_init(&doneLock, NULL);
+ start(thread, arg);
+}
+
+static void * do_thread(void * arg){
+ Thread * thread = (Thread *) arg;
+ thread->doRun();
+}
+
+void Thread::doRun(){
+ this->function(this->arg);
+
+ pthread_mutex_lock(&doneLock);
+ this->done = true;
+ pthread_mutex_unlock(&doneLock);
+}
+
void Thread::start(void * (*thread)(void *), void * arg){
done = false;
- pthread_create(&this->thread, NULL, thread, arg);
+ this->arg = arg;
+ this->function = thread;
+ pthread_create(&this->thread, NULL, do_thread, this);
}
bool Thread::isRunning(){
pthread_mutex_lock(&doneLock);
bool what = done;
pthread_mutex_unlock(&doneLock);
return what;
}
void Thread::kill(){
pthread_cancel(thread);
+ pthread_join(thread, NULL);
}
Thread::~Thread(){
/* FIXME: Should we join the thread? */
/* pthread_join(thread); */
}
}
diff --git a/util/thread.h b/util/thread.h
index f9702211..98627f53 100644
--- a/util/thread.h
+++ b/util/thread.h
@@ -1,29 +1,38 @@
#ifndef _paintown_thread_h
#define _paintown_thread_h
#include <pthread.h>
namespace Util{
class Thread{
public:
/* does not start a new thread yet */
Thread();
+ /* starts a thread */
+ Thread(void * (*thread)(void*), void * arg);
+
/* starts a thread */
void start(void * (*thread)(void *), void * arg);
bool isRunning();
void kill();
virtual ~Thread();
+public:
+ /* actually runs the thread */
+ void doRun();
+
protected:
pthread_mutex_t doneLock;
pthread_t thread;
volatile bool done;
+ void * arg;
+ void * (*function)(void *);
};
}
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 12:45 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69094
Default Alt Text
(3 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline