Mutex.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2011, SRI International (R)
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as published by
00006  * the Free Software Foundation, either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifdef USE_TBB
00019 #include <tbb/mutex.h>
00020 #else
00021 #include <iostream>
00022 #endif
00023 
00024 #include <OpenKarto/Mutex.h>
00025 
00026 namespace karto
00027 {
00028 
00032 
00033   struct MutexPrivate
00034   {
00035 #ifdef USE_TBB
00036     tbb::mutex m_Mutex;
00037 #endif
00038   };
00039 
00040   struct ScopedLockPrivate
00041   {
00042 #ifdef USE_TBB
00043     tbb::mutex::scoped_lock m_Lock;
00044 #endif
00045   };
00046 
00050 
00051   Mutex::Mutex()
00052     : m_pMutexPrivate(new MutexPrivate())
00053   {
00054 #ifndef USE_TBB
00055     std::cout << "Warning 'NULL Mutex' is used, so no thread safety! Compile with TBB and define USE_TBB to enable TBB mutex." << std::endl;
00056 #endif
00057   }
00058 
00059   Mutex::~Mutex()
00060   {
00061     delete m_pMutexPrivate;
00062   }
00063 
00067 
00068   Mutex::ScopedLock::ScopedLock()
00069     : m_pScopedLockPrivate(new ScopedLockPrivate())
00070   {
00071   }
00072 
00073   Mutex::ScopedLock::ScopedLock(Mutex& rMutex)
00074     : m_pScopedLockPrivate(new ScopedLockPrivate())
00075   {
00076     Acquire(rMutex);
00077   }
00078 
00079   Mutex::ScopedLock::~ScopedLock()
00080   {
00081     Release();
00082 
00083     delete m_pScopedLockPrivate;
00084   }
00085 
00086   void Mutex::ScopedLock::Acquire(Mutex& rMutex)
00087   {
00088 #ifdef USE_TBB
00089     m_pScopedLockPrivate->m_Lock.acquire(rMutex.m_pMutexPrivate->m_Mutex);
00090 #endif
00091   }
00092 
00093   bool Mutex::ScopedLock::TryAcquire(Mutex& rMutex)
00094   {
00095 #ifdef USE_TBB
00096     return m_pScopedLockPrivate->m_Lock.try_acquire(rMutex.m_pMutexPrivate->m_Mutex);
00097 #else
00098     return true;
00099 #endif
00100   }
00101 
00102   void Mutex::ScopedLock::Release()
00103   {
00104 #ifdef USE_TBB
00105     m_pScopedLockPrivate->m_Lock.release();
00106 #endif
00107   }
00108 }


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Sun Apr 2 2017 03:53:08