00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #ifndef _FL_THREAD_H_
00023 #define _FL_THREAD_H_
00024 
00025 #include "Enumerations.h"
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 #ifdef _WIN32
00043 
00044 #ifndef _WIN32_WCE
00045 # include <process.h>
00046 #endif
00047 # include <windows.h>
00048 # include <limits.h>
00049 
00050 #else
00051 
00052 # include <pthread.h>
00053 # include <signal.h>
00054 # include <unistd.h>
00055 
00056 #endif
00057 
00058 typedef int (*thread_function)(void * arg);
00059 
00061 
00062 
00064 class Fl_Mutex
00065 {
00066 public:
00067     Fl_Mutex() { init(); }
00068     ~Fl_Mutex() { destroy(); }
00069     inline void lock();
00070     inline void unlock();
00071 private:
00072     inline void init();
00073     inline void destroy();
00074 #ifndef _WIN32
00075     pthread_mutex_t cs;
00076     pthread_t owner_;
00077     int recursive_counter;
00078 #else
00079     CRITICAL_SECTION cs;
00080 #endif
00081 };
00082 
00084 
00085 
00087 class Fl_Thread
00088 {
00089 public:
00090     
00091     enum {
00092         IDLE_PR = 0,
00093         LOWEST_PR,
00094         BELOW_NORMAL_PR,
00095         NORMAL_PR,
00096         ABOVE_NORMAL_PR,
00097         HIGHEST_PR,
00098         REALTIME_PR
00099     };
00100 
00101     Fl_Thread() {
00102         _threadHandle = 0; _threadId = 0;_function = 0; _arg = 0;
00103         _kill_thread = 0; _th_running = 0; _ms_sleep = 0;
00104     }
00105     virtual ~Fl_Thread() { destroy(); }
00106 
00107     inline bool create(thread_function function = 0, void* arg = 0);
00108     inline void destroy(int exitcode);
00109     inline void join(int timeout = 100);
00110     inline void kill_thread() { _kill_thread = true; }
00111 
00112     inline int get_priority() const;
00113     inline int set_priority(unsigned int priority);
00114 
00115     virtual int single_step() { return 0; }
00116 
00117     
00118     void ms_sleep(int sleep) { _ms_sleep = sleep; }
00119     int ms_sleep()           { return _ms_sleep;  }
00120 
00121 private:
00122     int internal_th_function();
00123 
00124     bool _kill_thread, _th_running;
00125     unsigned int _ms_sleep;
00126 
00127     unsigned long   _threadId;
00128     thread_function _function;
00129     void *_arg;
00130 
00131     
00132 #ifndef _WIN32
00133     static void *st_th_func(void *arg);
00134     pthread_t _threadHandle;
00135 #else
00136     static int st_th_func(void *arg);
00137     HANDLE _threadHandle;
00138 #endif
00139     inline void destroy();
00140 };
00141 
00142 
00143 #ifdef _WIN32
00144 # include "Fl_Thread_w32.h"
00145 #else
00146 # include "Fl_Thread_Linux.h"
00147 #endif
00148 
00149 static inline int fl_create_thread(Fl_Thread& t, int (*f) (void *), void* p) {
00150     return t.create(f, p);
00151 }
00152 
00153 #endif