library of assembled shared sources

http://lass.cocamware.com

thread.cpp

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #include "util_common.h"
00044 #include "thread.h"
00045 #include "atomic.h"
00046 
00047 #if defined(LASS_UTIL_THREAD_HAVE_POSIX)
00048 #   include "impl/thread_posix.inl"
00049 #elif defined(LASS_UTIL_THREAD_HAVE_WIN32)
00050 #   include "impl/thread_win32.inl"
00051 #else
00052 #   error "[LASS BUILD MSG] Threading not supported for this platform"
00053 #endif
00054 
00055 namespace lass
00056 {
00057 namespace util
00058 {
00059 
00060 unsigned numberOfProcessors = impl::numberOfProcessors();
00061 
00062 // --- Mutex ---------------------------------------------------------------------------------------
00063 
00064 Mutex::Mutex(void )
00065 {
00066     pimpl_ = new impl::MutexInternal;
00067 }
00068 
00069 Mutex::~Mutex(void)
00070 {
00071     LASS_ASSERT(pimpl_);
00072     LASS_ASSERT(pimpl_->lockCount() == 0);
00073     if (pimpl_->lockCount() > 0)
00074     {
00075         std::cerr << "[LASS RUN MSG] UNDEFINED BEHAVIOUR WARNING: "
00076             << "destroying a CriticalSection that still has " 
00077             << pimpl_->lockCount() << " locks." << std::endl;
00078     }
00079     delete pimpl_;
00080     pimpl_ = 0;
00081 }
00082 
00083 void Mutex::lock()
00084 {
00085     LASS_ASSERT(pimpl_);
00086     pimpl_->lock();
00087 }
00088 
00089 const LockResult Mutex::tryLock()
00090 {
00091     LASS_ASSERT(pimpl_);
00092     return pimpl_->tryLock();
00093 }
00094 
00095 void Mutex::unlock()
00096 {
00097     LASS_ASSERT(pimpl_);
00098     pimpl_->unlock();
00099 }
00100 
00101 const bool Mutex::isLocked() const
00102 {
00103     LASS_ASSERT(pimpl_);
00104     return pimpl_->lockCount() > 0;
00105 }
00106 
00107 
00108 
00109 
00110 // --- CriticalSection -----------------------------------------------------------------------------
00111 
00112 CriticalSection::CriticalSection(void )
00113 {
00114     pimpl_ = new impl::CriticalSectionInternal;
00115 }
00116 
00117 CriticalSection::~CriticalSection(void)
00118 {
00119     LASS_ASSERT(pimpl_);
00120     impl::CriticalSectionInternal* pimpl = static_cast<impl::CriticalSectionInternal*>(pimpl_);
00121     LASS_ASSERT(pimpl->lockCount() == 0);
00122     if (pimpl->lockCount() > 0)
00123     {
00124         std::cerr << "[LASS RUN MSG] UNDEFINED BEHAVIOUR WARNING: "
00125             << "destroying a CriticalSection that still has " 
00126             << pimpl->lockCount() << " locks." << std::endl;
00127     }
00128     delete pimpl;
00129     pimpl_ = 0;
00130 }
00131 
00132 void CriticalSection::lock()
00133 {
00134     LASS_ASSERT(pimpl_);
00135     impl::CriticalSectionInternal* pimpl = static_cast<impl::CriticalSectionInternal*>(pimpl_);
00136     pimpl->lock();
00137 }
00138 
00139 const LockResult CriticalSection::tryLock()
00140 {
00141     LASS_ASSERT(pimpl_);
00142     impl::CriticalSectionInternal* pimpl = static_cast<impl::CriticalSectionInternal*>(pimpl_);
00143     return pimpl->tryLock();
00144 }
00145 
00146 void CriticalSection::unlock()
00147 {
00148     LASS_ASSERT(pimpl_);
00149     impl::CriticalSectionInternal* pimpl = static_cast<impl::CriticalSectionInternal*>(pimpl_);
00150     pimpl->unlock();
00151 }
00152 
00153 const bool CriticalSection::isLocked() const
00154 {
00155     LASS_ASSERT(pimpl_);
00156     impl::CriticalSectionInternal* pimpl = static_cast<impl::CriticalSectionInternal*>(pimpl_);
00157     return pimpl->lockCount() > 0;
00158 }
00159 
00160 
00161 
00162 // --- Condition -----------------------------------------------------------------------------------
00163 
00164 Condition::Condition(void)
00165 {
00166     pimpl_ = new impl::ConditionInternal;
00167 }
00168 
00169 Condition::~Condition(void)
00170 {
00171     delete pimpl_;
00172     pimpl_ = 0;
00173 }
00174 
00175 void Condition::wait()
00176 {
00177     LASS_ASSERT(pimpl_);
00178     pimpl_->wait();
00179 }
00180 
00181 const WaitResult Condition::wait(unsigned long iMilliSeconds)
00182 {
00183     LASS_ASSERT(pimpl_);
00184     return pimpl_->wait(iMilliSeconds);
00185 }
00186 
00187 void Condition::signal()
00188 {
00189     LASS_ASSERT(pimpl_);
00190     pimpl_->signal();
00191 }
00192 
00193 void Condition::broadcast()
00194 {
00195     LASS_ASSERT(pimpl_);
00196     pimpl_->broadcast();
00197 }
00198 
00199 
00200 
00201 // --- Thread --------------------------------------------------------------------------------------
00202 
00203 Thread::Thread(ThreadKind iKind, const char* name)
00204 {
00205     pimpl_ = new impl::ThreadInternal(*this, iKind, name);
00206 }
00207 
00208 Thread::~Thread()
00209 {
00210     delete pimpl_;
00211     pimpl_ = 0;
00212 }
00213 
00214 void Thread::run()
00215 {
00216     LASS_ASSERT(pimpl_);
00217     pimpl_->run();
00218 }
00219 
00220 void Thread::join()
00221 {
00222     LASS_ASSERT(pimpl_);
00223     pimpl_->join();
00224 }
00225 
00226 /** bind this thread to a processor (this as in this-pointer)
00227  */
00228 void Thread::bind(unsigned processor)
00229 {
00230     LASS_ASSERT(pimpl_);
00231     pimpl_->bind(processor);
00232 }
00233 
00234 void Thread::sleep(unsigned long iMilliSeconds)
00235 {
00236     impl::ThreadInternal::sleep(iMilliSeconds);
00237 }
00238 
00239 void Thread::yield()
00240 {
00241     impl::ThreadInternal::yield();
00242 }
00243 
00244 /** bind current thread to a processor (current as in callee's context)
00245  */
00246 void Thread::bindCurrent(unsigned processor)
00247 {
00248     impl::ThreadInternal::bindCurrent(processor);
00249 }
00250 
00251 
00252 
00253 // --- ThreadLocalStorage --------------------------------------------------------------------------
00254 
00255 ThreadLocalStorage::ThreadLocalStorage(void (*destructor)(void*))
00256 {
00257     pimpl_ = new impl::ThreadLocalStorageInternal(destructor);
00258 }
00259 
00260 ThreadLocalStorage::~ThreadLocalStorage()
00261 {
00262     delete pimpl_;
00263     pimpl_ = 0;
00264 }
00265 
00266 void* const ThreadLocalStorage::get() const
00267 {
00268     LASS_ASSERT(pimpl_);
00269     return pimpl_->get();
00270 }
00271 
00272 void ThreadLocalStorage::set(void* value)
00273 {
00274     LASS_ASSERT(pimpl_);
00275     pimpl_->set(value);
00276 }
00277 
00278 }
00279 }
00280 
00281 // EOF

Generated on Mon Nov 10 14:21:40 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo