Visual Servoing Platform  version 3.6.1 under development (2024-07-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_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 
51 #ifdef ENABLE_VISP_NAMESPACE
52 namespace VISP_NAMESPACE_NAME
53 {
54 #endif
70 class VP_DEPRECATED vpMutex
71 {
72 public:
73  vpMutex() : m_mutex()
74  {
75 #if defined(VISP_HAVE_PTHREAD)
76  pthread_mutex_init(&m_mutex, nullptr);
77 #elif defined(_WIN32)
78 #ifdef WINRT_8_1
79  m_mutex = CreateMutexEx(nullptr, nullptr, 0, nullptr);
80 #else
81  m_mutex = CreateMutex(nullptr, // default security attributes
82  FALSE, // initially not owned
83  nullptr); // unnamed mutex
84 #endif
85  if (m_mutex == nullptr) {
86  std::cout << "CreateMutex error: " << GetLastError() << std::endl;
87  return;
88  }
89 #endif
90  }
91  void lock()
92  {
93 #if defined(VISP_HAVE_PTHREAD)
94  pthread_mutex_lock(&m_mutex);
95 #elif defined(_WIN32)
96  DWORD dwWaitResult;
97 #ifdef WINRT_8_1
98  dwWaitResult = WaitForSingleObjectEx(m_mutex, INFINITE, FALSE);
99 #else
100  dwWaitResult = WaitForSingleObject(m_mutex, // handle to mutex
101  INFINITE); // no time-out interval
102 #endif
103  if (dwWaitResult == WAIT_FAILED)
104  std::cout << "lock() error: " << GetLastError() << std::endl;
105 #endif
106  }
107  void unlock()
108  {
109 #if defined(VISP_HAVE_PTHREAD)
110  pthread_mutex_unlock(&m_mutex);
111 #elif defined(_WIN32)
112  // Release ownership of the mutex object
113  if (!ReleaseMutex(m_mutex)) {
114  // Handle error.
115  std::cout << "unlock() error: " << GetLastError() << std::endl;
116  }
117 #endif
118  }
119 
173  {
174  private:
175  vpMutex &_mutex;
176 
177  // private:
178  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
179  // vpScopedLock &operator=(const vpScopedLock &){
180  // throw vpException(vpException::functionNotImplementedError,"Not
181  // implemented!"); return *this;
182  // }
183  //#endif
184 
185  public:
187  vpScopedLock(vpMutex &mutex) : _mutex(mutex) { _mutex.lock(); }
189  virtual ~vpScopedLock() { _mutex.unlock(); }
190  };
191 
192 private:
193 #if defined(VISP_HAVE_PTHREAD)
194  pthread_mutex_t m_mutex;
195 #elif defined(_WIN32)
196  HANDLE m_mutex;
197 #endif
198 };
199 #ifdef ENABLE_VISP_NAMESPACE
200 }
201 #endif
202 #endif
203 #endif
Class that allows protection by mutex.
Definition: vpMutex.h:173
vpScopedLock(vpMutex &mutex)
Constructor that locks the mutex.
Definition: vpMutex.h:187
virtual ~vpScopedLock()
Destructor that unlocks the mutex.
Definition: vpMutex.h:189
void unlock()
Definition: vpMutex.h:107
void lock()
Definition: vpMutex.h:91
vpMutex()
Definition: vpMutex.h:73