CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
CiftiMutex.h
1 #ifndef __CIFTI_MUTEX_H__
2 #define __CIFTI_MUTEX_H__
3 
4 /*LICENSE_START*/
5 /*
6  * Copyright (c) 2016, Washington University School of Medicine
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifdef _OPENMP
32 #define __CIFTI_MUTEX_H_HAVE_IMPL__
33 
34 #include "omp.h"
35 
36 namespace cifti
37 {
38  class CiftiMutex
39  {
40  omp_lock_t m_lock;
41  public:
42  CiftiMutex(const CiftiMutex&) { omp_init_lock(&m_lock); };//allow copy, assign, but make them do nothing other than default construct
43  CiftiMutex& operator=(const CiftiMutex&) { return *this; };
44  CiftiMutex() { omp_init_lock(&m_lock); }
45  ~CiftiMutex() { omp_destroy_lock(&m_lock); }
46  friend class CiftiMutexLocker;
47  };
48 
49  class CiftiMutexLocker
50  {
51  CiftiMutex* m_mutex;
52  CiftiMutexLocker();//disallow default construction, assign
53  CiftiMutexLocker& operator=(const CiftiMutexLocker& rhs);
54  public:
55  CiftiMutexLocker(CiftiMutex* mutex) { m_mutex = mutex; omp_set_lock(&(m_mutex->m_lock)); }
56  ~CiftiMutexLocker() { omp_unset_lock(&(m_mutex->m_lock)); }
57  };
58 }
59 
60 #else //_OPENMP
61 
62 #ifdef CIFTILIB_USE_QT
63 #define __CIFTI_MUTEX_H_HAVE_IMPL__
64 
65 #include <QMutex>
66 
67 namespace cifti
68 {
69  typedef QMutex CiftiMutex;
70  typedef QMutexLocker CiftiMutexLocker;
71 }
72 
73 #endif //CIFTILIB_USE_QT
74 
75 #ifdef CIFTILIB_USE_XMLPP
76 #define __CIFTI_MUTEX_H_HAVE_IMPL__
77 
78 #include <glibmm/thread.h>
79 
80 namespace cifti
81 {
82  typedef Glib::Mutex CiftiMutex;
83 
84  //API difference: glib's locker class takes a reference, while QT's takes a pointer
85  class CiftiMutexLocker
86  {
87  CiftiMutex* m_mutex;
88  CiftiMutexLocker();//disallow default construction, assign
89  CiftiMutexLocker& operator=(const CiftiMutexLocker& rhs);
90  public:
91  CiftiMutexLocker(CiftiMutex* mutex) { m_mutex = mutex; m_mutex->lock(); }
92  ~CiftiMutexLocker() { m_mutex->unlock(); }
93  };
94 }
95 
96 #endif //CIFTILIB_USE_XMLPP
97 
98 #endif //_OPENMP
99 
100 
101 #ifndef __CIFTI_MUTEX_H_HAVE_IMPL__
102 #error "you must have openmp support, or define either CIFTILIB_USE_QT or CIFTILIB_USE_XMLPP to select what mutex implementation to use"
103 #endif
104 
105 #endif //__CIFTI_MUTEX_H__
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:41