00001 #ifndef __XRDPOSIXOBJECT_HH__ 00002 #define __XRDPOSIXOBJECT_HH__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d P o s i x O b j e c t . h h */ 00006 /* */ 00007 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 00031 /******************************************************************************/ 00032 00033 #include <sys/types.h> 00034 00035 #include "XrdSys/XrdSysAtomics.hh" 00036 #include "XrdSys/XrdSysPthread.hh" 00037 00038 class XrdPosixDir; 00039 class XrdPosixFile; 00040 00041 class XrdPosixObject 00042 { 00043 public: 00044 00045 bool AssignFD(bool isStream=false); 00046 00047 static bool CanStream() {return baseFD == 0 && freeFD < 255;} 00048 00049 static XrdPosixDir *Dir (int fildes, bool glk=false); 00050 00051 static XrdPosixFile *File(int fildes, bool glk=false); 00052 00053 int FDNum() {return fdNum;} 00054 00055 static int Init(int numfd); 00056 00057 void Lock(bool wr=true) 00058 {if (wr) objMutex.WriteLock(); 00059 else objMutex.ReadLock(); 00060 } 00061 00062 void Ref() {AtomicBeg(updMutex); 00063 AtomicInc(refCnt); 00064 AtomicEnd(updMutex); 00065 } 00066 int Refs() {AtomicRet(updMutex, refCnt);} 00067 void unRef() {AtomicBeg(updMutex); 00068 AtomicDec(refCnt); 00069 AtomicEnd(updMutex); 00070 } 00071 00072 static void Release(XrdPosixObject *oP, bool needlk=true); 00073 00074 static XrdPosixDir *ReleaseDir( int fildes); 00075 00076 static XrdPosixFile *ReleaseFile(int fildes); 00077 00078 static void Shutdown(); 00079 00080 void UnLock() {objMutex.UnLock();} 00081 00082 static bool Valid(int fd) 00083 {return fd >= baseFD && fd <= (highFD+baseFD) 00084 && myFiles && myFiles[fd-baseFD];} 00085 00086 virtual bool Who(XrdPosixDir **dirP) {return false;} 00087 00088 virtual bool Who(XrdPosixFile **fileP) {return false;} 00089 00090 XrdPosixObject() : fdNum(-1), refCnt(0) {} 00091 virtual ~XrdPosixObject() {if (fdNum >= 0) Release(this);} 00092 00093 protected: 00094 XrdSysRecMutex updMutex; 00095 XrdSysRWLock objMutex; 00096 int fdNum; 00097 int refCnt; 00098 00099 private: 00100 00101 static XrdSysMutex fdMutex; 00102 static XrdPosixObject **myFiles; 00103 static int lastFD; 00104 static int highFD; 00105 static int baseFD; 00106 static int freeFD; 00107 static int posxFD; 00108 static int devNull; 00109 }; 00110 #endif