Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
diff --git a/util/thread.cpp b/util/thread.cpp
index 02facd4e..5ab05a75 100644
--- a/util/thread.cpp
+++ b/util/thread.cpp
@@ -1,77 +1,106 @@
#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(ThreadId * thread, void * attributes, ThreadFunction function, void * arg){
+ pthread_create(thread, (pthread_attr_t*) attributes, function, arg);
+}
+
+void joinThread(ThreadId thread){
+ pthread_join(thread, NULL);
+}
+
+void cancelThread(ThreadId 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){
- pthread_mutex_init(&doneLock, NULL);
+ Thread::initializeLock(&doneLock);
}
WaitThread::WaitThread(void * (*thread)(void*), void * arg){
- pthread_mutex_init(&doneLock, NULL);
+ 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);
- pthread_mutex_lock(&doneLock);
+ Thread::acquireLock(&doneLock);
this->done = true;
- pthread_mutex_unlock(&doneLock);
+ Thread::releaseLock(&doneLock);
}
-void WaitThread::start(void * (*thread)(void *), void * arg){
+void WaitThread::start(Thread::ThreadFunction thread, void * arg){
done = false;
this->arg = arg;
this->function = thread;
- pthread_create(&this->thread, NULL, do_thread, this);
+ Thread::createThread(&this->thread, NULL, do_thread, this);
}
bool WaitThread::isRunning(){
- pthread_mutex_lock(&doneLock);
+ Thread::acquireLock(&doneLock);
bool what = done;
- pthread_mutex_unlock(&doneLock);
+ Thread::releaseLock(&doneLock);
return what;
}
void WaitThread::kill(){
- /* FIXME: cancel is not implemented for libogc, find another way.
- * thread suspend/resume is there, though.
- */
-#if !defined(WII)
- pthread_cancel(thread);
-#endif
- pthread_join(thread, NULL);
+ Thread::cancelThread(thread);
+ Thread::joinThread(thread);
}
WaitThread::~WaitThread(){
/* FIXME: Should we join the thread? */
/* pthread_join(thread); */
}
-ThreadBoolean::ThreadBoolean(volatile bool & what, pthread_mutex_t & lock):
+ThreadBoolean::ThreadBoolean(volatile bool & what, Thread::Lock & lock):
what(what),
lock(lock){
}
bool ThreadBoolean::get(){
- pthread_mutex_lock(&lock);
+ Thread::acquireLock(&lock);
bool b = what;
- pthread_mutex_unlock(&lock);
+ Thread::releaseLock(&lock);
return b;
}
void ThreadBoolean::set(bool value){
- pthread_mutex_lock(&lock);
+ Thread::acquireLock(&lock);
what = value;
- pthread_mutex_unlock(&lock);
+ Thread::releaseLock(&lock);
}
}
diff --git a/util/thread.h b/util/thread.h
index 0220a52e..ec6ed299 100644
--- a/util/thread.h
+++ b/util/thread.h
@@ -1,51 +1,64 @@
#ifndef _paintown_thread_h
#define _paintown_thread_h
#include <pthread.h>
namespace Util{
+namespace Thread{
+ typedef pthread_mutex_t Lock;
+ typedef pthread_t ThreadId;
+ typedef void * (*ThreadFunction)(void*);
+
+ void initializeLock(Lock * lock);
+ void acquireLock(Lock * lock);
+ void releaseLock(Lock * lock);
+ void createThread(ThreadId * thread, void * attributes, ThreadFunction function, void * arg);
+ void joinThread(ThreadId thread);
+ void cancelThread(ThreadId thread);
+}
+
class WaitThread{
public:
/* does not start a new thread yet */
WaitThread();
/* starts a thread */
- WaitThread(void * (*thread)(void*), void * arg);
+ WaitThread(Thread::ThreadFunction thread, void * arg);
/* starts a thread */
- void start(void * (*thread)(void *), void * arg);
+ void start(Thread::ThreadFunction thread, void * arg);
bool isRunning();
void kill();
virtual ~WaitThread();
public:
/* actually runs the thread */
void doRun();
protected:
- pthread_mutex_t doneLock;
- pthread_t thread;
+ Thread::Lock doneLock;
+ Thread::ThreadId thread;
volatile bool done;
void * arg;
- void * (*function)(void *);
+ Thread::ThreadFunction function;
};
/* wraps a boolean with lock/unlock while checking/setting it */
class ThreadBoolean{
public:
- ThreadBoolean(volatile bool & what, pthread_mutex_t & lock);
+ ThreadBoolean(volatile bool & what, Thread::Lock & lock);
bool get();
void set(bool value);
protected:
volatile bool & what;
- pthread_mutex_t & lock;
+ Thread::Lock & lock;
};
}
#endif

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 12:33 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69051
Default Alt Text
(4 KB)

Event Timeline