Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F126660
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.h b/util/thread.h
index 9067afb0..06d73932 100644
--- a/util/thread.h
+++ b/util/thread.h
@@ -1,78 +1,113 @@
#ifndef _paintown_thread_h
#define _paintown_thread_h
#ifdef USE_SDL
#include <SDL_thread.h>
#include <SDL_mutex.h>
#else
#include <pthread.h>
#endif
namespace Util{
/* Either uses pthreads or SDL_thread */
namespace Thread{
#ifdef USE_SDL
typedef SDL_mutex* Lock;
typedef SDL_Thread* Id;
typedef int (*ThreadFunction)(void*);
#else
typedef pthread_mutex_t Lock;
typedef pthread_t Id;
typedef void * (*ThreadFunction)(void*);
#endif
void initializeLock(Lock * lock);
int acquireLock(Lock * lock);
int releaseLock(Lock * lock);
void destroyLock(Lock * lock);
bool 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;
};
+template<class X>
+class Future{
+public:
+ Future(){
+ }
+
+ virtual ~Future(){
+ }
+
+ virtual X get(){
+ Thread::joinThread(thread);
+ return thing;
+ }
+
+protected:
+ static void * runit(void * arg){
+ Future<X> * me = (Future<X>*) arg;
+ me->compute();
+ return NULL;
+ }
+
+ virtual void set(X x){
+ this->thing = x;
+ }
+
+ virtual void start(){
+ Thread::createThread(&thread, NULL, (Thread::ThreadFunction) runit, this);
+ }
+
+ virtual void compute() = 0;
+
+ X thing;
+ Thread::Id thread;
+};
+
}
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 12:26 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69024
Default Alt Text
(2 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline