Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:43

0001 #ifndef __SYS_SEMWAIT__
0002 #define __SYS_SEMWAIT__
0003 
0004 /******************************************************************************/
0005 /*                       X r d S y s S e m W a i t                            */
0006 /*                                                                            */
0007 /* Author: Fabrizio Furano (INFN, 2005)                                       */
0008 /*                                                                            */
0009 /* This file is part of the XRootD software suite.                            */
0010 /*                                                                            */
0011 /* XRootD is free software: you can redistribute it and/or modify it under    */
0012 /* the terms of the GNU Lesser General Public License as published by the     */
0013 /* Free Software Foundation, either version 3 of the License, or (at your     */
0014 /* option) any later version.                                                 */
0015 /*                                                                            */
0016 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0017 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0018 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0019 /* License for more details.                                                  */
0020 /*                                                                            */
0021 /* You should have received a copy of the GNU Lesser General Public License   */
0022 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0023 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0024 /*                                                                            */
0025 /* The copyright holder's institutional names and contributor's names may not */
0026 /* be used to endorse or promote products derived from this software without  */
0027 /* specific prior written permission of the institution or contributor.       */
0028 /*                                                                            */
0029 /* A counting semaphore with timed out wait primitive                         */
0030 /******************************************************************************/
0031 
0032 #include "XrdSys/XrdSysPthread.hh"
0033 
0034 class XrdSysSemWait {
0035  public:
0036 
0037    int  CondWait() {
0038 
0039       int rc = 0;
0040       // Wait until the sempahore value is positive. This will not be starvation
0041       // free is the OS implements an unfair mutex;
0042       // Returns 0 if signalled, non-0 if would block
0043       //
0044 
0045       semVar.Lock();
0046       if (semVal > 0) semVal--;
0047       else {
0048      rc = 1;
0049       }
0050 
0051       semVar.UnLock();
0052 
0053       return rc;
0054 
0055    };
0056    
0057    void Post() {
0058       // Add one to the semaphore counter. If we the value is > 0 and there is a
0059       // thread waiting for the sempagore, signal it to get the semaphore.
0060       //
0061       semVar.Lock();
0062 
0063       if (semWait > 0) {
0064      semVar.Signal();
0065      semWait--;
0066       }
0067       else
0068      semVal++;
0069       
0070       semVar.UnLock();
0071    };
0072    
0073    void Wait()   {
0074       // Wait until the sempahore value is positive. This will not be starvation
0075       // free is the OS implements an unfair mutex;
0076       //
0077 
0078       semVar.Lock();
0079       if (semVal > 0) semVal--;
0080       else {
0081      semWait++;
0082      semVar.Wait();
0083       }
0084 
0085       semVar.UnLock();
0086 
0087    };
0088 
0089    int Wait(int secs)  {
0090       int rc = 0;
0091       // Wait until the sempahore value is positive. This will not be starvation
0092       // free is the OS implements an unfair mutex;
0093       // Returns 0 if signalled, non-0 if timeout
0094       //
0095 
0096       semVar.Lock();
0097       if (semVal > 0) semVal--;
0098       else {
0099      semWait++;
0100      rc = semVar.Wait(secs);
0101      if (rc) semWait--;
0102       }
0103 
0104       semVar.UnLock();
0105 
0106       return rc;
0107    };
0108 
0109    XrdSysSemWait(int semval=1,const char *cid=0) : semVar(0, cid) {
0110       semVal = semval; semWait = 0;
0111    }
0112 
0113    ~XrdSysSemWait() {}
0114 
0115 private:
0116 
0117 XrdSysCondVar semVar;
0118 int           semVal;
0119 int           semWait;
0120 };
0121 
0122 
0123 
0124 #endif