-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdEcUtilities.hh
254 lines (222 loc) · 9.43 KB
/
XrdEcUtilities.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//------------------------------------------------------------------------------
// Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
// Author: Michal Simon <michal.simon@cern.ch>
//------------------------------------------------------------------------------
// This file is part of the XRootD software suite.
//
// 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/>.
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//------------------------------------------------------------------------------
#ifndef SRC_XRDEC_XRDECUTILITIES_HH_
#define SRC_XRDEC_XRDECUTILITIES_HH_
#include "XrdEc/XrdEcObjCfg.hh"
#include "XrdCl/XrdClXRootDResponses.hh"
#include "XrdCl/XrdClFileSystem.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClCheckSumManager.hh"
#include "XrdCl/XrdClUtils.hh"
#include "XrdCks/XrdCksCalc.hh"
#include <exception>
#include <memory>
#include <random>
#include <queue>
#include <mutex>
#include <condition_variable>
namespace XrdEc
{
//---------------------------------------------------------------------------
//! A buffer with stripe data and info on validity
//---------------------------------------------------------------------------
struct stripe_t
{
//-------------------------------------------------------------------------
//! Constructor
//!
//! @param buffer : buffer with stripe data
//! @param valid : true if data are valid, false otherwise
//-------------------------------------------------------------------------
stripe_t( char *buffer, bool valid ) : buffer( buffer ), valid( valid )
{
}
char *buffer; //< buffer with stripe data
bool valid; //< true if data are valid, otherwise false
};
//---------------------------------------------------------------------------
//! All stripes in a block
//---------------------------------------------------------------------------
typedef std::vector<stripe_t> stripes_t;
//----------------------------------------------------------------------------
//! a buffer type
//----------------------------------------------------------------------------
typedef std::vector<char> buffer_t;
//----------------------------------------------------------------------------
//! Generic I/O exception, wraps up XrdCl::XRootDStatus (@see XRootDStatus)
//----------------------------------------------------------------------------
class IOError : public std::exception
{
public:
//------------------------------------------------------------------------
//! Constructor
//!
//! @param st : status
//------------------------------------------------------------------------
IOError( const XrdCl::XRootDStatus &st ) noexcept : st( st ), msg( st.ToString() )
{
}
//------------------------------------------------------------------------
//! Copy constructor
//------------------------------------------------------------------------
IOError( const IOError &err ) noexcept : st( err.st ), msg( err.st.ToString() )
{
}
//------------------------------------------------------------------------
//! Assigment operator
//------------------------------------------------------------------------
IOError& operator=( const IOError &err ) noexcept
{
st = err.st;
msg = err.st.ToString();
return *this;
}
//------------------------------------------------------------------------
//! Destructor
//------------------------------------------------------------------------
virtual ~IOError()
{
}
//------------------------------------------------------------------------
//! overloaded @see std::exception
//------------------------------------------------------------------------
virtual const char* what() const noexcept
{
return msg.c_str();
}
//------------------------------------------------------------------------
//! @return : the status
//------------------------------------------------------------------------
const XrdCl::XRootDStatus& Status() const
{
return st;
}
enum
{
ioTooManyErrors
};
private:
//------------------------------------------------------------------------
//! The status object
//------------------------------------------------------------------------
XrdCl::XRootDStatus st;
//------------------------------------------------------------------------
//! The error message
//------------------------------------------------------------------------
std::string msg;
};
//---------------------------------------------------------------------------
//! A utility function for scheduling read operation handler
//!
//! @param offset : offset of the read
//! @param size : number of bytes read
//! @param buffer : buffer with the data read
//! @param handler : user callback
//---------------------------------------------------------------------------
void ScheduleHandler( uint64_t offset, uint32_t size, void *buffer, XrdCl::ResponseHandler *handler );
//---------------------------------------------------------------------------
//! A utility function for scheduling an operation handler
//!
//! @param handler : user callback
//! @param st : operation status
//---------------------------------------------------------------------------
void ScheduleHandler( XrdCl::ResponseHandler *handler, const XrdCl::XRootDStatus &st = XrdCl::XRootDStatus() );
//---------------------------------------------------------------------------
// A class implementing synchronous queue
//---------------------------------------------------------------------------
template<typename Element>
struct sync_queue
{
//-------------------------------------------------------------------------
// An internal exception used for interrupting the `dequeue` method
//-------------------------------------------------------------------------
struct wait_interrupted{ };
//-------------------------------------------------------------------------
// Default constructor
//-------------------------------------------------------------------------
sync_queue() : interrupted( false )
{
}
//-------------------------------------------------------------------------
// Enqueue new element into the queue
//-------------------------------------------------------------------------
inline void enqueue( Element && element )
{
std::unique_lock<std::mutex> lck( mtx );
elements.push( std::move( element ) );
cv.notify_all();
}
//-------------------------------------------------------------------------
// Dequeue an element from the front of the queue
// Note: if the queue is empty blocks until a new element is enqueued
//-------------------------------------------------------------------------
inline Element dequeue()
{
std::unique_lock<std::mutex> lck( mtx );
while( elements.empty() )
{
cv.wait( lck );
if( interrupted ) throw wait_interrupted();
}
Element element = std::move( elements.front() );
elements.pop();
return std::move( element );
}
//-------------------------------------------------------------------------
// Dequeue an element from the front of the queue
// Note: if the queue is empty returns false, true otherwise
//-------------------------------------------------------------------------
inline bool dequeue( Element &e )
{
std::unique_lock<std::mutex> lck( mtx );
if( elements.empty() ) return false;
e = std::move( elements.front() );
elements.pop();
return true;
}
//-------------------------------------------------------------------------
// Checks if the queue is empty
//-------------------------------------------------------------------------
bool empty()
{
std::unique_lock<std::mutex> lck( mtx );
return elements.empty();
}
//-------------------------------------------------------------------------
// Interrupt all waiting `dequeue` routines
//-------------------------------------------------------------------------
inline void interrupt()
{
interrupted = true;
cv.notify_all();
}
private:
std::queue<Element> elements; //< the queue itself
std::mutex mtx; //< mutex guarding the queue
std::condition_variable cv;
std::atomic<bool> interrupted; //< a flag, true if all `dequeue` routines
//< should be interrupted
};
}
#endif /* SRC_XRDEC_XRDECUTILITIES_HH_ */