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 * Last modified: 00010 * $Date: 2015-09-27 19:00:38 +0200 (Sun, 27 Sep 2015) $ by $Author: schulte $ 00011 * $Revision: 14762 $ 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 namespace Gecode { namespace Support { 00039 00040 /* 00041 * Runnable objects 00042 */ 00043 forceinline 00044 Runnable::Runnable(bool d0) 00045 : d(d0) {} 00046 forceinline void 00047 Runnable::todelete(bool d0) { 00048 d=d0; 00049 } 00050 forceinline bool 00051 Runnable::todelete(void) const { 00052 return d; 00053 } 00054 forceinline void 00055 Runnable::operator delete(void* p) { 00056 heap.rfree(p); 00057 } 00058 forceinline void* 00059 Runnable::operator new(size_t s) { 00060 return heap.ralloc(s); 00061 } 00062 00063 00064 /* 00065 * Mutexes 00066 * 00067 */ 00068 forceinline void* 00069 Mutex::operator new(size_t s) { 00070 return Gecode::heap.ralloc(s); 00071 } 00072 00073 forceinline void 00074 Mutex::operator delete(void* p) { 00075 Gecode::heap.rfree(p); 00076 } 00077 00078 #if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_PTHREADS_SPINLOCK) 00079 00080 /* 00081 * Fast mutexes 00082 * 00083 */ 00084 forceinline void* 00085 FastMutex::operator new(size_t s) { 00086 return Gecode::heap.ralloc(s); 00087 } 00088 00089 forceinline void 00090 FastMutex::operator delete(void* p) { 00091 Gecode::heap.rfree(p); 00092 } 00093 00094 #endif 00095 00096 /* 00097 * Locks 00098 */ 00099 forceinline 00100 Lock::Lock(Mutex& m0) : m(m0) { 00101 m.acquire(); 00102 } 00103 forceinline 00104 Lock::~Lock(void) { 00105 m.release(); 00106 } 00107 00108 00109 /* 00110 * Threads 00111 */ 00112 inline void 00113 Thread::Run::run(Runnable* r0) { 00114 m.acquire(); 00115 r = r0; 00116 m.release(); 00117 e.signal(); 00118 } 00119 inline void 00120 Thread::run(Runnable* r) { 00121 m()->acquire(); 00122 if (idle != NULL) { 00123 Run* i = idle; 00124 idle = idle->n; 00125 m()->release(); 00126 i->run(r); 00127 } else { 00128 m()->release(); 00129 (void) new Run(r); 00130 } 00131 } 00132 forceinline void 00133 Thread::Run::operator delete(void* p) { 00134 heap.rfree(p); 00135 } 00136 forceinline void* 00137 Thread::Run::operator new(size_t s) { 00138 return heap.ralloc(s); 00139 } 00140 00141 }} 00142 00143 // STATISTICS: support-any