KiCad PCB EDA Suite
openmp_mutex.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
33 #ifndef _OPENMP_MUTEX_H
34 #define _OPENMP_MUTEX_H
35 
36 #ifdef _OPENMP
37 
38 # include <omp.h>
39 
40 struct MutexType
41 {
42  MutexType() { omp_init_lock( &lock ); }
43  ~MutexType() { omp_destroy_lock( &lock ); }
44  void Lock() { omp_set_lock( &lock ); }
45  void Unlock() { omp_unset_lock( &lock ); }
46 
47  MutexType( const MutexType& ) { omp_init_lock( &lock ); }
48  MutexType& operator= ( const MutexType& ) { return *this; }
49 public:
50  omp_lock_t lock;
51 };
52 
53 #else
54 
57 struct MutexType
58 {
59  void Lock() {}
60  void Unlock() {}
61 };
62 #endif
63 
65 struct ScopedLock
66 {
67  explicit ScopedLock( MutexType& m ) : mut( m ), locked( true ) { mut.Lock(); }
69  void Unlock() { if( !locked ) return; locked = false; mut.Unlock(); }
70  void LockAgain() { if( locked ) return; mut.Lock(); locked = true; }
71 
72 private:
74  bool locked;
75 
76 private: // prevent copying the scoped lock.
77  void operator=(const ScopedLock&);
78  ScopedLock(const ScopedLock&);
79 };
80 
81 #endif // _OPENMP_MUTEX_H
void LockAgain()
Definition: openmp_mutex.h:70
MutexType & mut
Definition: openmp_mutex.h:73
ScopedLock(MutexType &m)
Definition: openmp_mutex.h:67
A dummy mutex that doesn't actually exclude anything, but as there is no parallelism either...
Definition: openmp_mutex.h:57
An exception-safe scoped lock-keeper.
Definition: openmp_mutex.h:65
void operator=(const ScopedLock &)
void Lock()
Definition: openmp_mutex.h:59
void Unlock()
Definition: openmp_mutex.h:69
void Unlock()
Definition: openmp_mutex.h:60