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 <string.h>
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 
82 
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 XrdXrootdMonitor;
97 class XrdXrootdPgwFob;
98 
100 {
101 public:
102 
103 XrdSfsFile *XrdSfsp; // -> Actual file object
104 union {char *mmAddr; // Memory mapped location, if any
105  unsigned
106  long long cbArg; // Callback argument upon close()
107  };
108 char *FileKey; // -> File hash name (actual file name now)
109 char FileMode; // 'r' or 'w'
110 bool AsyncMode; // 1 -> if file in async r/w mode
111 bool isMMapped; // 1 -> file is memory mapped
112 bool sfEnabled; // 1 -> file is sendfile enabled
113 union {int fdNum; // File descriptor number if regular file
114  int fHandle; // The file handle upon close()
115  };
116 XrdXrootdPgwFob *pgwFob; // Pgw freight pointer
117 XrdXrootdFileHP *fhProc; // File handle processor (set at close time)
118 const char *ID; // File user
119 
120 XrdXrootdFileStats Stats; // File access statistics
121 
122 static void Init(XrdXrootdFileLock *lp, XrdSysError *erP, int sfok);
123 
124  XrdXrootdFile(const char *id, const char *path, XrdSfsFile *fp,
125  char mode='r', bool async=false, struct stat *sP=0);
126  ~XrdXrootdFile();
127 
128 private:
129 int bin2hex(char *outbuff, char *inbuff, int inlen);
131 static int sfOK;
132 static const char *TraceID;
133 };
134 
135 /******************************************************************************/
136 /* X r d X r o o t d F i l e T a b l e */
137 /******************************************************************************/
138 
139 // The before define the structure of the file table. We will have FTABSIZE
140 // internal table entries. We will then provide an external linear table
141 // that increases by FTABSIZE entries. There is one file table per link and
142 // it is owned by the base protocol object.
143 //
144 #define XRD_FTABSIZE 16
145 
146 // WARNING! Manipulation (i.e., Add/Del/delete) of this object must be
147 // externally serialized at the link level. Only one thread
148 // may be active w.r.t this object during manipulation!
149 //
151 {
152 public:
153 
154  int Add(XrdXrootdFile *fp);
155 
156  XrdXrootdFile *Del(XrdXrootdMonitor *monP, int fnum, bool dodel=true);
157 
158 inline XrdXrootdFile *Get(int fnum)
159  {if (fnum >= 0)
160  {if (fnum < XRD_FTABSIZE) return FTab[fnum];
161  if (XTab && (fnum-XRD_FTABSIZE)<XTnum)
162  return XTab[fnum-XRD_FTABSIZE];
163  }
164  return (XrdXrootdFile *)0;
165  }
166 
167  void Recycle(XrdXrootdMonitor *monP);
168 
169  XrdXrootdFileTable(unsigned int mid=0) : fhProc(0), FTfree(0), monID(mid),
170  XTab(0), XTnum(0), XTfree(0)
171  {memset((void *)FTab, 0, sizeof(FTab));}
172 
173 private:
174 
175  ~XrdXrootdFileTable() {} // Always use Recycle() to delete this object!
176 
177 static const char *TraceID;
178 static const char *ID;
180 
182 int FTfree;
183 unsigned int monID;
184 
186 int XTnum;
187 int XTfree;
188 };
189 #endif
void Ref()
Definition: XrdXrootdFile.hh:74
~XrdXrootdFileTable()
Definition: XrdXrootdFile.hh:175
XrdXrootdFile * FTab[XRD_FTABSIZE]
Definition: XrdXrootdFile.hh:181
XrdXrootdPgwFob * pgwFob
Definition: XrdXrootdFile.hh:116
void Delete()
Definition: XrdXrootdFile.hh:59
std::vector< int > fhAvail
Definition: XrdXrootdFile.hh:84
void Recycle(XrdXrootdMonitor *monP)
XrdSysMutex fhMutex
Definition: XrdXrootdFile.hh:83
int fdNum
Definition: XrdXrootdFile.hh:113
int bin2hex(char *outbuff, char *inbuff, int inlen)
bool isMMapped
Definition: XrdXrootdFile.hh:111
Definition: XrdXrootdPgwFob.hh:43
int FTfree
Definition: XrdXrootdFile.hh:182
~XrdXrootdFileHP()
Definition: XrdXrootdFile.hh:81
static int sfOK
Definition: XrdXrootdFile.hh:131
static const char * TraceID
Definition: XrdXrootdFile.hh:177
XrdXrootdFile * Get(int fnum)
Definition: XrdXrootdFile.hh:158
static const char * ID
Definition: XrdXrootdFile.hh:178
Definition: XrdXrootdMonitor.hh:75
Definition: XrdSysError.hh:89
XrdXrootdFile(const char *id, const char *path, XrdSfsFile *fp, char mode='r', bool async=false, struct stat *sP=0)
XrdSfsFile * XrdSfsp
Definition: XrdXrootdFile.hh:103
Definition: XrdXrootdFile.hh:99
Definition: XrdSysPthread.hh:165
Definition: XrdXrootdFile.hh:150
XrdXrootdFileHP * fhProc
Definition: XrdXrootdFile.hh:179
int XTfree
Definition: XrdXrootdFile.hh:187
bool sfEnabled
Definition: XrdXrootdFile.hh:112
char * FileKey
Definition: XrdXrootdFile.hh:108
XrdXrootdFileTable(unsigned int mid=0)
Definition: XrdXrootdFile.hh:169
bool AsyncMode
Definition: XrdXrootdFile.hh:110
#define XRD_FTABSIZE
Definition: XrdXrootdFile.hh:144
int Get()
Definition: XrdXrootdFile.hh:64
XrdXrootdFile * Del(XrdXrootdMonitor *monP, int fnum, bool dodel=true)
Definition: XrdXrootdFileStats.hh:35
int XTnum
Definition: XrdXrootdFile.hh:186
#define stat(a, b)
Definition: XrdPosix.hh:96
void Lock()
Definition: XrdSysPthread.hh:222
XrdXrootdFile ** XTab
Definition: XrdXrootdFile.hh:185
bool noMore
Definition: XrdXrootdFile.hh:86
static XrdXrootdFileLock * Locker
Definition: XrdXrootdFile.hh:130
XrdXrootdFileHP * fhProc
Definition: XrdXrootdFile.hh:117
int fHandle
Definition: XrdXrootdFile.hh:114
static void Init(XrdXrootdFileLock *lp, XrdSysError *erP, int sfok)
unsigned int monID
Definition: XrdXrootdFile.hh:183
int Add(XrdXrootdFile *fp)
void Avail(int fHandle)
Definition: XrdXrootdFile.hh:48
char * mmAddr
Definition: XrdXrootdFile.hh:104
int refs
Definition: XrdXrootdFile.hh:85
Definition: XrdSfsInterface.hh:364
XrdXrootdFileStats Stats
Definition: XrdXrootdFile.hh:120
void UnLock()
Definition: XrdSysPthread.hh:224
char FileMode
Definition: XrdXrootdFile.hh:109
XrdXrootdFileHP(int rsv=2)
Definition: XrdXrootdFile.hh:76
const char * ID
Definition: XrdXrootdFile.hh:118
Definition: XrdXrootdFileLock.hh:32
Definition: XrdXrootdFile.hh:44
unsigned long long cbArg
Definition: XrdXrootdFile.hh:106
static const char * TraceID
Definition: XrdXrootdFile.hh:132