2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <sys/resource.h>
22 #include <sys/types.h>
28 #include "pbd/compose.h"
29 #include "pbd/file_manager.h"
30 #include "pbd/debug.h"
35 FileManager
* FileDescriptor::_manager
;
37 FileManager::FileManager ()
41 int const r
= getrlimit (RLIMIT_NOFILE
, &rl
);
43 /* XXX: this is a bit arbitrary */
45 _max_open
= rl
.rlim_cur
- 64;
50 DEBUG_TRACE (DEBUG::FileManager
, string_compose ("FileManager can open up to %1 files.\n", _max_open
));
54 FileManager::add (FileDescriptor
* d
)
56 Glib::Mutex::Lock
lm (_mutex
);
60 /** @return true on error, otherwise false */
62 FileManager::allocate (FileDescriptor
* d
)
64 Glib::Mutex::Lock
lm (_mutex
);
68 /* this file needs to be opened */
70 if (_open
== _max_open
) {
72 /* We already have the maximum allowed number of files opened, so we must try to close one.
73 Find the unallocated, open file with the lowest last_used time.
76 double lowest_last_used
= DBL_MAX
;
77 list
<FileDescriptor
*>::iterator oldest
= _files
.end ();
79 for (list
<FileDescriptor
*>::iterator i
= _files
.begin(); i
!= _files
.end(); ++i
) {
80 if ((*i
)->is_open() && (*i
)->_refcount
== 0) {
81 if ((*i
)->_last_used
< lowest_last_used
) {
82 lowest_last_used
= (*i
)->_last_used
;
88 if (oldest
== _files
.end()) {
89 /* no unallocated and open files exist, so there's nothing we can do */
97 "closed file for %1 to release file handle; now have %2 of %3 open\n",
98 (*oldest
)->_name
, _open
, _max_open
104 DEBUG_TRACE (DEBUG::FileManager
, string_compose ("open of %1 failed.\n", d
->_name
));
110 DEBUG_TRACE (DEBUG::FileManager
, string_compose ("opened file for %1; now have %2 of %3 open.\n", d
->_name
, _open
, _max_open
));
114 clock_gettime (CLOCK_MONOTONIC
, &t
);
115 d
->_last_used
= t
.tv_sec
+ (double) t
.tv_nsec
/ 10e9
;
122 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
124 FileManager::release (FileDescriptor
* d
)
126 Glib::Mutex::Lock
lm (_mutex
);
129 assert (d
->_refcount
>= 0);
132 /** Remove a file from our lists. It will be closed if it is currently open. */
134 FileManager::remove (FileDescriptor
* d
)
136 Glib::Mutex::Lock
lm (_mutex
);
142 string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d
->_name
, _open
, _max_open
)
150 FileManager::close (FileDescriptor
* d
)
152 /* we must have a lock on our mutex */
155 d
->Closed (); /* EMIT SIGNAL */
159 FileDescriptor::FileDescriptor (string
const & n
, bool w
)
169 FileDescriptor::manager ()
172 _manager
= new FileManager
;
178 /** Release a previously allocated handle to this file */
180 FileDescriptor::release ()
182 manager()->release (this);
187 /** @param n Filename.
188 * @param w true to open writeable, otherwise false.
189 * @param m Open mode for the file.
192 FdFileDescriptor::FdFileDescriptor (string
const & n
, bool w
, mode_t m
)
193 : FileDescriptor (n
, w
)
197 manager()->add (this);
200 FdFileDescriptor::~FdFileDescriptor ()
202 manager()->remove (this);
206 FdFileDescriptor::is_open () const
208 /* we must have a lock on the FileManager's mutex */
214 FdFileDescriptor::open ()
216 /* we must have a lock on the FileManager's mutex */
218 _fd
= ::open (_name
.c_str(), _writeable
? (O_RDWR
| O_CREAT
) : O_RDONLY
, _mode
);
223 FdFileDescriptor::close ()
225 /* we must have a lock on the FileManager's mutex */
231 /** @return fd, or -1 on error */
233 FdFileDescriptor::allocate ()
235 bool const f
= manager()->allocate (this);
240 /* this is ok thread-wise because allocate () has incremented
241 the Descriptor's refcount, so the file will not be closed
247 /** @param n Filename.
248 * @param w true to open writeable, otherwise false.
251 StdioFileDescriptor::StdioFileDescriptor (string
const & n
, std::string
const & m
)
252 : FileDescriptor (n
, false)
256 manager()->add (this);
259 StdioFileDescriptor::~StdioFileDescriptor ()
261 manager()->remove (this);
265 StdioFileDescriptor::is_open () const
267 /* we must have a lock on the FileManager's mutex */
273 StdioFileDescriptor::open ()
275 /* we must have a lock on the FileManager's mutex */
277 _file
= fopen (_name
.c_str(), _mode
.c_str());
282 StdioFileDescriptor::close ()
284 /* we must have a lock on the FileManager's mutex */
290 /** @return FILE*, or 0 on error */
292 StdioFileDescriptor::allocate ()
294 bool const f
= manager()->allocate (this);
299 /* this is ok thread-wise because allocate () has incremented
300 the Descriptor's refcount, so the file will not be closed