Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __XN_THREAD_SAFE_QUEUE_H__
00026 #define __XN_THREAD_SAFE_QUEUE_H__
00027
00028
00029
00030
00031 #include <XnQueue.h>
00032 #include <XnOS.h>
00033
00034
00035
00036
00040 class XnThreadSafeQueue : public XnQueue
00041 {
00042 public:
00043 XnThreadSafeQueue() : m_hLock(NULL) {}
00044
00045 ~XnThreadSafeQueue()
00046 {
00047 xnOSCloseCriticalSection(&m_hLock);
00048 }
00049
00050 XnStatus Init()
00051 {
00052 XnStatus nRetVal = XN_STATUS_OK;
00053
00054 nRetVal = xnOSCreateCriticalSection(&m_hLock);
00055 XN_IS_STATUS_OK(nRetVal);
00056
00057 return (XN_STATUS_OK);
00058 }
00059
00060 XnStatus Push(XnValue const& value)
00061 {
00062 XnStatus nRetVal = XN_STATUS_OK;
00063
00064 nRetVal = xnOSEnterCriticalSection(&m_hLock);
00065 XN_IS_STATUS_OK(nRetVal);
00066
00067 nRetVal = XnQueue::Push(value);
00068 xnOSLeaveCriticalSection(&m_hLock);
00069
00070 return nRetVal;
00071 }
00072
00073 XnStatus Pop(XnValue& value)
00074 {
00075 XnStatus nRetVal = XN_STATUS_OK;
00076
00077 nRetVal = xnOSEnterCriticalSection(&m_hLock);
00078 XN_IS_STATUS_OK(nRetVal);
00079
00080 nRetVal = XnQueue::Pop(value);
00081 xnOSLeaveCriticalSection(&m_hLock);
00082
00083 return nRetVal;
00084 }
00085
00086 XnUInt32 Size() const
00087 {
00088 xnOSEnterCriticalSection(&m_hLock);
00089 XnUInt32 nSize = XnQueue::Size();
00090 xnOSLeaveCriticalSection(&m_hLock);
00091 return (nSize);
00092 }
00093
00094 private:
00095
00096 mutable XN_CRITICAL_SECTION_HANDLE m_hLock;
00097 };
00098
00104 #define XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(decl, Type, ClassName, Translator) \
00105 class decl ClassName : public XnThreadSafeQueue \
00106 { \
00107 public: \
00108 ~ClassName() \
00109 { \
00110 \
00111 Type dummy; \
00112 while (Size() != 0) \
00113 Pop(dummy); \
00114 } \
00115 XnStatus Push(Type const& value) \
00116 { \
00117 XnValue val = Translator::CreateValueCopy(value); \
00118 XnStatus nRetVal = XnThreadSafeQueue::Push(val); \
00119 if (nRetVal != XN_STATUS_OK) \
00120 { \
00121 Translator::FreeValue(val); \
00122 return (nRetVal); \
00123 } \
00124 return XN_STATUS_OK; \
00125 } \
00126 XnStatus Pop(Type& value) \
00127 { \
00128 XnValue val; \
00129 XnStatus nRetVal = XnThreadSafeQueue::Pop(val); \
00130 if (nRetVal != XN_STATUS_OK) return (nRetVal); \
00131 value = Translator::GetFromValue(val); \
00132 return XN_STATUS_OK; \
00133 } \
00134 };
00135
00141 #define XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR(Type, ClassName, Translator) \
00142 XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(, Type, ClassName, Translator)
00143
00148 #define XN_DECLARE_THREAD_SAFE_QUEUE_DECL(decl, Type, ClassName) \
00149 XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, Type, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
00150 XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(decl, Type, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName))
00151
00155 #define XN_DECLARE_THREAD_SAFE_QUEUE(Type, ClassName) \
00156 XN_DECLARE_THREAD_SAFE_QUEUE_DECL(, Type, ClassName)
00157
00158 #endif //__XN_THREAD_SAFE_QUEUE_H__