openmp_lock.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00008 /*  This program is free software; you can redistribute it and/or
00009 *  modify it under the terms of the GNU General Public
00010 *  License as published by the Free Software Foundation; either
00011 *  version 2 of the License, or (at your option) any later version.
00012 *
00013 *  This software is distributed in the hope that it will be useful,
00014 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 *  General Public License for more details.
00017 *
00018 *  You should have received a copy of the GNU General Public
00019 *  License along with this software. If not, see
00020 *  <http://www.gnu.org/licenses/>.
00021 *
00022 */
00023 
00024 #ifndef OPENMP_LOCK_H
00025 #define OPENMP_LOCK_H
00026 
00027 #include <hugin_config.h>
00028 
00029 namespace hugin_omp
00030 {
00031 
00032 #ifdef HAVE_OPENMP
00033 #include <omp.h>
00034 
00036 class Lock
00037 {
00038 public:
00039     Lock()       {omp_init_lock(&m_lock);}
00040     ~Lock()      {omp_destroy_lock(&m_lock);}
00041 
00042     void Set()   {omp_set_lock(&m_lock);}
00043     void Unset() {omp_unset_lock(&m_lock);}
00044     bool Test()  {return (omp_test_lock(&m_lock) != 0);}
00045 private:
00046     Lock(const Lock&);
00047     Lock& operator=(const Lock&);
00048     omp_lock_t m_lock;
00049 };
00050 #else
00051 /* A dummy mutex that doesn't actually exclude anything,
00052 * but as there is no parallelism either, no worries. */
00053 class Lock
00054 {
00055 public:
00056     void Set() {}
00057     void Unset() {}
00058     bool Test() { return false; }
00059 };
00060 #endif
00061  
00062  /* An exception-safe scoped lock-keeper. */
00063 class ScopedLock
00064 {
00065 public:
00066     ScopedLock(Lock& lock):m_lock(lock) {m_lock.Set();}
00067     ~ScopedLock() {m_lock.Unset();}
00068 private:
00069     ScopedLock(const ScopedLock&);
00070     ScopedLock&operator=(const ScopedLock&);
00071     Lock& m_lock;
00072 };
00073 
00074 }; // namespace omp
00075 #endif

Generated on 22 Jan 2018 for Hugintrunk by  doxygen 1.4.7