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/types.h>
28 #include <mach/mach_time.h>
31 #include "pbd/compose.h"
32 #include "pbd/file_manager.h"
33 #include "pbd/resource.h"
34 #include "pbd/debug.h"
39 FileManager* FileDescriptor::_manager;
41 FileManager::FileManager ()
44 struct ResourceLimit rl;
46 /* XXX: this is a bit arbitrary */
47 if (get_resource_limit (OpenFiles, rl)) {
48 _max_open = rl.current_limit - 64;
53 DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
57 FileManager::add (FileDescriptor* d)
59 Glib::Threads::Mutex::Lock lm (_mutex);
63 /** @return true on error, otherwise false */
65 FileManager::allocate (FileDescriptor* d)
67 Glib::Threads::Mutex::Lock lm (_mutex);
71 /* this file needs to be opened */
73 if (_open == _max_open) {
75 /* We already have the maximum allowed number of files opened, so we must try to close one.
76 Find the unallocated, open file with the lowest last_used time.
79 double lowest_last_used = DBL_MAX;
80 list<FileDescriptor*>::iterator oldest = _files.end ();
82 for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) {
83 if ((*i)->is_open() && (*i)->_refcount == 0) {
84 if ((*i)->_last_used < lowest_last_used) {
85 lowest_last_used = (*i)->_last_used;
91 if (oldest == _files.end()) {
92 /* no unallocated and open files exist, so there's nothing we can do */
100 "closed file for %1 to release file handle; now have %2 of %3 open\n",
101 (*oldest)->_path, _open, _max_open
107 DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_path));
113 DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_path, _open, _max_open));
117 d->_last_used = mach_absolute_time();
118 #elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
120 clock_gettime (CLOCK_MONOTONIC, &t);
121 d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
124 gettimeofday (&now, NULL);
125 d->_last_used = now.tv_sec + (double) now.tv_usec / 10e6;
133 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
135 FileManager::release (FileDescriptor* d)
137 Glib::Threads::Mutex::Lock lm (_mutex);
140 assert (d->_refcount >= 0);
143 /** Remove a file from our lists. It will be closed if it is currently open. */
145 FileManager::remove (FileDescriptor* d)
147 Glib::Threads::Mutex::Lock lm (_mutex);
153 string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_path, _open, _max_open)
161 FileManager::close (FileDescriptor* d)
163 /* we must have a lock on our mutex */
166 d->Closed (); /* EMIT SIGNAL */
170 FileDescriptor::FileDescriptor (string const & n, bool w)
180 FileDescriptor::manager ()
183 _manager = new FileManager;
189 /** Release a previously allocated handle to this file */
191 FileDescriptor::release ()
193 manager()->release (this);
198 /** @param file_name Filename.
199 * @param writeable true to open writeable, otherwise false.
200 * @param mode Open mode for the file.
203 FdFileDescriptor::FdFileDescriptor (string const & file_name, bool writeable, mode_t mode)
204 : FileDescriptor (file_name, writeable)
208 manager()->add (this);
211 FdFileDescriptor::~FdFileDescriptor ()
213 manager()->remove (this);
217 FdFileDescriptor::is_open () const
219 /* we must have a lock on the FileManager's mutex */
225 FdFileDescriptor::open ()
227 /* we must have a lock on the FileManager's mutex */
229 _fd = ::open (_path.c_str(), _writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
234 FdFileDescriptor::close ()
236 /* we must have a lock on the FileManager's mutex */
242 /** @return fd, or -1 on error */
244 FdFileDescriptor::allocate ()
246 bool const f = manager()->allocate (this);
251 /* this is ok thread-wise because allocate () has incremented
252 the Descriptor's refcount, so the file will not be closed
259 FileDescriptor::set_path (const string& p)
264 /** @param file_name Filename.
265 * @param mode Mode to pass to fopen.
268 StdioFileDescriptor::StdioFileDescriptor (string const & file_name, std::string const & mode)
269 : FileDescriptor (file_name, false)
273 manager()->add (this);
276 StdioFileDescriptor::~StdioFileDescriptor ()
278 manager()->remove (this);
282 StdioFileDescriptor::is_open () const
284 /* we must have a lock on the FileManager's mutex */
290 StdioFileDescriptor::open ()
292 /* we must have a lock on the FileManager's mutex */
294 _file = fopen (_path.c_str(), _mode.c_str());
299 StdioFileDescriptor::close ()
301 /* we must have a lock on the FileManager's mutex */
307 /** @return FILE*, or 0 on error */
309 StdioFileDescriptor::allocate ()
311 bool const f = manager()->allocate (this);
316 /* this is ok thread-wise because allocate () has incremented
317 the Descriptor's refcount, so the file will not be closed