Visual Servoing Platform  version 3.6.0 under development (2023-09-27)
vpMutex.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
34 #ifndef _vpMutex_h_
35 #define _vpMutex_h_
36 
37 #include <iostream>
38 #include <visp3/core/vpConfig.h>
39 
40 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
41 
42 #if defined(VISP_HAVE_PTHREAD)
43 #include <pthread.h>
44 #elif defined(_WIN32)
45 // Include WinSock2.h before windows.h to ensure that winsock.h is not
46 // included by windows.h since winsock.h and winsock2.h are incompatible
47 #include <WinSock2.h>
48 #include <windows.h>
49 #endif
50 
69 class vpMutex
70 {
71 public:
72  vpMutex() : m_mutex()
73  {
74 #if defined(VISP_HAVE_PTHREAD)
75  pthread_mutex_init(&m_mutex, NULL);
76 #elif defined(_WIN32)
77 #ifdef WINRT_8_1
78  m_mutex = CreateMutexEx(NULL, NULL, 0, NULL);
79 #else
80  m_mutex = CreateMutex(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  {
92 #if defined(VISP_HAVE_PTHREAD)
93  pthread_mutex_lock(&m_mutex);
94 #elif defined(_WIN32)
95  DWORD dwWaitResult;
96 #ifdef WINRT_8_1
97  dwWaitResult = WaitForSingleObjectEx(m_mutex, INFINITE, FALSE);
98 #else
99  dwWaitResult = WaitForSingleObject(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  {
108 #if defined(VISP_HAVE_PTHREAD)
109  pthread_mutex_unlock(&m_mutex);
110 #elif defined(_WIN32)
111  // Release ownership of the mutex object
112  if (!ReleaseMutex(m_mutex)) {
113  // Handle error.
114  std::cout << "unlock() error: " << GetLastError() << std::endl;
115  }
116 #endif
117  }
118 
166  {
167  private:
168  vpMutex &_mutex;
169 
170  // private:
171  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
172  // vpScopedLock &operator=(const vpScopedLock &){
173  // throw vpException(vpException::functionNotImplementedError,"Not
174  // implemented!"); return *this;
175  // }
176  //#endif
177 
178  public:
180  vpScopedLock(vpMutex &mutex) : _mutex(mutex) { _mutex.lock(); }
182  virtual ~vpScopedLock() { _mutex.unlock(); }
183  };
184 
185 private:
186 #if defined(VISP_HAVE_PTHREAD)
187  pthread_mutex_t m_mutex;
188 #elif defined(_WIN32)
189  HANDLE m_mutex;
190 #endif
191 };
192 
193 #endif
194 #endif
Class that allows protection by mutex.
Definition: vpMutex.h:166
vpScopedLock(vpMutex &mutex)
Constructor that locks the mutex.
Definition: vpMutex.h:180
virtual ~vpScopedLock()
Destructor that unlocks the mutex.
Definition: vpMutex.h:182
void unlock()
Definition: vpMutex.h:106
void lock()
Definition: vpMutex.h:90
vpMutex()
Definition: vpMutex.h:72