xrootd
XrdClSyncQueue.hh
Go to the documentation of this file.
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 #include <cassert>
00024 #include "XrdSys/XrdSysPthread.hh"
00025 
00026 namespace XrdCl
00027 {
00028   //----------------------------------------------------------------------------
00030   //----------------------------------------------------------------------------
00031   template <typename Item>
00032   class SyncQueue
00033   {
00034     public:
00035       //------------------------------------------------------------------------
00037       //------------------------------------------------------------------------
00038       SyncQueue()
00039       {
00040         pSem = new XrdSysSemaphore(0);
00041       };
00042 
00043       //------------------------------------------------------------------------
00045       //------------------------------------------------------------------------
00046       ~SyncQueue()
00047       {
00048         delete pSem;
00049       }
00050 
00051       //------------------------------------------------------------------------
00053       //------------------------------------------------------------------------
00054       void Put( const Item &item )
00055       {
00056         XrdSysMutexHelper scopedLock( pMutex );
00057         pQueue.push( item );
00058         pSem->Post();
00059       }
00060 
00061       //------------------------------------------------------------------------
00063       //------------------------------------------------------------------------
00064       Item Get()
00065       {
00066         pSem->Wait();
00067         XrdSysMutexHelper scopedLock( pMutex );
00068         assert( !pQueue.empty() );
00069         Item i = pQueue.front();
00070         pQueue.pop();
00071         return i;
00072       }
00073 
00074       //------------------------------------------------------------------------
00076       //------------------------------------------------------------------------
00077       void Clear()
00078       {
00079         XrdSysMutexHelper scopedLock( pMutex );
00080         while( !pQueue.empty() )
00081           pQueue.pop();
00082         delete pSem;
00083         pSem = new XrdSysSemaphore(0);
00084       }
00085 
00086     private:
00087       std::queue<Item>  pQueue;
00088       XrdSysMutex       pMutex;
00089       XrdSysSemaphore  *pSem;
00090   };
00091 }
00092 
00093 #endif // __XRD_CL_ANY_OBJECT_HH__