Generated on Tue Apr 18 10:22:15 2017 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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00014  *     $Revision: 14967 $
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   private:
00261     bool d;
00262   public:
00264     Runnable(bool d=true);
00266     void todelete(bool d);
00268     bool todelete(void) const;
00270     virtual void run(void) = 0;
00272     virtual ~Runnable(void) {}
00274     static void* operator new(size_t s);
00276     static void  operator delete(void* p);
00277   };
00278 
00288   class Thread {
00289   public:
00291     class Run {
00292     public:
00294       Run* n;
00296       Runnable* r;
00298       Event e;
00300       Mutex m;
00302       GECODE_SUPPORT_EXPORT Run(Runnable* r);
00304       GECODE_SUPPORT_EXPORT void exec(void);
00306       void run(Runnable* r);
00308       static void* operator new(size_t s);
00310       static void  operator delete(void* p);
00311     };
00313     GECODE_SUPPORT_EXPORT static Mutex* m(void);
00315     GECODE_SUPPORT_EXPORT static Run* idle;
00316   public:
00326     static void run(Runnable* r);
00328     static void sleep(unsigned int ms);
00330     static unsigned int npu(void);
00331   private:
00333     Thread(const Thread&) {}
00335     void operator=(const Thread&) {}
00336   };
00337 
00338 }}
00339 
00340 // STATISTICS: support-any