xrootd
XrdSysSemWait.hh
Go to the documentation of this file.
1 #ifndef __SYS_SEMWAIT__
2 #define __SYS_SEMWAIT__
3 
4 /******************************************************************************/
5 /* X r d S y s S e m W a i t */
6 /* */
7 /* Author: Fabrizio Furano (INFN, 2005) */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /* */
29 /* A counting semaphore with timed out wait primitive */
30 /******************************************************************************/
31 
32 #include "XrdSys/XrdSysPthread.hh"
33 
35  public:
36 
37  int CondWait() {
38 
39  int rc = 0;
40  // Wait until the sempahore value is positive. This will not be starvation
41  // free is the OS implements an unfair mutex;
42  // Returns 0 if signalled, non-0 if would block
43  //
44 
45  semVar.Lock();
46  if (semVal > 0) semVal--;
47  else {
48  rc = 1;
49  }
50 
51  semVar.UnLock();
52 
53  return rc;
54 
55  };
56 
57  void Post() {
58  // Add one to the semaphore counter. If we the value is > 0 and there is a
59  // thread waiting for the sempagore, signal it to get the semaphore.
60  //
61  semVar.Lock();
62 
63  if (semWait > 0) {
64  semVar.Signal();
65  semWait--;
66  }
67  else
68  semVal++;
69 
70  semVar.UnLock();
71  };
72 
73  void Wait() {
74  // Wait until the sempahore value is positive. This will not be starvation
75  // free is the OS implements an unfair mutex;
76  //
77 
78  semVar.Lock();
79  if (semVal > 0) semVal--;
80  else {
81  semWait++;
82  semVar.Wait();
83  }
84 
85  semVar.UnLock();
86 
87  };
88 
89  int Wait(int secs) {
90  int rc = 0;
91  // Wait until the sempahore value is positive. This will not be starvation
92  // free is the OS implements an unfair mutex;
93  // Returns 0 if signalled, non-0 if timeout
94  //
95 
96  semVar.Lock();
97  if (semVal > 0) semVal--;
98  else {
99  semWait++;
100  rc = semVar.Wait(secs);
101  if (rc) semWait--;
102  }
103 
104  semVar.UnLock();
105 
106  return rc;
107  };
108 
109  XrdSysSemWait(int semval=1,const char *cid=0) : semVar(0, cid) {
110  semVal = semval; semWait = 0;
111  }
112 
114 
115 private:
116 
118 int semVal;
120 };
121 
122 
123 
124 #endif
XrdSysCondVar semVar
Definition: XrdSysSemWait.hh:117
int Wait(int secs)
Definition: XrdSysSemWait.hh:89
void Wait()
Definition: XrdSysSemWait.hh:73
int semWait
Definition: XrdSysSemWait.hh:119
~XrdSysSemWait()
Definition: XrdSysSemWait.hh:113
void Signal()
Definition: XrdSysPthread.hh:59
XrdSysSemWait(int semval=1, const char *cid=0)
Definition: XrdSysSemWait.hh:109
Definition: XrdSysPthread.hh:53
void Post()
Definition: XrdSysSemWait.hh:57
Definition: XrdSysSemWait.hh:34
void UnLock()
Definition: XrdSysPthread.hh:69
int CondWait()
Definition: XrdSysSemWait.hh:37
int semVal
Definition: XrdSysSemWait.hh:118
void Lock()
Definition: XrdSysPthread.hh:57