Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMutex.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Mutex protection.
32  *
33  * Authors:
34  * Celine Teuliere
35  *
36  *****************************************************************************/
37 
38 
39 #ifndef __vpMutex_h_
40 #define __vpMutex_h_
41 
42 #include <visp3/core/vpConfig.h>
43 #include <iostream>
44 
45 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
46 
47 #if defined(VISP_HAVE_PTHREAD)
48 # include <pthread.h>
49 #elif defined(_WIN32)
50 # include <windows.h>
51 #endif
52 
70 class vpMutex {
71 public:
72  vpMutex() : m_mutex() {
73 #if defined(VISP_HAVE_PTHREAD)
74  pthread_mutex_init( &m_mutex, NULL );
75 #elif defined(_WIN32)
76 # ifdef WINRT_8_1
77  m_mutex = CreateMutexEx(NULL, NULL, 0, NULL);
78 # else
79  m_mutex = CreateMutex(
80  NULL, // default security attributes
81  FALSE, // initially not owned
82  NULL); // unnamed mutex
83 #endif
84  if (m_mutex == NULL) {
85  std::cout << "CreateMutex error: " << GetLastError() << std::endl;
86  return;
87  }
88 #endif
89  }
90  void lock() {
91 #if defined(VISP_HAVE_PTHREAD)
92  pthread_mutex_lock( &m_mutex );
93 #elif defined(_WIN32)
94  DWORD dwWaitResult;
95 # ifdef WINRT_8_1
96  dwWaitResult = WaitForSingleObjectEx(m_mutex, INFINITE, FALSE);
97 # else
98  dwWaitResult = WaitForSingleObject(
99  m_mutex, // handle to mutex
100  INFINITE); // no time-out interval
101 # endif
102  if (dwWaitResult == WAIT_FAILED)
103  std::cout << "lock() error: " << GetLastError() << std::endl;
104 #endif
105  }
106  void unlock() {
107 #if defined(VISP_HAVE_PTHREAD)
108  pthread_mutex_unlock( &m_mutex );
109 #elif defined(_WIN32)
110  // Release ownership of the mutex object
111  if (!ReleaseMutex(m_mutex))
112  {
113  // Handle error.
114  std::cout << "unlock() error: " << GetLastError() << std::endl;
115  }
116 #endif
117  }
118 
164  {
165  private:
166  vpMutex & _mutex;
167 
168 // private:
169 //#ifndef DOXYGEN_SHOULD_SKIP_THIS
170 // vpScopedLock &operator=(const vpScopedLock &){
171 // throw vpException(vpException::functionNotImplementedError,"Not implemented!");
172 // return *this;
173 // }
174 //#endif
175 
176  public:
179  : _mutex(mutex)
180  {
181  _mutex.lock();
182  }
185  {
186  _mutex.unlock();
187  }
188  };
189 private:
190 #if defined(VISP_HAVE_PTHREAD)
191  pthread_mutex_t m_mutex;
192 #elif defined(_WIN32)
193  HANDLE m_mutex;
194 #endif
195 };
196 
197 #endif
198 #endif
Class that allows protection by mutex.
Definition: vpMutex.h:163
void lock()
Definition: vpMutex.h:90
void unlock()
Definition: vpMutex.h:106
~vpScopedLock()
Destructor that unlocks the mutex.
Definition: vpMutex.h:184
vpMutex()
Definition: vpMutex.h:72
vpScopedLock(vpMutex &mutex)
Constructor that locks the mutex.
Definition: vpMutex.h:178