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>
30 #include <mach/mach_time.h>
33 #include "pbd/compose.h"
34 #include "pbd/file_manager.h"
35 #include "pbd/debug.h"
40 FileManager
* FileDescriptor::_manager
;
42 FileManager::FileManager ()
46 int const r
= getrlimit (RLIMIT_NOFILE
, &rl
);
48 /* XXX: this is a bit arbitrary */
50 _max_open
= rl
.rlim_cur
- 64;
55 DEBUG_TRACE (DEBUG::FileManager
, string_compose ("FileManager can open up to %1 files.\n", _max_open
));
59 FileManager::add (FileDescriptor
* d
)
61 Glib::Mutex::Lock
lm (_mutex
);
65 /** @return true on error, otherwise false */
67 FileManager::allocate (FileDescriptor
* d
)
69 Glib::Mutex::Lock
lm (_mutex
);
73 /* this file needs to be opened */
75 if (_open
== _max_open
) {
77 /* We already have the maximum allowed number of files opened, so we must try to close one.
78 Find the unallocated, open file with the lowest last_used time.
81 double lowest_last_used
= DBL_MAX
;
82 list
<FileDescriptor
*>::iterator oldest
= _files
.end ();
84 for (list
<FileDescriptor
*>::iterator i
= _files
.begin(); i
!= _files
.end(); ++i
) {
85 if ((*i
)->is_open() && (*i
)->_refcount
== 0) {
86 if ((*i
)->_last_used
< lowest_last_used
) {
87 lowest_last_used
= (*i
)->_last_used
;
93 if (oldest
== _files
.end()) {
94 /* no unallocated and open files exist, so there's nothing we can do */
102 "closed file for %1 to release file handle; now have %2 of %3 open\n",
103 (*oldest
)->_path
, _open
, _max_open
109 DEBUG_TRACE (DEBUG::FileManager
, string_compose ("open of %1 failed.\n", d
->_path
));
115 DEBUG_TRACE (DEBUG::FileManager
, string_compose ("opened file for %1; now have %2 of %3 open.\n", d
->_path
, _open
, _max_open
));
119 d
->_last_used
= get_absolute_time();
122 clock_gettime (CLOCK_MONOTONIC
, &t
);
123 d
->_last_used
= t
.tv_sec
+ (double) t
.tv_nsec
/ 10e9
;
131 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
133 FileManager::release (FileDescriptor
* d
)
135 Glib::Mutex::Lock
lm (_mutex
);
138 assert (d
->_refcount
>= 0);
141 /** Remove a file from our lists. It will be closed if it is currently open. */
143 FileManager::remove (FileDescriptor
* d
)
145 Glib::Mutex::Lock
lm (_mutex
);
151 string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d
->_path
, _open
, _max_open
)
159 FileManager::close (FileDescriptor
* d
)
161 /* we must have a lock on our mutex */
164 d
->Closed (); /* EMIT SIGNAL */
168 FileDescriptor::FileDescriptor (string
const & n
, bool w
)
178 FileDescriptor::manager ()
181 _manager
= new FileManager
;
187 /** Release a previously allocated handle to this file */
189 FileDescriptor::release ()
191 manager()->release (this);
196 /** @param n Filename.
197 * @param w true to open writeable, otherwise false.
198 * @param m Open mode for the file.
201 FdFileDescriptor::FdFileDescriptor (string
const & n
, bool w
, mode_t m
)
202 : FileDescriptor (n
, w
)
206 manager()->add (this);
209 FdFileDescriptor::~FdFileDescriptor ()
211 manager()->remove (this);
215 FdFileDescriptor::is_open () const
217 /* we must have a lock on the FileManager's mutex */
223 FdFileDescriptor::open ()
225 /* we must have a lock on the FileManager's mutex */
227 _fd
= ::open (_path
.c_str(), _writeable
? (O_RDWR
| O_CREAT
) : O_RDONLY
, _mode
);
232 FdFileDescriptor::close ()
234 /* we must have a lock on the FileManager's mutex */
240 /** @return fd, or -1 on error */
242 FdFileDescriptor::allocate ()
244 bool const f
= manager()->allocate (this);
249 /* this is ok thread-wise because allocate () has incremented
250 the Descriptor's refcount, so the file will not be closed
257 FileDescriptor::set_path (const string
& p
)
263 /** @param n Filename.
264 * @param w true to open writeable, otherwise false.
267 StdioFileDescriptor::StdioFileDescriptor (string
const & n
, std::string
const & m
)
268 : FileDescriptor (n
, false)
272 manager()->add (this);
275 StdioFileDescriptor::~StdioFileDescriptor ()
277 manager()->remove (this);
281 StdioFileDescriptor::is_open () const
283 /* we must have a lock on the FileManager's mutex */
289 StdioFileDescriptor::open ()
291 /* we must have a lock on the FileManager's mutex */
293 _file
= fopen (_path
.c_str(), _mode
.c_str());
298 StdioFileDescriptor::close ()
300 /* we must have a lock on the FileManager's mutex */
306 /** @return FILE*, or 0 on error */
308 StdioFileDescriptor::allocate ()
310 bool const f
= manager()->allocate (this);
315 /* this is ok thread-wise because allocate () has incremented
316 the Descriptor's refcount, so the file will not be closed