Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
vpMutex.h
1
/*
2
* ViSP, open source Visual Servoing Platform software.
3
* Copyright (C) 2005 - 2025 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
46
// Mute warning with clang-cl
47
// warning : non-portable path to file '<WinSock2.h>'; specified path differs in case from file name on disk [-Wnonportable-system-include-path]
48
// warning : non-portable path to file '<Windows.h>'; specified path differs in case from file name on disk [-Wnonportable-system-include-path]
49
#if defined(__clang__)
50
# pragma clang diagnostic push
51
# pragma clang diagnostic ignored "-Wnonportable-system-include-path"
52
#endif
53
54
// Include WinSock2.h before windows.h to ensure that winsock.h is not
55
// included by windows.h since winsock.h and winsock2.h are incompatible
56
#include <WinSock2.h>
57
#include <windows.h>
58
59
#if defined(__clang__)
60
# pragma clang diagnostic pop
61
#endif
62
#endif
63
64
#ifdef ENABLE_VISP_NAMESPACE
65
namespace
VISP_NAMESPACE_NAME
66
{
67
#endif
83
class
VP_DEPRECATED
vpMutex
84
{
85
public
:
86
vpMutex
() : m_mutex()
87
{
88
#if defined(VISP_HAVE_PTHREAD)
89
pthread_mutex_init(&m_mutex,
nullptr
);
90
#elif defined(_WIN32)
91
#ifdef WINRT_8_1
92
m_mutex = CreateMutexEx(
nullptr
,
nullptr
, 0,
nullptr
);
93
#else
94
m_mutex = CreateMutex(
nullptr
,
// default security attributes
95
FALSE,
// initially not owned
96
nullptr
);
// unnamed mutex
97
#endif
98
if
(m_mutex ==
nullptr
) {
99
std::cout <<
"CreateMutex error: "
<< GetLastError() << std::endl;
100
return
;
101
}
102
#endif
103
}
104
void
lock
()
105
{
106
#if defined(VISP_HAVE_PTHREAD)
107
pthread_mutex_lock(&m_mutex);
108
#elif defined(_WIN32)
109
DWORD dwWaitResult;
110
#ifdef WINRT_8_1
111
dwWaitResult = WaitForSingleObjectEx(m_mutex, INFINITE, FALSE);
112
#else
113
dwWaitResult = WaitForSingleObject(m_mutex,
// handle to mutex
114
INFINITE);
// no time-out interval
115
#endif
116
if
(dwWaitResult == WAIT_FAILED)
117
std::cout <<
"lock() error: "
<< GetLastError() << std::endl;
118
#endif
119
}
120
void
unlock
()
121
{
122
#if defined(VISP_HAVE_PTHREAD)
123
pthread_mutex_unlock(&m_mutex);
124
#elif defined(_WIN32)
125
// Release ownership of the mutex object
126
if
(!ReleaseMutex(m_mutex)) {
127
// Handle error.
128
std::cout <<
"unlock() error: "
<< GetLastError() << std::endl;
129
}
130
#endif
131
}
132
185
class
vpScopedLock
186
{
187
private
:
188
vpMutex
&_mutex;
189
190
// private:
191
//#ifndef DOXYGEN_SHOULD_SKIP_THIS
192
// vpScopedLock &operator=(const vpScopedLock &){
193
// throw vpException(vpException::functionNotImplementedError,"Not
194
// implemented!"); return *this;
195
// }
196
//#endif
197
198
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
199
vpScopedLock
&operator=(
const
vpScopedLock
&) =
delete
;
// non copyable
200
#endif
201
202
public
:
204
vpScopedLock
(
vpMutex
&mutex) : _mutex(mutex) { _mutex.lock(); }
206
virtual
~vpScopedLock
() { _mutex.unlock(); }
207
};
208
209
private
:
210
#if defined(VISP_HAVE_PTHREAD)
211
pthread_mutex_t m_mutex;
212
#elif defined(_WIN32)
213
HANDLE m_mutex;
214
#endif
215
};
216
#ifdef ENABLE_VISP_NAMESPACE
217
}
218
#endif
219
#endif
220
#endif
vpMutex::vpScopedLock::vpScopedLock
vpScopedLock(vpMutex &mutex)
Constructor that locks the mutex.
Definition
vpMutex.h:204
vpMutex::vpScopedLock::~vpScopedLock
virtual ~vpScopedLock()
Destructor that unlocks the mutex.
Definition
vpMutex.h:206
vpMutex::unlock
void unlock()
Definition
vpMutex.h:120
vpMutex::lock
void lock()
Definition
vpMutex.h:104
vpMutex::vpMutex
vpMutex()
Definition
vpMutex.h:86
VISP_NAMESPACE_NAME
Definition
vpEigenConversion.h:44
modules
core
include
visp3
core
vpMutex.h
Generated by
1.17.0