00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2013 by European Organization for Nuclear Research (CERN) 00003 // Author: Lukasz Janyst <ljanyst@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // XRootD is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // XRootD is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00017 //------------------------------------------------------------------------------ 00018 00019 #ifndef __XRD_CL_SYNC_QUEUE_HH__ 00020 #define __XRD_CL_SYNC_QUEUE_HH__ 00021 00022 #include <queue> 00023 00024 #include "XrdSys/XrdSysPthread.hh" 00025 #include "XrdCl/XrdClUglyHacks.hh" 00026 00027 namespace XrdCl 00028 { 00029 //---------------------------------------------------------------------------- 00031 //---------------------------------------------------------------------------- 00032 template <typename Item> 00033 class SyncQueue 00034 { 00035 public: 00036 //------------------------------------------------------------------------ 00038 //------------------------------------------------------------------------ 00039 SyncQueue() 00040 { 00041 pSem = new Semaphore(0); 00042 }; 00043 00044 //------------------------------------------------------------------------ 00046 //------------------------------------------------------------------------ 00047 ~SyncQueue() 00048 { 00049 delete pSem; 00050 } 00051 00052 //------------------------------------------------------------------------ 00054 //------------------------------------------------------------------------ 00055 void Put( const Item &item ) 00056 { 00057 XrdSysMutexHelper scopedLock( pMutex ); 00058 pQueue.push( item ); 00059 pSem->Post(); 00060 } 00061 00062 //------------------------------------------------------------------------ 00064 //------------------------------------------------------------------------ 00065 Item Get() 00066 { 00067 pSem->Wait(); 00068 XrdSysMutexHelper scopedLock( pMutex ); 00069 00070 // this is not possible, so when it happens we commit a suicide 00071 if( pQueue.empty() ) 00072 abort(); 00073 00074 Item i = pQueue.front(); 00075 pQueue.pop(); 00076 return i; 00077 } 00078 00079 //------------------------------------------------------------------------ 00081 //------------------------------------------------------------------------ 00082 void Clear() 00083 { 00084 XrdSysMutexHelper scopedLock( pMutex ); 00085 while( !pQueue.empty() ) 00086 pQueue.pop(); 00087 delete pSem; 00088 pSem = new Semaphore(0); 00089 } 00090 00091 //------------------------------------------------------------------------ 00093 //------------------------------------------------------------------------ 00094 bool IsEmpty() 00095 { 00096 XrdSysMutexHelper scopedLock( pMutex ); 00097 return pQueue.empty(); 00098 } 00099 00100 protected: 00101 std::queue<Item> pQueue; 00102 XrdSysMutex pMutex; 00103 Semaphore *pSem; 00104 }; 00105 } 00106 00107 #endif // __XRD_CL_ANY_OBJECT_HH__