Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Fl_Thread.h

00001 /*
00002  * $Id: Fl_Thread.h,v 1.6 2003/03/15 16:09:56 laza2000 Exp $
00003  *
00004  * Extended Fast Light Toolkit (EFLTK)
00005  * Copyright (C) 2002-2003 by EDE-Team
00006  * WWW: http://www.sourceforge.net/projects/ede
00007  *
00008  * Fast Light Toolkit (FLTK)
00009  * Copyright (C) 1998-2003 by Bill Spitzak and others.
00010  * WWW: http://www.fltk.org
00011  *
00012  * This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00013  * version 2. See COPYING for details.
00014  *
00015  * Author : Mikko Lahteenmaki
00016  * Email  : mikko@fltk.net
00017  *
00018  * Please report all bugs and problems to "efltk-bugs@fltk.net"
00019  *
00020  */
00021 
00022 #ifndef _FL_THREAD_H_
00023 #define _FL_THREAD_H_
00024 
00025 #include "Enumerations.h"
00026 
00027 // Inline classes to provide portable support for threads and mutexes.
00028 //
00029 // Fltk does not use this (it has an internal mutex implementation
00030 // that is used if Fl::lock() is called). This header file's only
00031 // purpose is so we can write portable demo programs. It may be useful
00032 // or an inspiration to people who want to try writing multithreaded
00033 // programs themselves.
00034 //
00035 // EFltk has no multithreaded support unless the main thread calls Fl::lock().
00036 // This main thread is the only thread allowed to call Fl::run or Fl::wait.
00037 // From then on fltk will be locked except when the main thread is actually
00038 // waiting for events from the user. Other threads must call Fl::lock() and
00039 // Fl::unlock() to surround calls to fltk (such as to change widgets or
00040 // redraw them).
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 // MUTEX CLASS
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 // THREAD CLASS
00085 
00087 class Fl_Thread
00088 {
00089 public:
00090     // Values for set/get priority functions
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     // sleep time between single_steps
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     // static thread linker function
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 // Include system depend inline functions
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 /* FL_THREAD_H_ */

Generated on Thu Jul 31 15:33:45 2003 for eFLTK by doxygen1.2.15