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 /* files must be opened with O_BINARY flag on windows
233 * or it treats the file as a text stream and puts in
234 * line endings in etc
237 #define WRITE_FLAGS O_RDWR | O_CREAT | O_BINARY
238 #define READ_FLAGS O_RDONLY | O_BINARY
240 #define WRITE_FLAGS O_RDWR | O_CREAT
241 #define READ_FLAGS O_RDONLY
243 _fd = ::g_open (_path.c_str(), _writeable ? WRITE_FLAGS : READ_FLAGS, _mode);
248 FdFileDescriptor::close ()
250 /* we must have a lock on the FileManager's mutex */
256 /** @return fd, or -1 on error */
258 FdFileDescriptor::allocate ()
260 bool const f = manager()->allocate (this);
265 /* this is ok thread-wise because allocate () has incremented
266 the Descriptor's refcount, so the file will not be closed
273 FileDescriptor::set_path (const string& p)
278 /** @param file_name Filename.
279 * @param mode Mode to pass to fopen.
282 StdioFileDescriptor::StdioFileDescriptor (string const & file_name, std::string const & mode)
283 : FileDescriptor (file_name, false)
287 manager()->add (this);
290 StdioFileDescriptor::~StdioFileDescriptor ()
292 manager()->remove (this);
296 StdioFileDescriptor::is_open () const
298 /* we must have a lock on the FileManager's mutex */
304 StdioFileDescriptor::open ()
306 /* we must have a lock on the FileManager's mutex */
308 _file = fopen (_path.c_str(), _mode.c_str());
313 StdioFileDescriptor::close ()
315 /* we must have a lock on the FileManager's mutex */
321 /** @return FILE*, or 0 on error */
323 StdioFileDescriptor::allocate ()
325 bool const f = manager()->allocate (this);
330 /* this is ok thread-wise because allocate () has incremented
331 the Descriptor's refcount, so the file will not be closed