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 #include <cassert>
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  assert( !pQueue.empty() );
69  Item i = pQueue.front();
70  pQueue.pop();
71  return i;
72  }
73 
74  //------------------------------------------------------------------------
76  //------------------------------------------------------------------------
77  void Clear()
78  {
79  XrdSysMutexHelper scopedLock( pMutex );
80  while( !pQueue.empty() )
81  pQueue.pop();
82  delete pSem;
83  pSem = new XrdSysSemaphore(0);
84  }
85 
86  private:
87  std::queue<Item> pQueue;
90  };
91 }
92 
93 #endif // __XRD_CL_ANY_OBJECT_HH__