XRootD
XrdPosixObject Class Reference

#include <XrdPosixObject.hh>

+ Inheritance diagram for XrdPosixObject:
+ Collaboration diagram for XrdPosixObject:

Public Member Functions

 XrdPosixObject ()
 
virtual ~XrdPosixObject ()
 
bool AssignFD (bool isStream=false)
 
int FDNum ()
 
XrdOucECMsg getECMsg () const
 
void Lock (bool wr=true)
 
void Ref ()
 
int Refs ()
 
void UnLock ()
 
void unRef ()
 
virtual bool Who (XrdPosixDir **dirP)
 
virtual bool Who (XrdPosixFile **fileP)
 

Static Public Member Functions

static bool CanStream ()
 
static XrdPosixDirDir (int fildes, bool glk=false)
 
static XrdPosixFileFile (int fildes, bool glk=false)
 
static int Init (int numfd)
 
static void Release (XrdPosixObject *oP, bool needlk=true)
 
static XrdPosixDirReleaseDir (int fildes)
 
static XrdPosixFileReleaseFile (int fildes)
 
static void Shutdown ()
 
static bool Valid (int fd)
 

Public Attributes

XrdOucECMsg ecMsg
 
std::mutex ecMutex
 

Protected Attributes

int fdNum
 
XrdSysRWLock objMutex
 
int refCnt
 
XrdSysRecMutex updMutex
 

Detailed Description

Definition at line 44 of file XrdPosixObject.hh.

Constructor & Destructor Documentation

◆ XrdPosixObject()

XrdPosixObject::XrdPosixObject ( )
inline

Definition at line 95 of file XrdPosixObject.hh.

95 : fdNum(-1),refCnt(0),ecMsg("[posix]") {}
XrdOucECMsg ecMsg

◆ ~XrdPosixObject()

virtual XrdPosixObject::~XrdPosixObject ( )
inlinevirtual

Definition at line 96 of file XrdPosixObject.hh.

96 {if (fdNum >= 0) Release(this);}
static void Release(XrdPosixObject *oP, bool needlk=true)

References fdNum, and Release().

+ Here is the call graph for this function:

Member Function Documentation

◆ AssignFD()

bool XrdPosixObject::AssignFD ( bool  isStream = false)

Definition at line 67 of file XrdPosixObject.cc.

68 {
69  XrdSysMutexHelper fdHelper(fdMutex);
70  int fd;
71 
72 // Obtain a new filedscriptor from the system. Use the fd to track the object.
73 // Streams are not supported for virtual file descriptors.
74 //
75  if (baseFD)
76  { if (isStream) return 0;
77  for (fd = freeFD; fd < posxFD && myFiles[fd]; fd++) {}
78  if (fd >= posxFD) return 0;
79  freeFD = fd+1;
80  } else {
81  do{if ((fd = dup(devNull)) < 0) return false;
82  if (fd >= lastFD || (isStream && fd > 255))
83  {close(fd); return 0;}
84  if (!myFiles[fd]) break;
85  DMSG("AssignFD", "FD " <<fd <<" closed outside of XrdPosix!");
86  } while(1);
87  }
88 
89 // Enter object in out vector of objects and assign it the FD
90 //
91  myFiles[fd] = this;
92  if (fd > highFD) highFD = fd;
93  fdNum = fd + baseFD;
94 
95 // All done.
96 //
97  return true;
98 }
#define DMSG(x, y)
#define close(a)
Definition: XrdPosix.hh:48

References close, DMSG, and fdNum.

Referenced by XrdPosixXrootd::Opendir().

+ Here is the caller graph for this function:

◆ CanStream()

static bool XrdPosixObject::CanStream ( )
inlinestatic

Definition at line 50 of file XrdPosixObject.hh.

50 {return baseFD == 0 && freeFD < 255;}

◆ Dir()

XrdPosixDir * XrdPosixObject::Dir ( int  fildes,
bool  glk = false 
)
static

Definition at line 104 of file XrdPosixObject.cc.

