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