allow zero-input (i.e. tone generator) processors to be added
[ardour2.git] / libs / pbd / file_manager.cc
blob50f0f2b22d63407a5798fedb07fe2cb71f0d371c
1 /*
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.
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <cassert>
26 #include <iostream>
27 #include <cstdio>
29 #ifdef __APPLE__
30 #include <mach/mach_time.h>
31 #endif
33 #include "pbd/compose.h"
34 #include "pbd/file_manager.h"
35 #include "pbd/debug.h"
37 using namespace std;
38 using namespace PBD;
40 FileManager* FileDescriptor::_manager;
42 FileManager::FileManager ()
43 : _open (0)
45 struct rlimit rl;
46 int const r = getrlimit (RLIMIT_NOFILE, &rl);
48 /* XXX: this is a bit arbitrary */
49 if (r == 0) {
50 _max_open = rl.rlim_cur - 64;
51 } else {
52 _max_open = 256;
55 DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
58 void
59 FileManager::add (FileDescriptor* d)
61 Glib::Mutex::Lock lm (_mutex);
62 _files.push_back (d);
65 /** @return true on error, otherwise false */
66 bool
67 FileManager::allocate (FileDescriptor* d)
69 Glib::Mutex::Lock lm (_mutex);
71 if (!d->is_open()) {
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;
88 oldest = i;
93 if (oldest == _files.end()) {
94 /* no unallocated and open files exist, so there's nothing we can do */
95 return true;
98 close (*oldest);
99 DEBUG_TRACE (
100 DEBUG::FileManager,
101 string_compose (
102 "closed file for %1 to release file handle; now have %2 of %3 open\n",
103 (*oldest)->_path, _open, _max_open
108 if (d->open ()) {
109 DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_path));
110 return true;
113 _open++;
115 DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_path, _open, _max_open));
118 #ifdef __APPLE__
119 d->_last_used = mach_absolute_time();
120 #else
121 struct timespec t;
122 clock_gettime (CLOCK_MONOTONIC, &t);
123 d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
124 #endif
126 d->_refcount++;
128 return false;
131 /** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
132 void
133 FileManager::release (FileDescriptor* d)
135 Glib::Mutex::Lock lm (_mutex);
137 d->_refcount--;
138 assert (d->_refcount >= 0);
141 /** Remove a file from our lists. It will be closed if it is currently open. */
142 void
143 FileManager::remove (FileDescriptor* d)
145 Glib::Mutex::Lock lm (_mutex);
147 if (d->is_open ()) {
148 close (d);
149 DEBUG_TRACE (
150 DEBUG::FileManager,
151 string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_path, _open, _max_open)
155 _files.remove (d);
158 void
159 FileManager::close (FileDescriptor* d)
161 /* we must have a lock on our mutex */
163 d->close ();
164 d->Closed (); /* EMIT SIGNAL */
165 _open--;
168 FileDescriptor::FileDescriptor (string const & n, bool w)
169 : _refcount (0)
170 , _last_used (0)
171 , _path (n)
172 , _writeable (w)
177 FileManager*
178 FileDescriptor::manager ()
180 if (_manager == 0) {
181 _manager = new FileManager;
184 return _manager;
187 /** Release a previously allocated handle to this file */
188 void
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)
203 , _fd (-1)
204 , _mode (m)
206 manager()->add (this);
209 FdFileDescriptor::~FdFileDescriptor ()
211 manager()->remove (this);
214 bool
215 FdFileDescriptor::is_open () const
217 /* we must have a lock on the FileManager's mutex */
219 return _fd != -1;
222 bool
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);
228 return (_fd == -1);
231 void
232 FdFileDescriptor::close ()
234 /* we must have a lock on the FileManager's mutex */
236 ::close (_fd);
237 _fd = -1;
240 /** @return fd, or -1 on error */
242 FdFileDescriptor::allocate ()
244 bool const f = manager()->allocate (this);
245 if (f) {
246 return -1;
249 /* this is ok thread-wise because allocate () has incremented
250 the Descriptor's refcount, so the file will not be closed
252 return _fd;
256 void
257 FileDescriptor::set_path (const string& p)
259 _path = p;
262 /** @param n Filename.
263 * @param w true to open writeable, otherwise false.
266 StdioFileDescriptor::StdioFileDescriptor (string const & n, std::string const & m)
267 : FileDescriptor (n, false)
268 , _file (0)
269 , _mode (m)
271 manager()->add (this);
274 StdioFileDescriptor::~StdioFileDescriptor ()
276 manager()->remove (this);
279 bool
280 StdioFileDescriptor::is_open () const
282 /* we must have a lock on the FileManager's mutex */
284 return _file != 0;
287 bool
288 StdioFileDescriptor::open ()
290 /* we must have a lock on the FileManager's mutex */
292 _file = fopen (_path.c_str(), _mode.c_str());
293 return (_file == 0);
296 void
297 StdioFileDescriptor::close ()
299 /* we must have a lock on the FileManager's mutex */
301 fclose (_file);
302 _file = 0;
305 /** @return FILE*, or 0 on error */
306 FILE*
307 StdioFileDescriptor::allocate ()
309 bool const f = manager()->allocate (this);
310 if (f) {
311 return 0;
314 /* this is ok thread-wise because allocate () has incremented
315 the Descriptor's refcount, so the file will not be closed
317 return _file;