105 {
106  XrdPosixDir *dP;
107  XrdPosixObject *oP;
108  int waitCount = 0;
109  bool haveLock;
110 
111 // Validate the fildes
112 //
113 do{if (fd >= lastFD || fd < baseFD)
114  {errno = EBADF; return (XrdPosixDir *)0;}
115 
116 // Obtain the file object, if any
117 //
118  fdMutex.Lock();
119  if (!(oP = myFiles[fd - baseFD]) || !(oP->Who(&dP)))
120  {fdMutex.UnLock(); errno = EBADF; return (XrdPosixDir *)0;}
121 
122 // Attempt to lock the object in the appropriate mode. If we fail, then we need
123 // to retry this after dropping the global lock. We pause a bit to let the
124 // current lock holder a chance to unlock the lock. We only do this a limited
125 // amount of time (1 minute) so that we don't get stuck here forever.
126 //
127  if (glk) haveLock = oP->objMutex.CondWriteLock();
128  else haveLock = oP->objMutex.CondReadLock();
129  if (!haveLock)
130  {fdMutex.UnLock();
131  waitCount++;
132  if (waitCount > 120) break;
133  XrdSysTimer::Wait(500); // We wait 500 milliseconds
134  continue;
135  }
136 
137 // If the global lock is to be held, then release the object lock as this
138 // is a call to destroy the object and there is no need for the local lock.
139 //
140  if (glk) oP->UnLock();
141  else fdMutex.UnLock();
142  return dP;
143  } while(1);
144 
145 // If we get here then we timedout waiting for the object lock
146 //
147  errno = ETIMEDOUT;
148  return (XrdPosixDir *)0;
149 }
XrdSysRWLock objMutex
virtual bool Who(XrdPosixDir **dirP)
static void Wait(int milliseconds)
Definition: XrdSysTimer.cc:227

References XrdSysRWLock::CondReadLock(), XrdSysRWLock::CondWriteLock(), XrdSysMutex::Lock(), objMutex, UnLock(), XrdSysMutex::UnLock(), XrdSysTimer::Wait(), and Who().

Referenced by XrdPosixXrootd::isXrootdDir(), XrdPosixXrootd::QueryError(), XrdPosixXrootd::Readdir64(), XrdPosixXrootd::Readdir64_r(), ReleaseDir(), XrdPosixXrootd::Rewinddir(), XrdPosixXrootd::Seekdir(), XrdPosixXrootd::StatRet(), and XrdPosixXrootd::Telldir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ FDNum()

int XrdPosixObject::FDNum ( )
inline

Definition at line 56 of file XrdPosixObject.hh.

56 {return fdNum;}

References fdNum.

◆ File()

XrdPosixFile * XrdPosixObject::File ( int  fildes,
bool  glk = false 
)
static

Definition at line 155 of file XrdPosixObject.cc.

156 {
157  XrdPosixFile *fP;
158  XrdPosixObject *oP;
159  int waitCount = 0;
160  bool haveLock;
161 
162 // Validate the fildes
163 //
164 do{if (fd >= lastFD || fd < baseFD)
165  {errno = EBADF; return (XrdPosixFile *)0;}
166 
167 // Obtain the file object, if any
168 //
169  fdMutex.Lock();
170  if (!(oP = myFiles[fd - baseFD]) || !(oP->Who(&fP)))
171  {fdMutex.UnLock(); errno = EBADF; return (XrdPosixFile *)0;}
172 
173 // Attempt to lock the object in the appropriate mode. If we fail, then we need
174 // to retry this after dropping the global lock. We pause a bit to let the
175 // current lock holder a chance to unlock the lock. We only do this a limited
176 // amount of time (1 minute) so that we don't get stuck here forever.
177 //
178  if (glk) haveLock = oP->objMutex.CondWriteLock();
179  else haveLock = oP->objMutex.CondReadLock();
180  if (!haveLock)
181  {fdMutex.UnLock();
182  waitCount++;
183  if (waitCount > 120) break;
184  XrdSysTimer::Wait(500); // We wait 500 milliseconds
185  continue;
186  }
187 
188 // If the global lock is to be held, then release the object lock as this
189 // is a call to destroy the object and there is no need for the local lock.
190 //
191  if (glk) oP->UnLock();
192  else fdMutex.UnLock();
193  return fP;
194  } while(1);
195 
196 // If we get here then we timedout waiting for the object lock
197 //
198  errno = ETIMEDOUT;
199  return (XrdPosixFile *)0;
200 }

