00001 #ifndef __SYS_SEMWAIT__ 00002 #define __SYS_SEMWAIT__ 00003 00004 /******************************************************************************/ 00005 /* X r d S y s S e m W a i t */ 00006 /* */ 00007 /* Author: Fabrizio Furano (INFN, 2005) */ 00008 /* */ 00009 /* This file is part of the XRootD software suite. */ 00010 /* */ 00011 /* XRootD is free software: you can redistribute it and/or modify it under */ 00012 /* the terms of the GNU Lesser General Public License as published by the */ 00013 /* Free Software Foundation, either version 3 of the License, or (at your */ 00014 /* option) any later version. */ 00015 /* */ 00016 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00017 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00018 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00019 /* License for more details. */ 00020 /* */ 00021 /* You should have received a copy of the GNU Lesser General Public License */ 00022 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00023 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00024 /* */ 00025 /* The copyright holder's institutional names and contributor's names may not */ 00026 /* be used to endorse or promote products derived from this software without */ 00027 /* specific prior written permission of the institution or contributor. */ 00028 /* */ 00029 /* A counting semaphore with timed out wait primitive */ 00030 /******************************************************************************/ 00031 00032 #include "XrdSys/XrdSysPthread.hh" 00033 00034 class XrdSysSemWait { 00035 public: 00036 00037 int CondWait() { 00038 00039 int rc = 0; 00040 // Wait until the sempahore value is positive. This will not be starvation 00041 // free is the OS implements an unfair mutex; 00042 // Returns 0 if signalled, non-0 if would block 00043 // 00044 00045 semVar.Lock(); 00046 if (semVal > 0) semVal--; 00047 else { 00048 rc = 1; 00049 } 00050 00051 semVar.UnLock(); 00052 00053 return rc; 00054 00055 }; 00056 00057 void Post() { 00058 // Add one to the semaphore counter. If we the value is > 0 and there is a 00059 // thread waiting for the sempagore, signal it to get the semaphore. 00060 // 00061 semVar.Lock(); 00062 00063 if (semWait > 0) { 00064 semVar.Signal(); 00065 semWait--; 00066 } 00067 else 00068 semVal++; 00069 00070 semVar.UnLock(); 00071 }; 00072 00073 void Wait() { 00074 // Wait until the sempahore value is positive. This will not be starvation 00075 // free is the OS implements an unfair mutex; 00076 // 00077 00078 semVar.Lock(); 00079 if (semVal > 0) semVal--; 00080 else { 00081 semWait++; 00082 semVar.Wait(); 00083 } 00084 00085 semVar.UnLock(); 00086 00087 }; 00088 00089 int Wait(int secs) { 00090 int rc = 0; 00091 // Wait until the sempahore value is positive. This will not be starvation 00092 // free is the OS implements an unfair mutex; 00093 // Returns 0 if signalled, non-0 if timeout 00094 // 00095 00096 semVar.Lock(); 00097 if (semVal > 0) semVal--; 00098 else { 00099 semWait++; 00100 rc = semVar.Wait(secs); 00101 if (rc) semWait--; 00102 } 00103 00104 semVar.UnLock(); 00105 00106 return rc; 00107 }; 00108 00109 XrdSysSemWait(int semval=1,const char *cid=0) : semVar(0, cid) { 00110 semVal = semval; semWait = 0; 00111 } 00112 00113 ~XrdSysSemWait() {} 00114 00115 private: 00116 00117 XrdSysCondVar semVar; 00118 int semVal; 00119 int semWait; 00120 }; 00121 00122 00123 00124 #endif