Visual Servoing Platform  version 3.6.1 under development (2024-04-18)
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_BUILD_DEPRECATED_FUNCTIONS) && (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 
66 class vp_deprecated vpMutex
67 {
68 public:
69  vpMutex() : m_mutex()
70  {
71 #if defined(VISP_HAVE_PTHREAD)
72  pthread_mutex_init(&m_mutex, nullptr);
73 #elif defined(_WIN32)
74 #ifdef WINRT_8_1
75  m_mutex = CreateMutexEx(nullptr, nullptr, 0, nullptr);
76 #else
77  m_mutex = CreateMutex(nullptr, // default security attributes
78  FALSE, // initially not owned
79  nullptr); // unnamed mutex
80 #endif
81  if (m_mutex == nullptr) {
82  std::cout << "CreateMutex error: " << GetLastError() << std::endl;
83  return;
84  }
85 #endif
86  }
87  void lock()
88  {
89 #if defined(VISP_HAVE_PTHREAD)
90  pthread_mutex_lock(&m_mutex);
91 #elif defined(_WIN32)
92  DWORD dwWaitResult;
93 #ifdef WINRT_8_1
94  dwWaitResult = WaitForSingleObjectEx(m_mutex, INFINITE, FALSE);
95 #else
96  dwWaitResult = WaitForSingleObject(m_mutex, // handle to mutex
97  INFINITE); // no time-out interval
98 #endif
99  if (dwWaitResult == WAIT_FAILED)
100  std::cout << "lock() error: " << GetLastError() << std::endl;
101 #endif
102  }
103  void unlock()
104  {
105 #if defined(VISP_HAVE_PTHREAD)
106  pthread_mutex_unlock(&m_mutex);
107 #elif defined(_WIN32)
108  // Release ownership of the mutex object
109  if (!ReleaseMutex(m_mutex)) {
110  // Handle error.
111  std::cout << "unlock() error: " << GetLastError() << std::endl;
112  }
113 #endif
114  }
115 
161  {
162  private:
163  vpMutex &_mutex;
164 
165  // private:
166  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
167  // vpScopedLock &operator=(const vpScopedLock &){
168  // throw vpException(vpException::functionNotImplementedError,"Not
169  // implemented!"); return *this;
170  // }
171  //#endif
172 
173  public:
175  vpScopedLock(vpMutex &mutex) : _mutex(mutex) { _mutex.lock(); }
177  virtual ~vpScopedLock() { _mutex.unlock(); }
178  };
179 
180 private:
181 #if defined(VISP_HAVE_PTHREAD)
182  pthread_mutex_t m_mutex;
183 #elif defined(_WIN32)
184  HANDLE m_mutex;
185 #endif
186 };
187 
188 #endif
189 #endif
Class that allows protection by mutex.
Definition: vpMutex.h:161
vpScopedLock(vpMutex &mutex)
Constructor that locks the mutex.
Definition: vpMutex.h:175
virtual ~vpScopedLock()
Destructor that unlocks the mutex.
Definition: vpMutex.h:177
void unlock()
Definition: vpMutex.h:103
void lock()
Definition: vpMutex.h:87
vpMutex()
Definition: vpMutex.h:69