References XrdSysRWLock::CondReadLock(), XrdSysRWLock::CondWriteLock(), XrdSysMutex::Lock(), objMutex, UnLock(), XrdSysMutex::UnLock(), XrdSysTimer::Wait(), and Who().

Referenced by XrdPosixXrootd::endPoint(), XrdPosixXrootd::Fstat(), XrdPosixXrootd::Fsync(), XrdPosixXrootd::Ftruncate(), XrdPosixXrootd::Lseek(), XrdPosixExtra::pgRead(), XrdPosixExtra::pgWrite(), XrdPosixXrootd::Pread(), XrdPosixXrootd::Pwrite(), XrdPosixXrootd::QueryError(), XrdPosixXrootd::Read(), ReleaseFile(), XrdPosixXrootd::VRead(), and XrdPosixXrootd::Write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getECMsg()

XrdOucECMsg XrdPosixObject::getECMsg ( ) const

Definition at line 203 of file XrdPosixObject.cc.

203  {
204  std::unique_lock lock(ecMutex);
205  return ecMsg;
206 }
std::mutex ecMutex

References ecMsg, and ecMutex.

Referenced by XrdPosixXrootd::QueryError().

+ Here is the caller graph for this function:

◆ Init()

int XrdPosixObject::Init ( int  numfd)
static

Definition at line 212 of file XrdPosixObject.cc.

213 {
214  static const int maxFD = 1048576;
215  struct rlimit rlim;
216  int isize, limfd;
217 
218 // Initialize the /dev/null file descriptors, bail if we cannot
219 //
220  devNull = open("/dev/null", O_RDWR, 0744);
221  if (devNull < 0) return -1;
222 
223 // Obtain the file descriptor limit but be careful of infinity
224 //
225  if (getrlimit(RLIMIT_NOFILE, &rlim)) limfd = maxFD;
226  else {if (rlim.rlim_max == RLIM_INFINITY || (int)rlim.rlim_max > maxFD)
227  {rlim.rlim_cur = maxFD;
228  setrlimit(RLIMIT_NOFILE, &rlim);
229  } else {
230  if (rlim.rlim_cur != rlim.rlim_max)
231  {rlim.rlim_cur = rlim.rlim_max;
232  setrlimit(RLIMIT_NOFILE, &rlim);
233  }
234  }
235  limfd = static_cast<int>(rlim.rlim_cur);
236  }
237 
238 // Compute size of table. if the passed fdnum is negative then the caller does
239 // not want us to shadow fd's (ther caller promises to be honest). Otherwise,
240 // the actual fdnum limit will be based on the current limit.
241 //
242  if (fdnum < 0) {posxFD = fdnum = -fdnum; baseFD = limfd;}
243  else fdnum = limfd;
244  isize = fdnum * sizeof(XrdPosixFile *);
245 
246 // Allocate the table for fd-type pointers
247 //
248  if (!(myFiles = (XrdPosixObject **)malloc(isize))) lastFD = -1;
249  else {memset((void *)myFiles, 0, isize); lastFD = fdnum+baseFD;}
250 
251 // All done
252 //
253  return baseFD;
254 }
int open(const char *path, int oflag,...)
int devNull
Definition: XrdGlobals.cc:55

References XrdGlobal::devNull, and open().

Referenced by XrdPosixXrootd::XrdPosixXrootd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Lock()

void XrdPosixObject::Lock ( bool  wr = true)
inline

Definition at line 62 of file XrdPosixObject.hh.

