Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:37

0001 //------------------------------------------------------------------------------
0002 // Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
0003 // Author: Lukasz Janyst <ljanyst@cern.ch>
0004 //------------------------------------------------------------------------------
0005 // XRootD is free software: you can redistribute it and/or modify
0006 // it under the terms of the GNU Lesser General Public License as published by
0007 // the Free Software Foundation, either version 3 of the License, or
0008 // (at your option) any later version.
0009 //
0010 // XRootD is distributed in the hope that it will be useful,
0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 // GNU General Public License for more details.
0014 //
0015 // You should have received a copy of the GNU Lesser General Public License
0016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0017 //------------------------------------------------------------------------------
0018 
0019 #ifndef __XRD_CL_SYNC_QUEUE_HH__
0020 #define __XRD_CL_SYNC_QUEUE_HH__
0021 
0022 #include <queue>
0023 
0024 #include "XrdSys/XrdSysPthread.hh"
0025 
0026 namespace XrdCl
0027 {
0028   //----------------------------------------------------------------------------
0029   //! A synchronized queue
0030   //----------------------------------------------------------------------------
0031   template <typename Item>
0032   class SyncQueue
0033   {
0034     public:
0035       //------------------------------------------------------------------------
0036       //! Constructor
0037       //------------------------------------------------------------------------
0038       SyncQueue()
0039       {
0040         pSem = new XrdSysSemaphore(0);
0041       };
0042 
0043       //------------------------------------------------------------------------
0044       //! Destructor
0045       //------------------------------------------------------------------------
0046       ~SyncQueue()
0047       {
0048         delete pSem;
0049       }
0050 
0051       //------------------------------------------------------------------------
0052       //! Put the item in the queue
0053       //------------------------------------------------------------------------
0054       void Put( const Item &item )
0055       {
0056         XrdSysMutexHelper scopedLock( pMutex );
0057         pQueue.push( item );
0058         pSem->Post();
0059       }
0060 
0061       //------------------------------------------------------------------------
0062       //! Get the item from the front of the queue
0063       //------------------------------------------------------------------------
0064       Item Get()
0065       {
0066         pSem->Wait();
0067         XrdSysMutexHelper scopedLock( pMutex );
0068 
0069         // this is not possible, so when it happens we commit a suicide
0070         if( pQueue.empty() )
0071           abort();
0072 
0073         Item i = pQueue.front();
0074         pQueue.pop();
0075         return i;
0076       }
0077 
0078       //------------------------------------------------------------------------
0079       //! Clear the queue
0080       //------------------------------------------------------------------------
0081       void Clear()
0082       {
0083         XrdSysMutexHelper scopedLock( pMutex );
0084         while( !pQueue.empty() )
0085           pQueue.pop();
0086         delete pSem;
0087         pSem = new XrdSysSemaphore(0);
0088       }
0089 
0090       //------------------------------------------------------------------------
0091       //! Check if the queue is empty
0092       //------------------------------------------------------------------------
0093       bool IsEmpty()
0094       {
0095         XrdSysMutexHelper scopedLock( pMutex );
0096         return pQueue.empty();
0097       }
0098 
0099     protected:
0100       std::queue<Item>  pQueue;
0101       XrdSysMutex       pMutex;
0102       XrdSysSemaphore  *pSem;
0103   };
0104 }
0105 
0106 #endif // __XRD_CL_ANY_OBJECT_HH__