g2o
Loading...
Searching...
No Matches
openmp_mutex.h
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright notice,
10// this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#ifndef G2O_OPENMP_MUTEX
28#define G2O_OPENMP_MUTEX
29
30#include "g2o/config.h"
31
32#ifdef G2O_OPENMP
33#include <omp.h>
34#else
35#include <cassert>
36#endif
37
38namespace g2o {
39
40#ifdef G2O_OPENMP
41
45class OpenMPMutex {
46 public:
47 OpenMPMutex() { omp_init_lock(&_lock); }
48 ~OpenMPMutex() { omp_destroy_lock(&_lock); }
49 void lock() { omp_set_lock(&_lock); }
50 void unlock() { omp_unset_lock(&_lock); }
51
52 protected:
53 omp_lock_t _lock;
54};
55
56#else
57
58/*
59 * dummy which does nothing in case we don't have OpenMP support.
60 * In debug mode, the mutex allows to verify the correct lock and unlock
61 * behavior
62 */
64 public:
65#ifdef NDEBUG
66 OpenMPMutex() {}
67#else
68 OpenMPMutex() : _cnt(0) {}
69#endif
70 ~OpenMPMutex() { assert(_cnt == 0 && "Freeing locked mutex"); }
71 void lock() { assert(++_cnt == 1 && "Locking already locked mutex"); }
72 void unlock() {
73 assert(--_cnt == 0 && "Trying to unlock a mutex which is not locked");
74 }
75
76 protected:
77#ifndef NDEBUG
78 char _cnt;
79#endif
80};
81
82#endif
83
88 public:
89 explicit ScopedOpenMPMutex(OpenMPMutex* mutex) : _mutex(mutex) {
90 _mutex->lock();
91 }
93
94 private:
98};
99
100} // namespace g2o
101
102#endif
lock a mutex within a scope
OpenMPMutex *const _mutex
void operator=(const ScopedOpenMPMutex &)
ScopedOpenMPMutex(const ScopedOpenMPMutex &)
ScopedOpenMPMutex(OpenMPMutex *mutex)