OpenNI 1.5.7
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
Include
XnThreadSafeQueue.h
Go to the documentation of this file.
1
/*****************************************************************************
2
* *
3
* OpenNI 1.x Alpha *
4
* Copyright (C) 2012 PrimeSense Ltd. *
5
* *
6
* This file is part of OpenNI. *
7
* *
8
* Licensed under the Apache License, Version 2.0 (the "License"); *
9
* you may not use this file except in compliance with the License. *
10
* You may obtain a copy of the License at *
11
* *
12
* http://www.apache.org/licenses/LICENSE-2.0 *
13
* *
14
* Unless required by applicable law or agreed to in writing, software *
15
* distributed under the License is distributed on an "AS IS" BASIS, *
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17
* See the License for the specific language governing permissions and *
18
* limitations under the License. *
19
* *
20
*****************************************************************************/
21
#ifndef __XN_THREAD_SAFE_QUEUE_H__
22
#define __XN_THREAD_SAFE_QUEUE_H__
23
24
//---------------------------------------------------------------------------
25
// Includes
26
//---------------------------------------------------------------------------
27
#include <
XnQueue.h
>
28
#include <
XnOS.h
>
29
30
//---------------------------------------------------------------------------
31
// Types
32
//---------------------------------------------------------------------------
36
class
XnThreadSafeQueue
:
public
XnQueue
37
{
38
public
:
39
XnThreadSafeQueue
() : m_hLock(NULL) {}
40
41
~XnThreadSafeQueue
()
42
{
43
xnOSCloseCriticalSection
(&m_hLock);
44
}
45
46
XnStatus
Init
()
47
{
48
XnStatus
nRetVal =
XN_STATUS_OK
;
49
50
nRetVal =
xnOSCreateCriticalSection
(&m_hLock);
51
XN_IS_STATUS_OK
(nRetVal);
52
53
return
(
XN_STATUS_OK
);
54
}
55
56
XnStatus
Push
(
XnValue
const
& value)
57
{
58
XnStatus
nRetVal =
XN_STATUS_OK
;
59
60
nRetVal =
xnOSEnterCriticalSection
(&m_hLock);
61
XN_IS_STATUS_OK
(nRetVal);
62
63
nRetVal =
XnQueue::Push
(value);
64
xnOSLeaveCriticalSection
(&m_hLock);
65
66
return
nRetVal;
67
}
68
69
XnStatus
Pop
(
XnValue
& value)
70
{
71
XnStatus
nRetVal =
XN_STATUS_OK
;
72
73
nRetVal =
xnOSEnterCriticalSection
(&m_hLock);
74
XN_IS_STATUS_OK
(nRetVal);
75
76
nRetVal =
XnQueue::Pop
(value);
77
xnOSLeaveCriticalSection
(&m_hLock);
78
79
return
nRetVal;
80
}
81
82
XnUInt32
Size
()
const
83
{
84
xnOSEnterCriticalSection
(&m_hLock);
85
XnUInt32 nSize =
XnQueue::Size
();
86
xnOSLeaveCriticalSection
(&m_hLock);
87
return
(nSize);
88
}
89
90
private
:
91
// NOTE: we declare the lock as mutable, as it may change on const methods.
92
mutable
XN_CRITICAL_SECTION_HANDLE m_hLock;
93
};
94
100
#define XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(decl, Type, ClassName, Translator) \
101
class decl ClassName : public XnThreadSafeQueue \
102
{ \
103
public: \
104
~ClassName() \
105
{ \
106
/* We do this using Pop() to make sure memory is freed. */
\
107
Type dummy; \
108
while (Size() != 0) \
109
Pop(dummy); \
110
} \
111
XnStatus Push(Type const& value) \
112
{ \
113
XnValue val = Translator::CreateValueCopy(value); \
114
XnStatus nRetVal = XnThreadSafeQueue::Push(val); \
115
if (nRetVal != XN_STATUS_OK) \
116
{ \
117
Translator::FreeValue(val); \
118
return (nRetVal); \
119
} \
120
return XN_STATUS_OK; \
121
} \
122
XnStatus Pop(Type& value) \
123
{ \
124
XnValue val; \
125
XnStatus nRetVal = XnThreadSafeQueue::Pop(val); \
126
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
127
value = Translator::GetFromValue(val); \
128
Translator::FreeValue(val); \
129
return XN_STATUS_OK; \
130
} \
131
};
132
138
#define XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR(Type, ClassName, Translator) \
139
XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(, Type, ClassName, Translator)
140
145
#define XN_DECLARE_THREAD_SAFE_QUEUE_DECL(decl, Type, ClassName) \
146
XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, Type, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
147
XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(decl, Type, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName))
148
152
#define XN_DECLARE_THREAD_SAFE_QUEUE(Type, ClassName) \
153
XN_DECLARE_THREAD_SAFE_QUEUE_DECL(, Type, ClassName)
154
155
#endif //__XN_THREAD_SAFE_QUEUE_H__
Generated on Tue Nov 12 2013 13:40:20 for OpenNI 1.5.7 by
1.8.3.1