Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F126728
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/thread.cpp b/util/thread.cpp
index aac754db..552fbe76 100644
--- a/util/thread.cpp
+++ b/util/thread.cpp
@@ -1,54 +1,72 @@
#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();
return NULL;
}
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;
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); */
}
+ThreadBoolean::ThreadBoolean(volatile bool & what, pthread_mutex_t & lock):
+what(what),
+lock(lock){
+}
+
+bool ThreadBoolean::get(){
+ pthread_mutex_lock(&lock);
+ bool b = what;
+ pthread_mutex_unlock(&lock);
+ return b;
+}
+
+void ThreadBoolean::set(bool value){
+ pthread_mutex_lock(&lock);
+ what = value;
+ pthread_mutex_unlock(&lock);
+}
+
}
diff --git a/util/thread.h b/util/thread.h
index 98627f53..096755b5 100644
--- a/util/thread.h
+++ b/util/thread.h
@@ -1,38 +1,51 @@
#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 *);
};
+/* wraps a boolean with lock/unlock while checking/setting it */
+class ThreadBoolean{
+public:
+ ThreadBoolean(volatile bool & what, pthread_mutex_t & lock);
+
+ bool get();
+ void set(bool value);
+
+protected:
+ volatile bool & what;
+ pthread_mutex_t & lock;
+};
+
}
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 12:44 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69091
Default Alt Text
(2 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline