00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef fv_threads_H_
00011 #define fv_threads_H_
00012
00013
00014 #ifdef _WIN32
00015
00016 #include <windows.h>
00017 typedef HANDLE fv_thread;
00018 typedef unsigned fv_thread_id;
00019 typedef unsigned (__stdcall * fv_thread_routine)(void *);
00020 typedef CRITICAL_SECTION fv_critical_section;
00021
00022 #define fv_thread_return unsigned __stdcall
00023 #define INITCRITICALSECTION(crit_section) InitializeCriticalSection(&(crit_section));
00024 #define DELETECRITICALSECTION(crit_section) DeleteCriticalSection(&(crit_section));
00025 #define ENTERCRITICALSECTION(crit_section) EnterCriticalSection(&(crit_section));
00026 #define LEAVECRITICALSECTION(crit_section) LeaveCriticalSection(&(crit_section));
00027
00028 #else
00029
00030
00031 #include <pthread.h>
00032 typedef pthread_t fv_thread;
00033 typedef pthread_t fv_thread_id;
00034 typedef void *(*fv_thread_routine)(void *);
00035 typedef pthread_mutex_t fv_critical_section;
00036 typedef pthread_cond_t fv_signal_t;
00037
00038 #define fv_thread_return void*
00039 #define INITCRITICALSECTION(crit_section) pthread_mutex_init(&(crit_section),NULL);
00040 #define DELETECRITICALSECTION(crit_section) pthread_mutex_destroy(&(crit_section));
00041 #define ENTERCRITICALSECTION(crit_section) pthread_mutex_lock (&(crit_section));
00042 #define LEAVECRITICALSECTION(crit_section) pthread_mutex_unlock(&(crit_section));
00043 #define INITSIGNAL(signal_var) pthread_cond_init(&(signal_var), NULL);
00044 #define DELETESIGNAL(signal_var) pthread_cond_destroy(&(signal_var));
00045
00046 #endif
00047
00048 #ifdef __cplusplus
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 class Lock
00067 {
00068 protected:
00069 fv_critical_section _sec;
00070 bool _is;
00071
00072 public:
00073 Lock() : _is(false) {
00074 INITCRITICALSECTION(this->_sec);
00075 }
00076 ~Lock() {
00077 DELETECRITICALSECTION(this->_sec);
00078 }
00079
00080 void lock() {
00081 if (!_is) {
00082 ENTERCRITICALSECTION(this->_sec);
00083 _is = true;
00084 }
00085 }
00086 void unlock() {
00087 _is = false;
00088 LEAVECRITICALSECTION(this->_sec);
00089 }
00090 };
00091
00092
00093 class Thread
00094 {
00095 public:
00096 Thread();
00097 virtual ~Thread();
00098
00099 virtual void Start();
00100 virtual void Stop();
00101 virtual int Run();
00102 virtual int Check() const { return 1; }
00103
00104 static void* ThreadFunc(void *data);
00105
00106 void Terminate();
00107 void WaitForNotify();
00108 int Notify();
00109
00110 protected:
00111 fv_thread _handle;
00112 fv_thread_id _id;
00113 fv_critical_section _crtsec;
00114 fv_signal_t _signal;
00115
00116 };
00117
00118
00119
00120 extern "C" {
00121 #endif
00122
00123 extern int utd_internal_error;
00124
00125
00126 extern fv_thread fv_start_thread(fv_thread_id* thread_id, fv_thread_routine func, void *data);
00127
00128
00129 extern void fv_end_thread(fv_thread thread);
00130
00131
00132 extern void fv_destroy_thread(fv_thread thread);
00133
00134
00135 extern void fv_wait_for_threads(const fv_thread *threads, int num);
00136
00137
00138 extern int fv_wait_for_wakeup(fv_signal_t *signal,fv_critical_section *critical);
00139
00140
00141 extern int fv_wakeup(fv_signal_t *signal);
00142
00143 #ifdef __cplusplus
00144 }
00145 #endif
00146
00147
00148 #endif
00149