Mutex.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifdef USE_TBB
19 #include <tbb/mutex.h>
20 #else
21 #include <iostream>
22 #endif
23 
24 #include <OpenKarto/Mutex.h>
25 
26 namespace karto
27 {
28 
32 
33  struct MutexPrivate
34  {
35 #ifdef USE_TBB
36  tbb::mutex m_Mutex;
37 #endif
38  };
39 
41  {
42 #ifdef USE_TBB
43  tbb::mutex::scoped_lock m_Lock;
44 #endif
45  };
46 
50 
52  : m_pMutexPrivate(new MutexPrivate())
53  {
54 #ifndef USE_TBB
55  std::cout << "Warning 'NULL Mutex' is used, so no thread safety! Compile with TBB and define USE_TBB to enable TBB mutex." << std::endl;
56 #endif
57  }
58 
60  {
61  delete m_pMutexPrivate;
62  }
63 
67 
69  : m_pScopedLockPrivate(new ScopedLockPrivate())
70  {
71  }
72 
74  : m_pScopedLockPrivate(new ScopedLockPrivate())
75  {
76  Acquire(rMutex);
77  }
78 
80  {
81  Release();
82 
83  delete m_pScopedLockPrivate;
84  }
85 
87  {
88 #ifdef USE_TBB
89  m_pScopedLockPrivate->m_Lock.acquire(rMutex.m_pMutexPrivate->m_Mutex);
90 #endif
91  }
92 
94  {
95 #ifdef USE_TBB
96  return m_pScopedLockPrivate->m_Lock.try_acquire(rMutex.m_pMutexPrivate->m_Mutex);
97 #else
98  return true;
99 #endif
100  }
101 
103  {
104 #ifdef USE_TBB
105  m_pScopedLockPrivate->m_Lock.release();
106 #endif
107  }
108 }
karto::Mutex::m_pMutexPrivate
MutexPrivate * m_pMutexPrivate
Definition: Mutex.h:101
karto::Mutex::ScopedLock::ScopedLock
ScopedLock()
Definition: Mutex.cpp:68
karto::Mutex::ScopedLock::~ScopedLock
~ScopedLock()
Definition: Mutex.cpp:79
karto::Mutex::ScopedLock::TryAcquire
bool TryAcquire(Mutex &rMutex)
Definition: Mutex.cpp:93
karto::Mutex::~Mutex
~Mutex()
Definition: Mutex.cpp:59
karto::Mutex::Mutex
Mutex()
Definition: Mutex.cpp:51
karto::Mutex::ScopedLock::Release
void Release()
Definition: Mutex.cpp:102
karto::Mutex::ScopedLock::Acquire
void Acquire(Mutex &rMutex)
Definition: Mutex.cpp:86
karto::ScopedLockPrivate
Definition: Mutex.cpp:40
karto::MutexPrivate
Definition: Mutex.cpp:33
Mutex.h
karto::Mutex
Definition: Mutex.h:43
karto
Definition: Any.cpp:20


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Wed Mar 2 2022 00:37:22