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