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 <glib/gstdio.h>
31 #include <mach/mach_time.h>
34 #include "pbd/compose.h"
35 #include "pbd/file_manager.h"
36 #include "pbd/resource.h"
37 #include "pbd/debug.h"
42 FileManager* FileDescriptor::_manager;
44 FileManager::FileManager ()
47 struct ResourceLimit rl;
49 /* XXX: this is a bit arbitrary */
50 if (get_resource_limit (OpenFiles, rl)) {
51 _max_open = rl.current_limit - 64;
56 DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
60 FileManager::add (FileDescriptor* d)
62 Glib::Threads::Mutex::Lock lm (_mutex);
66 /** @return true on error, otherwise false */
68 FileManager::allocate (FileDescriptor* d)
70 Glib::Threads::Mutex::Lock lm (_mutex);
74 /* this file needs to be opened */
76 if (_open == _max_open) {
78 /* We already have the maximum allowed number of files opened, so we must try to close one.
79 Find the unallocated, open file with the lowest last_used time.
82 double lowest_last_used = DBL_MAX;
83 list<FileDescriptor*>::iterator oldest = _files.end ();
85 for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) {
86 if ((*i)->is_open() && (*i)->_refcount == 0) {
87 if ((*i)->_last_used < lowest_last_used) {
88 lowest_last_used = (*i)->_last_used;
94 if (oldest == _files.end()) {
95 /* no unallocated and open files exist, so there's nothing we can do */
103 "closed file for %1 to release file handle; now have %2 of %3 open\n",
104 (*oldest)->_path, _open, _max_open
110 DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_path));
116 DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_path, _open, _max_open));
120 d->_last_used = mach_absolute_time();
121 #elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
123 clock_gettime (CLOCK_MONOTONIC, &t);
124 d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
127 gettimeofday (&now, NULL);
128 d->_last_used = now.tv_sec + (double) now.tv_usec / 10e6;
136 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
138 FileManager::release (FileDescriptor* d)
140 Glib::Threads::Mutex::Lock lm (_mutex);
143 assert (d->_refcount >= 0);
146 /** Remove a file from our lists. It will be closed if it is currently open. */
148 FileManager::remove (FileDescriptor* d)
150 Glib::Threads::Mutex::Lock lm (_mutex);
156 string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_path, _open, _max_open)
164 FileManager::close (FileDescriptor* d)
166 /* we must have a lock on our mutex */
169 d->Closed (); /* EMIT SIGNAL */
173 FileDescriptor::FileDescriptor (string const & n, bool w)
183 FileDescriptor::manager ()
186 _manager = new FileManager;
192 /** Release a previously allocated handle to this file */
194 FileDescriptor::release ()
196 manager()->release (this);
201 /** @param file_name Filename.
202 * @param writeable true to open writeable, otherwise false.
203 * @param mode Open mode for the file.
206 FdFileDescriptor::FdFileDescriptor (string const & file_name, bool writeable, mode_t mode)
207 : FileDescriptor (file_name, writeable)
211 manager()->add (this);
214 FdFileDescriptor::~FdFileDescriptor ()
216 manager()->remove (this);
220 FdFileDescriptor::is_open () const
222 /* we must have a lock on the FileManager's mutex */
228 FdFileDescriptor::open ()
230 /* we must have a lock on the FileManager's mutex */
232 _fd = ::g_open (_path.c_str(), _writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
237 FdFileDescriptor::close ()
239 /* we must have a lock on the FileManager's mutex */
245 /** @return fd, or -1 on error */
247 FdFileDescriptor::allocate ()
249 bool const f = manager()->allocate (this);
254 /* this is ok thread-wise because allocate () has incremented
255 the Descriptor's refcount, so the file will not be closed
262 FileDescriptor::set_path (const string& p)
267 /** @param file_name Filename.
268 * @param mode Mode to pass to fopen.
271 StdioFileDescriptor::StdioFileDescriptor (string const & file_name, std::string const & mode)
272 : FileDescriptor (file_name, false)
276 manager()->add (this);
279 StdioFileDescriptor::~StdioFileDescriptor ()
281 manager()->remove (this);
285 StdioFileDescriptor::is_open () const
287 /* we must have a lock on the FileManager's mutex */
293 StdioFileDescriptor::open ()
295 /* we must have a lock on the FileManager's mutex */
297 _file = fopen (_path.c_str(), _mode.c_str());
302 StdioFileDescriptor::close ()
304 /* we must have a lock on the FileManager's mutex */
310 /** @return FILE*, or 0 on error */
312 StdioFileDescriptor::allocate ()
314 bool const f = manager()->allocate (this);
319 /* this is ok thread-wise because allocate () has incremented
320 the Descriptor's refcount, so the file will not be closed