Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F126681
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/events.cpp b/util/events.cpp
index e837644a..64e797c7 100644
--- a/util/events.cpp
+++ b/util/events.cpp
@@ -1,61 +1,61 @@
#ifdef USE_SDL
#include <SDL.h>
#endif
#include "events.h"
#include "exceptions/shutdown_exception.h"
-#include "thread.h"
#include "funcs.h"
+#include "thread.h"
namespace Util{
EventManager::EventManager(){
}
#ifdef USE_SDL
void EventManager::runSDL(){
SDL_Event event;
while (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(WaitThread & 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/message-queue.cpp b/util/message-queue.cpp
index 8f32624c..0a276ee8 100644
--- a/util/message-queue.cpp
+++ b/util/message-queue.cpp
@@ -1,36 +1,36 @@
#include <queue>
-#include <pthread.h>
#include <string>
#include "message-queue.h"
+#include "thread.h"
MessageQueue::MessageQueue(){
- pthread_mutex_init(&lock, NULL);
+ Util::Thread::initializeLock(&lock);
}
void MessageQueue::add(const std::string & str){
- pthread_mutex_lock(&lock);
+ Util::Thread::acquireLock(&lock);
messages.push(str);
- pthread_mutex_unlock(&lock);
+ Util::Thread::releaseLock(&lock);
}
bool MessageQueue::hasAny(){
bool any = false;
- pthread_mutex_lock(&lock);
+ Util::Thread::acquireLock(&lock);
any = messages.size() > 0;
- pthread_mutex_unlock(&lock);
+ Util::Thread::releaseLock(&lock);
return any;
}
std::string MessageQueue::get(){
std::string str;
- pthread_mutex_lock(&lock);
+ Util::Thread::acquireLock(&lock);
if (messages.size() > 0){
str = messages.front();
messages.pop();
}
- pthread_mutex_unlock(&lock);
+ Util::Thread::releaseLock(&lock);
return str;
}
MessageQueue::~MessageQueue(){
}
diff --git a/util/message-queue.h b/util/message-queue.h
index 80042b21..97de054d 100644
--- a/util/message-queue.h
+++ b/util/message-queue.h
@@ -1,29 +1,29 @@
#ifndef _paintown_message_queue_h
#define _paintown_message_queue_h
#include <queue>
-#include <pthread.h>
#include <string>
+#include "thread.h"
/* multithreaded message queue.
* someone puts messages in, someone takes them out.
* FIFO order
*/
class MessageQueue{
public:
MessageQueue();
/* push on */
void add(const std::string & str);
/* true if any messages are left */
bool hasAny();
/* get the first message */
std::string get();
~MessageQueue();
private:
std::queue<std::string> messages;
- pthread_mutex_t lock;
+ Util::Thread::Lock lock;
};
#endif
diff --git a/util/thread.cpp b/util/thread.cpp
index f0b7fa55..91bde94a 100644
--- a/util/thread.cpp
+++ b/util/thread.cpp
@@ -1,106 +1,105 @@
-#include <pthread.h>
+// #include <pthread.h>
#include "thread.h"
namespace Util{
-
namespace Thread{
void initializeLock(Lock * lock){
pthread_mutex_init(lock, NULL);
}
void acquireLock(Lock * lock){
pthread_mutex_lock(lock);
}
void releaseLock(Lock * lock){
pthread_mutex_unlock(lock);
}
void createThread(Id * thread, void * attributes, ThreadFunction function, void * arg){
pthread_create(thread, (pthread_attr_t*) attributes, function, arg);
}
void joinThread(Id thread){
pthread_join(thread, NULL);
}
void cancelThread(Id thread){
/* FIXME: cancel is not implemented for libogc, find another way.
* thread suspend/resume is there, though.
*/
#if !defined(WII)
pthread_cancel(thread);
#endif
}
}
WaitThread::WaitThread():
done(false){
Thread::initializeLock(&doneLock);
}
WaitThread::WaitThread(void * (*thread)(void*), void * arg){
Thread::initializeLock(&doneLock);
start(thread, arg);
}
static void * do_thread(void * arg){
WaitThread * thread = (WaitThread *) arg;
thread->doRun();
return NULL;
}
void WaitThread::doRun(){
this->function(this->arg);
Thread::acquireLock(&doneLock);
this->done = true;
Thread::releaseLock(&doneLock);
}
void WaitThread::start(Thread::ThreadFunction thread, void * arg){
done = false;
this->arg = arg;
this->function = thread;
Thread::createThread(&this->thread, NULL, do_thread, this);
}
bool WaitThread::isRunning(){
Thread::acquireLock(&doneLock);
bool what = done;
Thread::releaseLock(&doneLock);
return what;
}
void WaitThread::kill(){
Thread::cancelThread(thread);
Thread::joinThread(thread);
}
WaitThread::~WaitThread(){
/* FIXME: Should we join the thread? */
/* pthread_join(thread); */
}
ThreadBoolean::ThreadBoolean(volatile bool & what, Thread::Lock & lock):
what(what),
lock(lock){
}
bool ThreadBoolean::get(){
Thread::acquireLock(&lock);
bool b = what;
Thread::releaseLock(&lock);
return b;
}
void ThreadBoolean::set(bool value){
Thread::acquireLock(&lock);
what = value;
Thread::releaseLock(&lock);
}
}
diff --git a/util/thread.h b/util/thread.h
index a2007a44..74a28a7a 100644
--- a/util/thread.h
+++ b/util/thread.h
@@ -1,65 +1,67 @@
#ifndef _paintown_thread_h
#define _paintown_thread_h
#include <pthread.h>
namespace Util{
/* Either uses pthreads or SDL_thread */
namespace Thread{
typedef pthread_mutex_t Lock;
typedef pthread_t Id;
typedef void * (*ThreadFunction)(void*);
void initializeLock(Lock * lock);
+
void acquireLock(Lock * lock);
+
void releaseLock(Lock * lock);
void createThread(Id * thread, void * attributes, ThreadFunction function, void * arg);
void joinThread(Id thread);
void cancelThread(Id thread);
}
class WaitThread{
public:
/* does not start a new thread yet */
WaitThread();
/* starts a thread */
WaitThread(Thread::ThreadFunction thread, void * arg);
/* starts a thread */
void start(Thread::ThreadFunction thread, void * arg);
bool isRunning();
void kill();
virtual ~WaitThread();
public:
/* actually runs the thread */
void doRun();
protected:
Thread::Lock doneLock;
Thread::Id thread;
volatile bool done;
void * arg;
Thread::ThreadFunction function;
};
/* wraps a boolean with lock/unlock while checking/setting it */
class ThreadBoolean{
public:
ThreadBoolean(volatile bool & what, Thread::Lock & lock);
bool get();
void set(bool value);
protected:
volatile bool & what;
Thread::Lock & lock;
};
}
#endif
diff --git a/util/timer.cpp b/util/timer.cpp
index 9610120f..a258bd08 100644
--- a/util/timer.cpp
+++ b/util/timer.cpp
@@ -1,41 +1,43 @@
+#include <time.h>
#include "timer.h"
#include "init.h"
#include "funcs.h"
+#include "thread.h"
namespace Util{
void * do_wait(void * timer_){
Timer * timer = (Timer *) timer_;
unsigned int now = Global::second_counter;
/* add 1 because the current time is in between X and X+1 */
while (Global::second_counter - now < timer->wait+1){
Util::rest(50);
bool do_stop = false;
Thread::acquireLock(&timer->lock);
do_stop = timer->stopped;
Thread::releaseLock(&timer->lock);
if (do_stop){
return NULL;
}
}
timer->func(timer->arg);
return NULL;
}
Timer::Timer(unsigned int seconds_to_wait, timeout func, void * arg):
wait(seconds_to_wait),
func(func),
arg(arg),
stopped(false){
Thread::initializeLock(&lock);
Thread::createThread(&timer, NULL, do_wait, this);
}
void Timer::stop(){
Thread::acquireLock(&lock);
stopped = true;
Thread::releaseLock(&lock);
Thread::joinThread(timer);
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 12:31 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69045
Default Alt Text
(8 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline