xrootd
XrdClRequestSync.hh
Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
00003 // Author: Lukasz Janyst <ljanyst@cern.ch>
00004 //------------------------------------------------------------------------------
00005 // XRootD is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // XRootD is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
00017 //------------------------------------------------------------------------------
00018 
00019 #ifndef __XRD_CL_REQUEST_SYNC_HH__
00020 #define __XRD_CL_REQUEST_SYNC_HH__
00021 
00022 #include "XrdSys/XrdSysPthread.hh"
00023 
00024 namespace XrdCl
00025 {
00026   //----------------------------------------------------------------------------
00028   //----------------------------------------------------------------------------
00029   class RequestSync
00030   {
00031     public:
00032       //------------------------------------------------------------------------
00037       //------------------------------------------------------------------------
00038       RequestSync( uint32_t reqTotal, uint32_t reqQuota ):
00039         pQuotaSem( new XrdSysSemaphore( reqQuota ) ),
00040         pTotalSem( new XrdSysSemaphore( 0 ) ),
00041         pRequestsLeft( reqTotal ),
00042         pFailureCounter( 0 )
00043       {
00044         if( !reqTotal )
00045           pTotalSem->Post();
00046       }
00047 
00048       //------------------------------------------------------------------------
00050       //------------------------------------------------------------------------
00051       ~RequestSync()
00052       {
00053         delete pQuotaSem;
00054         delete pTotalSem;
00055       }
00056 
00057       //------------------------------------------------------------------------
00059       //------------------------------------------------------------------------
00060       void WaitForQuota()
00061       {
00062         pQuotaSem->Wait();
00063       }
00064 
00065       //------------------------------------------------------------------------
00067       //------------------------------------------------------------------------
00068       void WaitForAll()
00069       {
00070         pTotalSem->Wait();
00071       }
00072 
00073       //------------------------------------------------------------------------
00075       //------------------------------------------------------------------------
00076       void TaskDone( bool success = true )
00077       {
00078         XrdSysMutexHelper scopedLock( pMutex );
00079         if( !success )
00080           ++pFailureCounter;
00081         --pRequestsLeft;
00082         pQuotaSem->Post();
00083         if( !pRequestsLeft )
00084           pTotalSem->Post();
00085       }
00086 
00087       //------------------------------------------------------------------------
00089       //------------------------------------------------------------------------
00090       uint32_t FailureCount() const
00091       {
00092         return pFailureCounter;
00093       }
00094 
00095     private:
00096       XrdSysMutex      pMutex;
00097       XrdSysSemaphore *pQuotaSem;
00098       XrdSysSemaphore *pTotalSem;
00099       uint32_t         pRequestsLeft;
00100       uint32_t         pFailureCounter;
00101   };
00102 }
00103 
00104 #endif // __XRD_CL_REQUEST_SYNC_HH__