xrootd
XrdClRequestSync.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 //------------------------------------------------------------------------------
24 
25 #ifndef __XRD_CL_REQUEST_SYNC_HH__
26 #define __XRD_CL_REQUEST_SYNC_HH__
27 
28 #include "XrdSys/XrdSysPthread.hh"
29 #include "XrdCl/XrdClUglyHacks.hh"
30 
31 namespace XrdCl
32 {
33  //----------------------------------------------------------------------------
35  //----------------------------------------------------------------------------
37  {
38  public:
39  //------------------------------------------------------------------------
44  //------------------------------------------------------------------------
45  RequestSync( uint32_t reqTotal, uint32_t reqQuota ):
46  pQuotaSem( new Semaphore( reqQuota ) ),
47  pTotalSem( new Semaphore( 0 ) ),
48  pRequestsLeft( reqTotal ),
49  pFailureCounter( 0 )
50  {
51  if( !reqTotal )
52  pTotalSem->Post();
53  }
54 
55  //------------------------------------------------------------------------
57  //------------------------------------------------------------------------
59  {
60  delete pQuotaSem;
61  delete pTotalSem;
62  }
63 
64  //------------------------------------------------------------------------
66  //------------------------------------------------------------------------
67  void WaitForQuota()
68  {
69  pQuotaSem->Wait();
70  }
71 
72  //------------------------------------------------------------------------
74  //------------------------------------------------------------------------
75  void WaitForAll()
76  {
77  pTotalSem->Wait();
78  }
79 
80  //------------------------------------------------------------------------
82  //------------------------------------------------------------------------
83  void TaskDone( bool success = true )
84  {
85  XrdSysMutexHelper scopedLock( pMutex );
86  if( !success )
88  --pRequestsLeft;
89  pQuotaSem->Post();
90  if( !pRequestsLeft )
91  pTotalSem->Post();
92  }
93 
94  //------------------------------------------------------------------------
96  //------------------------------------------------------------------------
97  uint32_t FailureCount() const
98  {
99  return pFailureCounter;
100  }
101 
102  private:
103  RequestSync(const RequestSync &other);
104  RequestSync &operator = (const RequestSync &other);
105 
109  uint32_t pRequestsLeft;
110  uint32_t pFailureCounter;
111  };
112 }
113 
114 #endif // __XRD_CL_REQUEST_SYNC_HH__
RequestSync(uint32_t reqTotal, uint32_t reqQuota)
Definition: XrdClRequestSync.hh:45
void TaskDone(bool success=true)
Report the request finish.
Definition: XrdClRequestSync.hh:83
uint32_t pFailureCounter
Definition: XrdClRequestSync.hh:110
void Wait()
Definition: XrdSysPthread.hh:421
XrdSysMutex pMutex
Definition: XrdClRequestSync.hh:106
Definition: XrdSysPthread.hh:165
Semaphore * pQuotaSem
Definition: XrdClRequestSync.hh:107
void Post()
Definition: XrdSysPthread.hh:417
Definition: XrdSysPthread.hh:405
~RequestSync()
Destructor.
Definition: XrdClRequestSync.hh:58
Definition: XrdClAnyObject.hh:25
uint32_t FailureCount() const
Number of tasks finishing with an error.
Definition: XrdClRequestSync.hh:97
Semaphore * pTotalSem
Definition: XrdClRequestSync.hh:108
A helper running a fixed number of requests at a given time.
Definition: XrdClRequestSync.hh:36
RequestSync & operator=(const RequestSync &other)
void WaitForAll()
Wait for all the requests to be finished.
Definition: XrdClRequestSync.hh:75
void WaitForQuota()
Wait for the request quota.
Definition: XrdClRequestSync.hh:67
Definition: XrdSysPthread.hh:262
uint32_t pRequestsLeft
Definition: XrdClRequestSync.hh:109