63  {if (wr) objMutex.WriteLock();
64  else objMutex.ReadLock();
65  }

References objMutex, XrdSysRWLock::ReadLock(), and XrdSysRWLock::WriteLock().

+ Here is the call graph for this function:

◆ Ref()

void XrdPosixObject::Ref ( )
inline

Definition at line 67 of file XrdPosixObject.hh.

70  }
#define AtomicInc(x)
#define AtomicBeg(Mtx)
#define AtomicEnd(Mtx)
XrdSysRecMutex updMutex

References AtomicBeg, AtomicEnd, AtomicInc, refCnt, and updMutex.

Referenced by XrdPosixXrootd::Close(), XrdPosixXrootd::Fsync(), XrdPosixObjGuard::Init(), XrdPosixExtra::pgRead(), XrdPosixFile::pgRead(), XrdPosixFile::pgWrite(), XrdPosixExtra::pgWrite(), XrdPosixXrootd::Pread(), XrdPosixXrootd::Pwrite(), XrdPosixFile::Read(), XrdPosixFile::ReadV(), XrdPosixFile::Stat(), XrdPosixFile::Sync(), XrdPosixFile::Trunc(), XrdPosixXrootd::VRead(), and XrdPosixFile::Write().

+ Here is the caller graph for this function:

◆ Refs()

int XrdPosixObject::Refs ( )
inline

Definition at line 71 of file XrdPosixObject.hh.

#define AtomicRet(mtx, x)

References AtomicRet, refCnt, and updMutex.

Referenced by XrdPosixXrootd::Close(), and XrdPosixFile::DelayedDestroy().

+ Here is the caller graph for this function:

◆ Release()

void XrdPosixObject::Release ( XrdPosixObject oP,
bool  needlk = true 
)
static

Definition at line 260 of file XrdPosixObject.cc.

261 {
262 // Get the lock if need be
263 //
264  if (needlk) fdMutex.Lock();
265 
266 // Remove the object from the table
267 //
268  if (baseFD)
269  {int myFD = oP->fdNum - baseFD;
270  if (myFD < freeFD) freeFD = myFD;
271  myFiles[myFD] = 0;
272  } else {
273  myFiles[oP->fdNum] = 0;
274  close(oP->fdNum);
275  }
276 
277 // Zorch the object fd and release the global lock
278 //
279  oP->fdNum = -1;
280  fdMutex.UnLock();
281 }

References close, fdNum, XrdSysMutex::Lock(), and XrdSysMutex::UnLock().

Referenced by ~XrdPosixObject(), ReleaseDir(), and ReleaseFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ReleaseDir()

XrdPosixDir * XrdPosixObject::ReleaseDir ( int  fildes)
static

Definition at line 287 of file XrdPosixObject.cc.

288 {
289  XrdPosixDir *dP;
290 
291 // Find the directory object
292 //
293  if (!(dP = Dir(fd, true))) return (XrdPosixDir *)0;
294 
295 // Release it and return the underlying object
296 //
297  Release((XrdPosixObject *)dP, false);
298  return dP;
299 }
static XrdPosixDir * Dir(int fildes, bool glk=false)

References Dir(), and Release().

Referenced by XrdPosixXrootd::Closedir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ReleaseFile()

XrdPosixFile * XrdPosixObject::ReleaseFile ( int  fildes)
static

Definition at line 305 of file XrdPosixObject.cc.

306 {
307  XrdPosixFile *fP;
308 
309 // Find the file object
310 //
311  if (!(fP = File(fd, true))) return (XrdPosixFile *)0;
312 
313 // Release it and return the underlying object
314 //
315  Release((XrdPosixObject *)fP, false);
316  return fP;
317 }
static XrdPosixFile * File(int fildes, bool glk=false)

References File(), and Release().

Referenced by XrdPosixXrootd::Close().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Shutdown()

void XrdPosixObject::Shutdown ( )
static

Definition at line 323 of file XrdPosixObject.cc.

