Generated on Mon Jul 6 18:09:20 2009 for Gecode by doxygen 1.5.9

thread.hh

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2009
00008  *
00009  *  Last modified:
00010  *     $Date: 2009-05-18 11:35:21 +0200 (Mon, 18 May 2009) $ by $Author: schulte $
00011  *     $Revision: 9138 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #ifndef __GECODE_SUPPORT_THREAD_HH__
00039 #define __GECODE_SUPPORT_THREAD_HH__
00040 
00041 #include <gecode/support.hh>
00042 
00043 #ifdef GECODE_THREADS_WINDOWS
00044 #include <windows.h>
00045 #endif
00046 
00047 #ifdef GECODE_THREADS_PTHREADS
00048 #include <pthread.h>
00049 #endif
00050 
00051 #ifdef GECODE_HAS_THREADS
00052 
00066 namespace Gecode { namespace Support {
00067 
00075   class Runnable {
00076   public:
00078     virtual void run(void) = 0;
00080     virtual ~Runnable(void) {}
00082     static void* operator new(size_t s);
00084     static void  operator delete(void* p);
00085   };
00086 
00099   class Thread {
00100   private:
00101 #ifdef GECODE_THREADS_WINDOWS
00103     HANDLE w_h;
00104 #endif
00105 #ifdef GECODE_THREADS_PTHREADS
00107     pthread_t p_t;
00108 #endif
00109   public:
00116     GECODE_SUPPORT_EXPORT Thread(Runnable* r);
00118     ~Thread(void);
00120     static void sleep(unsigned int ms);
00122     static unsigned int npu(void);
00123   private:
00125     Thread(const Thread&) {}
00127     void operator=(const Thread&) {}
00128   };
00129 
00130 
00142   class Mutex {
00143   private:
00144 #ifdef GECODE_THREADS_WINDOWS
00146     CRITICAL_SECTION w_cs;
00147 #endif
00148 #ifdef GECODE_THREADS_PTHREADS
00150     pthread_mutex_t p_m;
00151 #endif
00152   public:
00154     Mutex(void);
00156     void acquire(void);
00158     bool tryacquire(void);
00160     void release(void);
00162     ~Mutex(void);
00163   private:
00165     Mutex(const Mutex&) {}
00167     void operator=(const Mutex&) {}
00168   };
00169 
00177   class Lock {
00178   private:
00180     Mutex& m;
00181   public:
00183     Lock(Mutex& m0);
00185     ~Lock(void);
00186   private:
00188     Lock(const Lock& l) : m(l.m) {}
00190     void operator=(const Lock&) {}
00191   };
00192 
00203   class Event {
00204   private:
00205 #ifdef GECODE_THREADS_WINDOWS
00207     HANDLE w_h;    
00208 #endif
00209 #ifdef GECODE_THREADS_PTHREADS
00211     pthread_mutex_t p_m;
00213     pthread_cond_t p_c;
00215     bool p_s;
00216 #endif
00217   public:
00219     Event(void);
00221     void signal(void);
00223     void wait(void);
00225     ~Event(void);
00226   private:
00228     Event(const Event&) {}
00230     void operator=(const Event&) {}
00231   };
00232 
00233 }}
00234 
00235 #ifdef GECODE_THREADS_WINDOWS
00236 #include <gecode/support/thread/windows.hpp>
00237 #endif
00238 #ifdef GECODE_THREADS_PTHREADS
00239 #include <gecode/support/thread/pthreads.hpp>
00240 #endif
00241 
00242 #include <gecode/support/thread/thread.hpp>
00243 
00244 // Workaround for broken windows headers
00245 #ifdef GECODE_THREADS_WINDOWS
00246 #ifdef min
00247 #undef min
00248 #endif
00249 #ifdef max
00250 #undef max
00251 #endif
00252 #endif
00253 
00254 #endif
00255 
00256 #endif
00257 
00258 // STATISTICS: support-any