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