FXThread.h

Go to the documentation of this file.
00001 #ifdef FX_FOXCOMPAT
00002 
00003 /********************************************************************************
00004 *                                                                               *
00005 *                 M u l i t h r e a d i n g   S u p p o r t                     *
00006 *                                                                               *
00007 *********************************************************************************
00008 * Copyright (C) 2004,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00009 *********************************************************************************
00010 * This library is free software; you can redistribute it and/or                 *
00011 * modify it under the terms of the GNU Lesser General Public                    *
00012 * License as published by the Free Software Foundation; either                  *
00013 * version 2.1 of the License, or (at your option) any later version.            *
00014 *                                                                               *
00015 * This library is distributed in the hope that it will be useful,               *
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00018 * Lesser General Public License for more details.                               *
00019 *                                                                               *
00020 * You should have received a copy of the GNU Lesser General Public              *
00021 * License along with this library; if not, write to the Free Software           *
00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00023 *********************************************************************************
00024 * $Id: FXThread.h,v 1.40.2.2 2006/07/26 15:25:53 fox Exp $                          *
00025 ********************************************************************************/
00026 #ifndef FXTHREAD_H
00027 #define FXTHREAD_H
00028 
00029 namespace FX {
00030 
00031 
00032 // Thread ID type
00033 #ifndef WIN32
00034 typedef unsigned long FXThreadID;
00035 #else
00036 typedef void*         FXThreadID;
00037 #endif
00038 
00039 
00040 class FXCondition;
00041 class QThreadFXThread;
00042 
00043 
00048 class FXAPI FXMutex {
00049   friend class FXCondition;
00050 private:
00051   FXuval data[24];
00052 private:
00053   FXMutex(const FXMutex&);
00054   FXMutex &operator=(const FXMutex&);
00055 public:
00056 
00058   FXMutex(FXbool recursive=FALSE);
00059 
00061   void lock();
00062 
00064   FXbool trylock();
00065 
00067   FXbool locked();
00068 
00070   void unlock();
00071 
00073   ~FXMutex();
00074   };
00075 
00076 
00084 class FXAPI FXMutexLock {
00085 private:
00086   FXMutex& mtx;
00087 private:
00088   FXMutexLock();
00089   FXMutexLock(const FXMutexLock&);
00090   FXMutexLock& operator=(const FXMutexLock&);
00091 public:
00092 
00094   FXMutexLock(FXMutex& m):mtx(m){ lock(); }
00095 
00097   FXMutex& mutex(){ return mtx; }
00098 
00100   void lock(){ mtx.lock(); }
00101 
00103   FXbool trylock(){ return mtx.trylock(); }
00104 
00106   FXbool locked(){ return mtx.locked(); }
00107 
00109   void unlock(){ mtx.unlock(); }
00110 
00112   ~FXMutexLock(){ unlock(); }
00113   };
00114 
00115 
00123 class FXAPI FXCondition {
00124 private:
00125   FXuval data[12];
00126 private:
00127   FXCondition(const FXCondition&);
00128   FXCondition& operator=(const FXCondition&);
00129 public:
00130 
00132   FXCondition();
00133 
00138   void wait(FXMutex& mtx);
00139 
00147   FXbool wait(FXMutex& mtx,FXlong nsec);
00148 
00152   void signal();
00153 
00157   void broadcast();
00158 
00160   ~FXCondition();
00161   };
00162 
00163 
00168 class FXAPI FXSemaphore {
00169 private:
00170   FXuval data[16];
00171 private:
00172   FXSemaphore(const FXSemaphore&);
00173   FXSemaphore& operator=(const FXSemaphore&);
00174 public:
00175 
00177   FXSemaphore(FXint initial=1);
00178 
00180   void wait();
00181 
00183   FXbool trywait();
00184 
00186   void post();
00187 
00189   ~FXSemaphore();
00190   };
00191 
00192 
00200 class FXAPI FXThread {
00201 private:
00202   friend class QThreadFXThread;
00203   QThreadFXThread *r;
00204 private:
00205   FXThread(const FXThread&);
00206   FXThread &operator=(const FXThread&);
00207 protected:
00208 
00213   virtual FXint run() = 0;
00214 
00215 public:
00216 
00218   FXThread();
00219 
00225   FXThreadID id() const;
00226 
00230   FXbool running() const;
00231 
00237   FXbool start(unsigned long stacksize=0);
00238 
00242   FXbool join();
00243 
00249   FXbool join(FXint& code);
00250 
00257   FXbool cancel();
00258 
00263   FXbool detach();
00264 
00270   static void exit(FXint code=0);
00271 
00275   static void yield();
00276 
00280   static FXlong time();
00281 
00285   static void sleep(FXlong nsec);
00286 
00290   static void wakeat(FXlong nsec);
00291 
00297   static FXThread* self();
00298 
00302   static FXThreadID current();
00303 
00307   void priority(FXint prio);
00308 
00312   FXint priority();
00313 
00319   virtual ~FXThread();
00320   };
00321 
00322 }
00323 
00324 #endif
00325 #endif

(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:31:24 2009 for TnFOX by doxygen v1.4.7