XRootD
XrdXrootdFile.hh
Go to the documentation of this file.
1 #ifndef _XROOTD_FILE_H_
2 #define _XROOTD_FILE_H_
3 /******************************************************************************/
4 /* */
5 /* X r d X r o o t d F i l e . h h */
6 /* */
7 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <cstring>
33 #include <set>
34 #include <vector>
35 
36 #include "XProtocol/XPtypes.hh"
37 #include "XrdSys/XrdSysPthread.hh"
39 
40 /******************************************************************************/
41 /* X r d X r o o t d F i l e H P */
42 /******************************************************************************/
43 
45 {
46 public:
47 
48 void Avail(int fHandle) {fhMutex.Lock();
49  bool done = (1 == refs--);
50  if (noMore)
51  {fhMutex.UnLock();
52  if (done) delete this;
53  } else {
54  fhAvail.push_back(fHandle);
55  fhMutex.UnLock();
56  }
57  }
58 
59 void Delete() {fhMutex.Lock();
60  if (!refs) {fhMutex.UnLock(); delete this;}
61  else {noMore = true; fhMutex.UnLock();}
62  }
63 
64 int Get() {int fh;
65  fhMutex.Lock();
66  if (fhAvail.empty()) fh = -1;
67  else {fh = fhAvail.back();
68  fhAvail.pop_back();
69  }
70  fhMutex.UnLock();
71  return fh;
72  }
73 
74 void Ref() {fhMutex.Lock(); refs++; fhMutex.UnLock();}
75 
76  XrdXrootdFileHP(int rsv=2) : refs(1), noMore(false)
77  {fhAvail.reserve(rsv);}
78 
79 private:
80 
81  ~XrdXrootdFileHP() {}
82 
83 XrdSysMutex fhMutex;
84 std::vector<int> fhAvail;
85 int refs;
86 bool noMore;
87 };
88 
89 
90 /******************************************************************************/
91 /* X r d X r o o t d F i l e */
92 /******************************************************************************/
93 
94 class XrdSfsFile;
95 class XrdXrootdFileLock;
96 class XrdXrootdAioFob;
97 class XrdXrootdMonitor;
98 class XrdXrootdPgwFob;
99 
101 {
102 public:
103 
104 XrdSfsFile *XrdSfsp; // -> Actual file object
105 union {char *mmAddr; // Memory mapped location, if any
106  unsigned
107  long long cbArg; // Callback argument upon close()
108  };
109 char *FileKey; // -> File hash name (actual file name now)
110 char FileMode; // 'r' or 'w'
111 bool AsyncMode; // 1 -> if file in async r/w mode
112 bool isMMapped; // 1 -> file is memory mapped
113 bool sfEnabled; // 1 -> file is sendfile enabled
114 union {int fdNum; // File descriptor number if regular file
115  int fHandle; // The file handle upon close()
116  };
117 XrdXrootdAioFob *aioFob; // Aio freight pointer for reads
118 XrdXrootdPgwFob *pgwFob; // Pgw freight pointer for writes
119 XrdXrootdFileHP *fhProc; // File handle processor (set at close time)
120 const char *ID; // File user
121 
122 XrdXrootdFileStats Stats; // File access statistics
123 
124 static void Init(XrdXrootdFileLock *lp, XrdSysError *erP, bool sfok);
125 
126  void Ref(int num);
127 
128  void Serialize();
129 
130  XrdXrootdFile(const char *id, const char *path, XrdSfsFile *fp,
131  char mode='r', bool async=false, struct stat *sP=0);
132  ~XrdXrootdFile();
133 
134 private:
135 int bin2hex(char *outbuff, char *inbuff, int inlen);
136 static XrdXrootdFileLock *Locker;
137 static int sfOK;
138 static const char *TraceID;
139 
140 int refCount; // Reference counter
141 int reserved;
142 XrdSysSemaphore *syncWait;
143 XrdSysMutex fileMutex;
144 };
145 
146 /******************************************************************************/
147 /* X r d X r o o t d F i l e T a b l e */
148 /******************************************************************************/
149 
150 // The before define the structure of the file table. We will have FTABSIZE
151 // internal table entries. We will then provide an external linear table
152 // that increases by FTABSIZE entries. There is one file table per link and
153 // it is owned by the base protocol object.
154 //
155 #define XRD_FTABSIZE 16
156 
157 // WARNING! Manipulation (i.e., Add/Del/delete) of this object must be
158 // externally serialized at the link level. Only one thread
159 // may be active w.r.t this object during manipulation!
160 //
162 {
163 public:
164 
165  int Add(XrdXrootdFile *fp);
166 
167  XrdXrootdFile *Del(XrdXrootdMonitor *monP, int fnum, bool dodel=true);
168 
169 inline XrdXrootdFile *Get(int fnum)
170  {if (fnum >= 0)
171  {if (fnum < XRD_FTABSIZE)
172  {if (FTab[fnum] != heldSpotP) return FTab[fnum];
173  } else {
174  if (XTab)
175  {int i = fnum - XRD_FTABSIZE;
176  if (i < XTnum && XTab[i] != heldSpotP)
177  return XTab[i];
178  }
179  }
180  }
181  return (XrdXrootdFile *)0;
182  }
183 
184  void Recycle(XrdXrootdMonitor *monP);
185 
186  XrdXrootdFileTable(unsigned int mid=0) : fhProc(0), FTfree(0), monID(mid),
187  XTab(0), XTnum(0), XTfree(0)
188  {memset((void *)FTab, 0, sizeof(FTab));}
189 
191 
192 private:
193 
194  ~XrdXrootdFileTable() {} // Always use Recycle() to delete this object!
195 
196 static const char *TraceID;
197 static const char *ID;
198 XrdXrootdFileHP *fhProc;
199 
201 int FTfree;
202 unsigned int monID;
203 
204 XrdXrootdFile **XTab;
205 int XTnum;
206 int XTfree;
207 };
208 #endif
int stat(const char *path, struct stat *buf)
#define XRD_FTABSIZE
void Avail(int fHandle)
XrdXrootdFileHP(int rsv=2)
void Recycle(XrdXrootdMonitor *monP)
XrdXrootdFileTable(unsigned int mid=0)
int Add(XrdXrootdFile *fp)
XrdXrootdFile * Get(int fnum)
static XrdXrootdFile * heldSpotP
XrdXrootdFile * Del(XrdXrootdMonitor *monP, int fnum, bool dodel=true)
const char * ID
XrdXrootdFileHP * fhProc
XrdXrootdFile(const char *id, const char *path, XrdSfsFile *fp, char mode='r', bool async=false, struct stat *sP=0)
void Ref(int num)
XrdXrootdPgwFob * pgwFob
static void Init(XrdXrootdFileLock *lp, XrdSysError *erP, bool sfok)
XrdSfsFile * XrdSfsp
XrdXrootdAioFob * aioFob
XrdXrootdFileStats Stats