Generated on Fri Mar 20 15:56:21 2015 for Gecode by doxygen 1.6.3

thread.hpp

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  *  Bugfixes provided by:
00010  *     David Rijsman <david.rijsman@quintiq.com>
00011  *
00012  *  Last modified:
00013  *     $Date: 2013-10-15 00:46:56 +0200 (Tue, 15 Oct 2013) $ by $Author: tack $
00014  *     $Revision: 14025 $
00015  *
00016  *  This file is part of Gecode, the generic constraint
00017  *  development environment:
00018  *     http://www.gecode.org
00019  *
00020  *  Permission is hereby granted, free of charge, to any person obtaining
00021  *  a copy of this software and associated documentation files (the
00022  *  "Software"), to deal in the Software without restriction, including
00023  *  without limitation the rights to use, copy, modify, merge, publish,
00024  *  distribute, sublicense, and/or sell copies of the Software, and to
00025  *  permit persons to whom the Software is furnished to do so, subject to
00026  *  the following conditions:
00027  *
00028  *  The above copyright notice and this permission notice shall be
00029  *  included in all copies or substantial portions of the Software.
00030  *
00031  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00032  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00033  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00034  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00035  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00036  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00037  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00038  *
00039  */
00040 
00041 #include <cstddef>
00042 
00043 #ifdef GECODE_THREADS_WINDOWS
00044 
00045 #ifndef NOMINMAX
00046 #  define NOMINMAX
00047 #endif
00048 
00049 #ifndef _WIN32_WINNT
00050 #  define _WIN32_WINNT 0x400
00051 #endif
00052 
00053 #ifndef WIN32_LEAN_AND_MEAN
00054 #  define WIN32_LEAN_AND_MEAN
00055 #endif
00056 
00057 #include <windows.h>
00058 
00059 #endif
00060 
00061 #ifdef GECODE_THREADS_PTHREADS
00062 
00063 #include <pthread.h>
00064 
00065 #ifdef GECODE_THREADS_OSX
00066 
00067 #include <libkern/OSAtomic.h>
00068 
00069 #endif
00070 
00071 #endif
00072 
00088 namespace Gecode { namespace Support {
00089 
00099   class Mutex {
00100   private:
00101 #ifdef GECODE_THREADS_WINDOWS
00102 
00103     CRITICAL_SECTION w_cs;
00104 #endif
00105 #ifdef GECODE_THREADS_PTHREADS
00106 
00107     pthread_mutex_t p_m;
00108 #endif
00109   public:
00111     Mutex(void);
00113     void acquire(void);
00115     bool tryacquire(void);
00117     void release(void);
00119     ~Mutex(void);
00121     static void* operator new(size_t s);
00123     static void  operator delete(void* p);
00124   private:
00126     Mutex(const Mutex&) {}
00128     void operator=(const Mutex&) {}
00129   };
00130 
00131 #if defined(GECODE_THREADS_WINDOWS) || !defined(GECODE_THREADS_PTHREADS)
00132 
00133   typedef Mutex FastMutex;
00134 
00135 #endif
00136 
00137 #ifdef GECODE_THREADS_PTHREADS
00138 
00139 #if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
00140 
00155   class FastMutex {
00156   private:
00157 #ifdef GECODE_THREADS_OSX
00158 
00159     OSSpinLock lck;
00160 #else
00161 
00162     pthread_spinlock_t p_s;
00163 #endif
00164   public:
00166     FastMutex(void);
00168     void acquire(void);
00170     bool tryacquire(void);
00172     void release(void);
00174     ~FastMutex(void);
00176     static void* operator new(size_t s);
00178     static void  operator delete(void* p);
00179   private:
00181     FastMutex(const FastMutex&) {}
00183     void operator=(const FastMutex&) {}
00184   };
00185 
00186 #else
00187 
00188   typedef Mutex FastMutex;
00189 
00190 #endif
00191 
00192 #endif
00193 
00199   class Lock {
00200   private:
00202     Mutex& m;
00203   public:
00205     Lock(Mutex& m0);
00207     ~Lock(void);
00208   private:
00210     Lock(const Lock& l) : m(l.m) {}
00212     void operator=(const Lock&) {}
00213   };
00214 
00223   class Event {
00224   private:
00225 #ifdef GECODE_THREADS_WINDOWS
00226 
00227     HANDLE w_h;    
00228 #endif
00229 #ifdef GECODE_THREADS_PTHREADS
00230 
00231     pthread_mutex_t p_m;
00233     pthread_cond_t p_c;
00235     bool p_s;
00236 #endif
00237   public:
00239     Event(void);
00241     void signal(void);
00243     void wait(void);
00245     ~Event(void);
00246   private:
00248     Event(const Event&) {}
00250     void operator=(const Event&) {}
00251   };
00252 
00258   class Runnable {
00259   public:
00261     virtual void run(void) = 0;
00263     virtual ~Runnable(void) {}
00265     static void* operator new(size_t s);
00267     static void  operator delete(void* p);
00268   };
00269 
00279   class Thread {
00280   public:
00282     class Run {
00283     public:
00285       Run* n;
00287       Runnable* r;
00289       Event e;
00291       Mutex m;
00293       GECODE_SUPPORT_EXPORT Run(Runnable* r);
00295       GECODE_SUPPORT_EXPORT void exec(void);
00297       void run(Runnable* r);
00299       static void* operator new(size_t s);
00301       static void  operator delete(void* p);
00302     };
00304     GECODE_SUPPORT_EXPORT static Mutex* m(void);
00306     GECODE_SUPPORT_EXPORT static Run* idle;
00307   public:
00317     static void run(Runnable* r);
00319     static void sleep(unsigned int ms);
00321     static unsigned int npu(void);
00322   private:
00324     Thread(const Thread&) {}
00326     void operator=(const Thread&) {}
00327   };
00328 
00329 }}
00330 
00331 // STATISTICS: support-any