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: 2010-05-11 20:29:52 +1000 (Tue, 11 May 2010) $ by $Author: tack $ 00014 * $Revision: 10939 $ 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 #ifdef GECODE_THREADS_WINDOWS 00042 00043 #ifndef NOMINMAX 00044 # define NOMINMAX 00045 #endif 00046 00047 #ifndef _WIN32_WINNT 00048 # define _WIN32_WINNT 0x400 00049 #endif 00050 00051 #ifndef WIN32_LEAN_AND_MEAN 00052 # define WIN32_LEAN_AND_MEAN 00053 #endif 00054 00055 #include <windows.h> 00056 00057 #endif 00058 00059 #ifdef GECODE_THREADS_PTHREADS 00060 00061 #include <pthread.h> 00062 00063 #endif 00064 00080 namespace Gecode { namespace Support { 00081 00091 class Mutex { 00092 private: 00093 #ifdef GECODE_THREADS_WINDOWS 00095 CRITICAL_SECTION w_cs; 00096 #endif 00097 #ifdef GECODE_THREADS_PTHREADS 00099 pthread_mutex_t p_m; 00100 #endif 00101 public: 00103 Mutex(void); 00105 void acquire(void); 00107 bool tryacquire(void); 00109 void release(void); 00111 ~Mutex(void); 00113 static void* operator new(size_t s); 00115 static void operator delete(void* p); 00116 private: 00118 Mutex(const Mutex&) {} 00120 void operator=(const Mutex&) {} 00121 }; 00122 00128 class Lock { 00129 private: 00131 Mutex& m; 00132 public: 00134 Lock(Mutex& m0); 00136 ~Lock(void); 00137 private: 00139 Lock(const Lock& l) : m(l.m) {} 00141 void operator=(const Lock&) {} 00142 }; 00143 00152 class Event { 00153 private: 00154 #ifdef GECODE_THREADS_WINDOWS 00156 HANDLE w_h; 00157 #endif 00158 #ifdef GECODE_THREADS_PTHREADS 00160 pthread_mutex_t p_m; 00162 pthread_cond_t p_c; 00164 bool p_s; 00165 #endif 00166 public: 00168 Event(void); 00170 void signal(void); 00172 void wait(void); 00174 ~Event(void); 00175 private: 00177 Event(const Event&) {} 00179 void operator=(const Event&) {} 00180 }; 00181 00187 class Runnable { 00188 public: 00190 virtual void run(void) = 0; 00192 virtual ~Runnable(void) {} 00194 static void* operator new(size_t s); 00196 static void operator delete(void* p); 00197 }; 00198 00208 class Thread { 00209 public: 00211 class Run { 00212 public: 00214 Run* n; 00216 Runnable* r; 00218 Event e; 00220 Mutex m; 00222 GECODE_SUPPORT_EXPORT Run(Runnable* r); 00224 GECODE_SUPPORT_EXPORT void exec(void); 00226 void run(Runnable* r); 00228 static void* operator new(size_t s); 00230 static void operator delete(void* p); 00231 }; 00233 GECODE_SUPPORT_EXPORT static Mutex m; 00235 GECODE_SUPPORT_EXPORT static Run* idle; 00236 public: 00246 static void run(Runnable* r); 00248 static void sleep(unsigned int ms); 00250 static unsigned int npu(void); 00251 private: 00253 Thread(const Thread&) {} 00255 void operator=(const Thread&) {} 00256 }; 00257 00258 }} 00259 00260 #ifdef GECODE_THREADS_WINDOWS 00261 #include <gecode/support/thread/windows.hpp> 00262 #endif 00263 #ifdef GECODE_THREADS_PTHREADS 00264 #include <gecode/support/thread/pthreads.hpp> 00265 #endif 00266 #ifndef GECODE_HAS_THREADS 00267 #include <gecode/support/thread/none.hpp> 00268 #endif 00269 00270 #include <gecode/support/thread/thread.hpp> 00271 00272 // STATISTICS: support-any