xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
26 namespace XrdCl
27 {
28  //----------------------------------------------------------------------------
30  //----------------------------------------------------------------------------
31  template <typename Item>
32  class SyncQueue
33  {
34  public:
35  //------------------------------------------------------------------------
37  //------------------------------------------------------------------------
39  {
40  pSem = new XrdSysSemaphore(0);
41  };
42 
43  //------------------------------------------------------------------------
45  //------------------------------------------------------------------------
47  {
48  delete pSem;
49  }
50 
51  //------------------------------------------------------------------------
53  //------------------------------------------------------------------------
54  void Put( const Item &item )
55  {
56  XrdSysMutexHelper scopedLock( pMutex );
57  pQueue.push( item );
58  pSem->Post();
59  }
60 
61  //------------------------------------------------------------------------
63  //------------------------------------------------------------------------
64  Item Get()
65  {
66  pSem->Wait();
67  XrdSysMutexHelper scopedLock( pMutex );
68 
69  // this is not possible, so when it happens we commit a suicide
70  if( pQueue.empty() )
71  abort();
72 
73  Item i = pQueue.front();
74  pQueue.pop();
75  return i;
76  }
77 
78  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
81  void Clear()
82  {
83  XrdSysMutexHelper scopedLock( pMutex );
84  while( !pQueue.empty() )
85  pQueue.pop();
86  delete pSem;
87  pSem = new XrdSysSemaphore(0);
88  }
89 
90  //------------------------------------------------------------------------
92  //------------------------------------------------------------------------
93  bool IsEmpty()
94  {
95  XrdSysMutexHelper scopedLock( pMutex );
96  return pQueue.empty();
97  }
98 
99  protected:
100  std::queue<Item> pQueue;
103  };
104 }
105 
106 #endif // __XRD_CL_ANY_OBJECT_HH__
SyncQueue()
Constructor.
Definition: XrdClSyncQueue.hh:38
void Put(const Item &item)
Put the item in the queue.
Definition: XrdClSyncQueue.hh:54
~SyncQueue()
Destructor.
Definition: XrdClSyncQueue.hh:46
XrdSysMutex pMutex
Definition: XrdClSyncQueue.hh:101
XrdSysSemaphore * pSem
Definition: XrdClSyncQueue.hh:102
A synchronized queue.
Definition: XrdClSyncQueue.hh:32
void Wait()
Definition: XrdSysPthread.hh:509
std::queue< Item > pQueue
Definition: XrdClSyncQueue.hh:100
bool IsEmpty()
Check if the queue is empty.
Definition: XrdClSyncQueue.hh:93
void Clear()
Clear the queue.
Definition: XrdClSyncQueue.hh:81
Definition: XrdSysPthread.hh:164
void Post()
Definition: XrdSysPthread.hh:505
Definition: XrdSysPthread.hh:493
Item Get()
Get the item from the front of the queue.
Definition: XrdClSyncQueue.hh:64
Definition: XrdSysPthread.hh:262