OpenNI 1.0.0
|
00001 /***************************************************************************** 00002 * * 00003 * OpenNI 1.0 Alpha * 00004 * Copyright (C) 2010 PrimeSense Ltd. * 00005 * * 00006 * This file is part of OpenNI. * 00007 * * 00008 * OpenNI is free software: you can redistribute it and/or modify * 00009 * it under the terms of the GNU Lesser General Public License as published * 00010 * by the Free Software Foundation, either version 3 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * OpenNI is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public License * 00019 * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. * 00020 * * 00021 *****************************************************************************/ 00022 00023 00024 00025 #ifndef __XN_THREAD_SAFE_QUEUE_H__ 00026 #define __XN_THREAD_SAFE_QUEUE_H__ 00027 00028 //--------------------------------------------------------------------------- 00029 // Includes 00030 //--------------------------------------------------------------------------- 00031 #include <XnQueue.h> 00032 #include <XnOS.h> 00033 00034 //--------------------------------------------------------------------------- 00035 // Types 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 // NOTE: we declare the lock as mutable, as it may change on const methods. 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 /* We do this using Pop() to make sure memory is freed. */ \ 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__