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();
119 #elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
121 clock_gettime (CLOCK_MONOTONIC, &t);
122 d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
125 gettimeofday (&now, NULL);
126 d->_last_used = now.tv_sec + (double) now.tv_usec / 10e6;
134 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
136 FileManager::release (FileDescriptor* d)
138 Glib::Threads::Mutex::Lock lm (_mutex);
141 assert (d->_refcount >= 0);
144 /** Remove a file from our lists. It will be closed if it is currently open. */
146 FileManager::remove (FileDescriptor* d)
148 Glib::Threads::Mutex::Lock lm (_mutex);
154 string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_path, _open, _max_open)
162 FileManager::close (FileDescriptor* d)
164 /* we must have a lock on our mutex */
167 d->Closed (); /* EMIT SIGNAL */
171 FileDescriptor::FileDescriptor (string const & n, bool w)
181 FileDescriptor::manager ()
184 _manager = new FileManager;
190 /** Release a previously allocated handle to this file */
192 FileDescriptor::release ()
194 manager()->release (this);
199 /** @param file_name Filename.
200 * @param writeable true to open writeable, otherwise false.
201 * @param mode Open mode for the file.
204 FdFileDescriptor::FdFileDescriptor (string const & file_name, bool writeable, mode_t mode)
205 : FileDescriptor (file_name, writeable)
209 manager()->add (this);
212 FdFileDescriptor::~FdFileDescriptor ()
214 manager()->remove (this);
218 FdFileDescriptor::is_open () const
220 /* we must have a lock on the FileManager's mutex */
226 FdFileDescriptor::open ()
228 /* we must have a lock on the FileManager's mutex */
230 _fd = ::open (_path.c_str(), _writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
235 FdFileDescriptor::close ()
237 /* we must have a lock on the FileManager's mutex */
243 /** @return fd, or -1 on error */
245 FdFileDescriptor::allocate ()
247 bool const f = manager()->allocate (this);
252 /* this is ok thread-wise because allocate () has incremented
253 the Descriptor's refcount, so the file will not be closed
260 FileDescriptor::set_path (const string& p)
265 /** @param file_name Filename.
266 * @param mode Mode to pass to fopen.
269 StdioFileDescriptor::StdioFileDescriptor (string const & file_name, std::string const & mode)
270 : FileDescriptor (file_name, false)
274 manager()->add (this);
277 StdioFileDescriptor::~StdioFileDescriptor ()
279 manager()->remove (this);
283 StdioFileDescriptor::is_open () const
285 /* we must have a lock on the FileManager's mutex */
291 StdioFileDescriptor::open ()
293 /* we must have a lock on the FileManager's mutex */
295 _file = fopen (_path.c_str(), _mode.c_str());
300 StdioFileDescriptor::close ()
302 /* we must have a lock on the FileManager's mutex */
308 /** @return FILE*, or 0 on error */
310 StdioFileDescriptor::allocate ()
312 bool const f = manager()->allocate (this);
317 /* this is ok thread-wise because allocate () has incremented
318 the Descriptor's refcount, so the file will not be closed