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>
29 #include <mach/mach_time.h>
32 #include "pbd/compose.h"
33 #include "pbd/file_manager.h"
34 #include "pbd/debug.h"
39 FileManager* FileDescriptor::_manager;
41 FileManager::FileManager ()
45 int const r = getrlimit (RLIMIT_NOFILE, &rl);
47 /* XXX: this is a bit arbitrary */
49 _max_open = rl.rlim_cur - 64;
54 DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
58 FileManager::add (FileDescriptor* d)
60 Glib::Threads::Mutex::Lock lm (_mutex);
64 /** @return true on error, otherwise false */
66 FileManager::allocate (FileDescriptor* d)
68 Glib::Threads::Mutex::Lock lm (_mutex);
72 /* this file needs to be opened */
74 if (_open == _max_open) {
76 /* We already have the maximum allowed number of files opened, so we must try to close one.
77 Find the unallocated, open file with the lowest last_used time.
80 double lowest_last_used = DBL_MAX;
81 list<FileDescriptor*>::iterator oldest = _files.end ();
83 for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) {
84 if ((*i)->is_open() && (*i)->_refcount == 0) {
85 if ((*i)->_last_used < lowest_last_used) {
86 lowest_last_used = (*i)->_last_used;
92 if (oldest == _files.end()) {
93 /* no unallocated and open files exist, so there's nothing we can do */
101 "closed file for %1 to release file handle; now have %2 of %3 open\n",
102 (*oldest)->_path, _open, _max_open
108 DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_path));
114 DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_path, _open, _max_open));
118 d->_last_used = mach_absolute_time();
121 clock_gettime (CLOCK_MONOTONIC, &t);
122 d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
130 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
132 FileManager::release (FileDescriptor* d)
134 Glib::Threads::Mutex::Lock lm (_mutex);
137 assert (d->_refcount >= 0);
140 /** Remove a file from our lists. It will be closed if it is currently open. */
142 FileManager::remove (FileDescriptor* d)
144 Glib::Threads::Mutex::Lock lm (_mutex);
150 string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_path, _open, _max_open)
158 FileManager::close (FileDescriptor* d)
160 /* we must have a lock on our mutex */
163 d->Closed (); /* EMIT SIGNAL */
167 FileDescriptor::FileDescriptor (string const & n, bool w)
177 FileDescriptor::manager ()
180 _manager = new FileManager;
186 /** Release a previously allocated handle to this file */
188 FileDescriptor::release ()
190 manager()->release (this);
195 /** @param file_name Filename.
196 * @param writeable true to open writeable, otherwise false.
197 * @param mode Open mode for the file.
200 FdFileDescriptor::FdFileDescriptor (string const & file_name, bool writeable, mode_t mode)
201 : FileDescriptor (file_name, writeable)
205 manager()->add (this);
208 FdFileDescriptor::~FdFileDescriptor ()
210 manager()->remove (this);
214 FdFileDescriptor::is_open () const
216 /* we must have a lock on the FileManager's mutex */
222 FdFileDescriptor::open ()
224 /* we must have a lock on the FileManager's mutex */
226 _fd = ::open (_path.c_str(), _writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
231 FdFileDescriptor::close ()
233 /* we must have a lock on the FileManager's mutex */
239 /** @return fd, or -1 on error */
241 FdFileDescriptor::allocate ()
243 bool const f = manager()->allocate (this);
248 /* this is ok thread-wise because allocate () has incremented
249 the Descriptor's refcount, so the file will not be closed
256 FileDescriptor::set_path (const string& p)
261 /** @param file_name Filename.
262 * @param mode Mode to pass to fopen.
265 StdioFileDescriptor::StdioFileDescriptor (string const & file_name, std::string const & mode)
266 : FileDescriptor (file_name, false)
270 manager()->add (this);
273 StdioFileDescriptor::~StdioFileDescriptor ()
275 manager()->remove (this);
279 StdioFileDescriptor::is_open () const
281 /* we must have a lock on the FileManager's mutex */
287 StdioFileDescriptor::open ()
289 /* we must have a lock on the FileManager's mutex */
291 _file = fopen (_path.c_str(), _mode.c_str());
296 StdioFileDescriptor::close ()
298 /* we must have a lock on the FileManager's mutex */
304 /** @return FILE*, or 0 on error */
306 StdioFileDescriptor::allocate ()
308 bool const f = manager()->allocate (this);
313 /* this is ok thread-wise because allocate () has incremented
314 the Descriptor's refcount, so the file will not be closed