324 {
325  XrdPosixObject *oP;
326  int i;
327 
328 // Destroy all files and static data
329 //
330  fdMutex.Lock();
331  if (myFiles)
332  {for (i = 0; i <= highFD; i++)
333  if ((oP = myFiles[i]))
334  {myFiles[i] = 0;
335  if (oP->fdNum >= 0) close(oP->fdNum);
336  oP->fdNum = -1;
337  delete oP;
338  };
339  free(myFiles); myFiles = 0;
340  }
341  fdMutex.UnLock();
342 }

References close, fdNum, XrdSysMutex::Lock(), and XrdSysMutex::UnLock().

Referenced by XrdPosixXrootd::~XrdPosixXrootd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ UnLock()

void XrdPosixObject::UnLock ( )
inline

Definition at line 85 of file XrdPosixObject.hh.

85 {objMutex.UnLock();}

References objMutex, and XrdSysRWLock::UnLock().

Referenced by Dir(), XrdPosixXrootd::endPoint(), File(), XrdPosixXrootd::Fstat(), XrdPosixXrootd::Fsync(), XrdPosixXrootd::Ftruncate(), XrdPosixXrootd::isXrootdDir(), XrdPosixXrootd::Lseek(), XrdPosixExtra::pgRead(), XrdPosixExtra::pgWrite(), XrdPosixXrootd::Pread(), XrdPosixXrootd::Pwrite(), XrdPosixXrootd::Read(), XrdPosixXrootd::Readdir64(), XrdPosixXrootd::Readdir64_r(), XrdPosixXrootd::Rewinddir(), XrdPosixXrootd::Seekdir(), XrdPosixXrootd::Telldir(), XrdPosixXrootd::VRead(), and XrdPosixXrootd::Write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unRef()

void XrdPosixObject::unRef ( )
inline

Definition at line 72 of file XrdPosixObject.hh.

75  }
#define AtomicDec(x)

References AtomicBeg, AtomicDec, AtomicEnd, refCnt, and updMutex.

Referenced by XrdPosixFile::DetachDone(), XrdPosixFileRH::HandleResponse(), XrdPosixObjGuard::Init(), XrdPosixFile::pgRead(), XrdPosixFile::pgWrite(), XrdPosixFile::Read(), XrdPosixFile::ReadV(), XrdPosixObjGuard::Release(), XrdPosixFile::Stat(), XrdPosixFile::Sync(), XrdPosixFile::Trunc(), and XrdPosixFile::Write().

+ Here is the caller graph for this function:

◆ Valid()

static bool XrdPosixObject::Valid ( int  fd)
inlinestatic

Definition at line 87 of file XrdPosixObject.hh.

88  {return fd >= baseFD && fd <= (highFD+baseFD)
89  && myFiles && myFiles[fd-baseFD];}

Referenced by XrdPosixXrootd::myFD().

+ Here is the caller graph for this function:

◆ Who() [1/2]

virtual bool XrdPosixObject::Who ( XrdPosixDir **  dirP)
inlinevirtual

Reimplemented in XrdPosixDir.

Definition at line 91 of file XrdPosixObject.hh.

91 {return false;}

Referenced by Dir(), and File().

+ Here is the caller graph for this function:

◆ Who() [2/2]

virtual bool XrdPosixObject::Who ( XrdPosixFile **  fileP)
inlinevirtual

Reimplemented in XrdPosixFile.

Definition at line 93 of file XrdPosixObject.hh.

93 {return false;}

Member Data Documentation

◆ ecMsg

◆ ecMutex

◆ fdNum

int XrdPosixObject::fdNum
protected

◆ objMutex

XrdSysRWLock XrdPosixObject::objMutex
protected

Definition at line 100 of file XrdPosixObject.hh.

Referenced by Dir(), File(), Lock(), XrdPosixDir::rewind(), and UnLock().

◆ refCnt

int XrdPosixObject::refCnt
protected

Definition at line 102 of file XrdPosixObject.hh.

Referenced by Ref(), Refs(), and unRef().

◆ updMutex


The documentation for this class was generated from the following files: