xrootd
XrdClSyncQueue.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_SYNC_QUEUE_HH__
20 #define __XRD_CL_SYNC_QUEUE_HH__
21 
22 #include <queue>
23 
24 #include "XrdSys/XrdSysPthread.hh"
25 #include "XrdCl/XrdClUglyHacks.hh"
26 
27 namespace XrdCl
28 {
29  //----------------------------------------------------------------------------
31  //----------------------------------------------------------------------------
32  template <typename Item>
33  class SyncQueue
34  {
35  public:
36  //------------------------------------------------------------------------
38  //------------------------------------------------------------------------
40  {
41  pSem = new Semaphore(0);
42  };
43 
44  //------------------------------------------------------------------------
46  //------------------------------------------------------------------------
48  {
49  delete pSem;
50  }
51 
52  //------------------------------------------------------------------------
54  //------------------------------------------------------------------------
55  void Put( const Item &item )
56  {
57  XrdSysMutexHelper scopedLock( pMutex );
58  pQueue.push( item );
59  pSem->Post();
60  }
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  Item Get()
66  {
67  pSem->Wait();
68  XrdSysMutexHelper scopedLock( pMutex );
69 
70  // this is not possible, so when it happens we commit a suicide
71  if( pQueue.empty() )
72  abort();
73 
74  Item i = pQueue.front();
75  pQueue.pop();
76  return i;
77  }
78 
79  //------------------------------------------------------------------------
81  //------------------------------------------------------------------------
82  void Clear()
83  {
84  XrdSysMutexHelper scopedLock( pMutex );
85  while( !pQueue.empty() )
86  pQueue.pop();
87  delete pSem;
88  pSem = new Semaphore(0);
89  }
90 
91  //------------------------------------------------------------------------
93  //------------------------------------------------------------------------
94  bool IsEmpty()
95  {
96  XrdSysMutexHelper scopedLock( pMutex );
97  return pQueue.empty();
98  }
99 
100  protected:
101  std::queue<Item> pQueue;
104  };
105 }
106 
107 #endif // __XRD_CL_ANY_OBJECT_HH__
A synchronized queue.
Definition: XrdClSyncQueue.hh:34
bool IsEmpty()
Check if the queue is empty.
Definition: XrdClSyncQueue.hh:94
void Put(const Item &item)
Put the item in the queue.
Definition: XrdClSyncQueue.hh:55
void Clear()
Clear the queue.
Definition: XrdClSyncQueue.hh:82
XrdSysMutex pMutex
Definition: XrdClSyncQueue.hh:102
SyncQueue()
Constructor.
Definition: XrdClSyncQueue.hh:39
Semaphore * pSem
Definition: XrdClSyncQueue.hh:103
~SyncQueue()
Destructor.
Definition: XrdClSyncQueue.hh:47
std::queue< Item > pQueue
Definition: XrdClSyncQueue.hh:101
Item Get()
Get the item from the front of the queue.
Definition: XrdClSyncQueue.hh:65
Definition: XrdSysPthread.hh:263
Definition: XrdSysPthread.hh:166
Definition: XrdSysPthread.hh:406
void Wait()
Definition: XrdSysPthread.hh:421
void Post()
Definition: XrdSysPthread.hh:417
Definition: XrdClAnyObject.hh:26
XrdSysSemaphore Semaphore
Definition: XrdClUglyHacks.hh:36