diff options
Diffstat (limited to 'src/libjin/Thread/Thread.h')
-rw-r--r-- | src/libjin/Thread/Thread.h | 108 |
1 files changed, 105 insertions, 3 deletions
diff --git a/src/libjin/Thread/Thread.h b/src/libjin/Thread/Thread.h index 2949589..e271fc9 100644 --- a/src/libjin/Thread/Thread.h +++ b/src/libjin/Thread/Thread.h @@ -3,19 +3,121 @@ #include "../modules.h" #if JIN_MODULES_THREAD +#include <string> +#include <map> +#if JIN_THREAD_SDL +# include "SDL2/SDL_thread.h" +#elif JIN_THREAD_CPP +# include <thread> +# include <mutex> +# include <condition_variable> +#endif + namespace jin { namespace thread { + /* + * ӢӢMutual exclusionд Mutexһڶ̱߳Уֹ߳ͬʱͬһԴ + * ȫֱждĻơĿͨƬһһٽcritical sectionɡٽ + * ָһԹԴзʵĴ룬һֻƻ㷨һ̡߳̿ӵжٽDz + * һӦûҪ˻ƵԴУꡢСжϴڶеĴ + * ݡͬ״̬ȵԴάЩԴͬһºǺѵģΪһ߳̿κһʱ̱ͣ + * ߣָѣ + */ + class Mutex; + class Conditional; + /* + * Thread:demand Receive a message from a thread. Wait for the message to exist before returning. + * Thread:getName Get the name of a thread. + * Thread:kill Forcefully terminate the thread. + * Thread:peek Receive a message from a thread, but leave it in the message box. + * Thread:receive Receive a message from a thread. + * Thread:send Send a message. + * Thread:set Set a value. + * Thread:start Starts the thread. + * Thread:wait Wait for a thread to finish. + */ class Thread { public: - Thread(); + union Value + { + enum + { + INTEGER, + REAL, + BOOL, + STRING, + POINTER + } type; + + int integer; + float real; + bool boolean; + const char* cstring; + void* pointer; + + Value() {}; + Value(int i) : integer(i), type(INTEGER){}; + Value(float r) : real(r), type(REAL) {}; + Value(bool b) : boolean(b), type(BOOL) {}; + Value(const char* cstr) : cstring(cstr), type(STRING) {}; + Value(void* p) : pointer(p), type(POINTER) {}; + }; + + private: + class ThreadData + { + public: + ThreadData(Mutex*, Conditional*); + ~ThreadData(); + bool exist(const std::string& name); + void set(std::string name, Value value); + Value get(std::string name); + void remove(std::string name); + Conditional* condition; + Mutex* mutex; + + private: + std::map<std::string, Value> share; // threads shared value + }; + + public: + typedef int(ThreadRunner)(void* /*ThreadData*/); + Thread(const std::string name, ThreadRunner threadfuncs); + ~Thread(); + bool start(); + void wait(); + void send(std::string name, Value value); + bool receive(std::string name); + Value fetch(std::string name); + Value demand(std::string name); + const char* getName(); + bool isRunning(); + //void kill(); + //Value peek(); + + private: + void lock(); + void unlock(); + #if JIN_THREAD_SDL + SDL_Thread* handle; // SDL thread + #elif JIN_THREAD_CPP + std::thread* handle; // cpp thread + #endif + Mutex* mutex; // mutex variable + Conditional* condition; // condition variable + ThreadRunner* threadRunner; // thread function + ThreadData* common; // threads common data + const std::string name; // thread name + bool running; // running + }; -} -} +} // thread +} // jin #endif // JIN_MODULES_THREAD #endif // __JIN_THREAD_H
\ No newline at end of file |