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