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>
27 #include "pbd/compose.h"
28 #include "pbd/file_manager.h"
29 #include "pbd/debug.h"
34 FileManager* FileDescriptor::_manager;
38 /** Class to limit the number of files held open */
44 void add (FileDescriptor *);
45 void remove (FileDescriptor *);
47 void release (FileDescriptor *);
48 bool allocate (FileDescriptor *);
52 void close (FileDescriptor *);
54 std::list<FileDescriptor*> _files; ///< files we know about
55 Glib::Mutex _mutex; ///< mutex for _files, _open and FileDescriptor contents
56 int _open; ///< number of open files
57 int _max_open; ///< maximum number of open files
62 FileManager::FileManager ()
66 int const r = getrlimit (RLIMIT_NOFILE, &rl);
68 /* XXX: this is a bit arbitrary */
70 _max_open = rl.rlim_cur - 64;
75 DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
79 FileManager::add (FileDescriptor* d)
81 Glib::Mutex::Lock lm (_mutex);
85 /** @return true on error, otherwise false */
87 FileManager::allocate (FileDescriptor* d)
89 Glib::Mutex::Lock lm (_mutex);
93 /* this file needs to be opened */
95 if (_open == _max_open) {
97 /* We already have the maximum allowed number of files opened, so we must try to close one.
98 Find the unallocated, open file with the lowest last_used time.
101 double lowest_last_used = DBL_MAX;
102 list<FileDescriptor*>::iterator oldest = _files.end ();
104 for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) {
105 if ((*i)->is_open() && (*i)->_refcount == 0) {
106 if ((*i)->_last_used < lowest_last_used) {
107 lowest_last_used = (*i)->_last_used;
113 if (oldest == _files.end()) {
114 /* no unallocated and open files exist, so there's nothing we can do */
122 "closed file for %1 to release file handle; now have %2 of %3 open\n",
123 (*oldest)->_name, _open, _max_open
129 DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_name));
135 DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_name, _open, _max_open));
139 clock_gettime (CLOCK_MONOTONIC, &t);
140 d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
147 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
149 FileManager::release (FileDescriptor* d)
151 Glib::Mutex::Lock lm (_mutex);
154 assert (d->_refcount >= 0);
157 /** Remove a file from our lists. It will be closed if it is currently open. */
159 FileManager::remove (FileDescriptor* d)
161 Glib::Mutex::Lock lm (_mutex);
167 string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_name, _open, _max_open)
175 FileManager::close (FileDescriptor* d)
177 /* we must have a lock on our mutex */
180 d->Closed (); /* EMIT SIGNAL */
184 FileDescriptor::FileDescriptor (string const & n, bool w)
194 FileDescriptor::manager ()
197 _manager = new FileManager;
203 /** Release a previously allocated handle to this file */
205 FileDescriptor::release ()
207 manager()->release (this);
210 /** @param n Filename.
211 * @param w true to open writeable, otherwise false.
212 * @param i SF_INFO for the file.
215 SndFileDescriptor::SndFileDescriptor (string const & n, bool w, SF_INFO* i)
216 : FileDescriptor (n, w)
220 manager()->add (this);
223 SndFileDescriptor::~SndFileDescriptor ()
225 manager()->remove (this);
228 /** @return SNDFILE*, or 0 on error */
230 SndFileDescriptor::allocate ()
232 bool const f = manager()->allocate (this);
237 /* this is ok thread-wise because allocate () has incremented
238 the Descriptor's refcount, so the file will not be closed
244 SndFileDescriptor::close ()
246 /* we must have a lock on the FileManager's mutex */
253 SndFileDescriptor::is_open () const
255 /* we must have a lock on the FileManager's mutex */
257 return _sndfile != 0;
261 SndFileDescriptor::open ()
263 /* we must have a lock on the FileManager's mutex */
265 _sndfile = sf_open (_name.c_str(), _writeable ? SFM_RDWR : SFM_READ, _info);
266 return (_sndfile == 0);
270 /** @param n Filename.
271 * @param w true to open writeable, otherwise false.
272 * @param m Open mode for the file.
275 FdFileDescriptor::FdFileDescriptor (string const & n, bool w, mode_t m)
276 : FileDescriptor (n, w)
280 manager()->add (this);
283 FdFileDescriptor::~FdFileDescriptor ()
285 manager()->remove (this);
289 FdFileDescriptor::is_open () const
291 /* we must have a lock on the FileManager's mutex */
297 FdFileDescriptor::open ()
299 /* we must have a lock on the FileManager's mutex */
301 _fd = ::open (_name.c_str(), _writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
306 FdFileDescriptor::close ()
308 /* we must have a lock on the FileManager's mutex */
314 /** @return fd, or -1 on error */
316 FdFileDescriptor::allocate ()
318 bool const f = manager()->allocate (this);
323 /* this is ok thread-wise because allocate () has incremented
324 the Descriptor's refcount, so the file will not be closed
330 /** @param n Filename.
331 * @param w true to open writeable, otherwise false.
334 StdioFileDescriptor::StdioFileDescriptor (string const & n, std::string const & m)
335 : FileDescriptor (n, false)
339 manager()->add (this);
342 StdioFileDescriptor::~StdioFileDescriptor ()
344 manager()->remove (this);
348 StdioFileDescriptor::is_open () const
350 /* we must have a lock on the FileManager's mutex */
356 StdioFileDescriptor::open ()
358 /* we must have a lock on the FileManager's mutex */
360 _file = fopen (_name.c_str(), _mode.c_str());
365 StdioFileDescriptor::close ()
367 /* we must have a lock on the FileManager's mutex */
373 /** @return FILE*, or 0 on error */
375 StdioFileDescriptor::allocate ()
377 bool const f = manager()->allocate (this);
382 /* this is ok thread-wise because allocate () has incremented
383 the Descriptor's refcount, so the file will not be closed