Keep to a quota of open files by closing least recently used ones as required. Shoul...
authorCarl Hetherington <carl@carlh.net>
Fri, 14 May 2010 02:24:44 +0000 (02:24 +0000)
committerCarl Hetherington <carl@carlh.net>
Fri, 14 May 2010 02:24:44 +0000 (02:24 +0000)
git-svn-id: svn://localhost/ardour2/branches/3.0@7101 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/ardour/ardour/audiosource.h
libs/ardour/ardour/debug.h
libs/ardour/ardour/file_manager.h [new file with mode: 0644]
libs/ardour/ardour/sndfilesource.h
libs/ardour/audiosource.cc
libs/ardour/debug.cc
libs/ardour/file_manager.cc [new file with mode: 0644]
libs/ardour/sndfilesource.cc
libs/ardour/wscript

index c4a6e4e475c26a56067d749805958860b8c726fa..9879c75a8db7f475dcc99377db2ec28d475b6582 100644 (file)
@@ -32,6 +32,7 @@
 #include "ardour/source.h"
 #include "ardour/ardour.h"
 #include "ardour/readable.h"
+#include "ardour/file_manager.h"
 #include "pbd/stateful.h"
 #include "pbd/xml++.h"
 
@@ -140,7 +141,8 @@ class AudioSource : virtual public Source,
                                     framecnt_t frames_per_peak);
 
   private:
-       int        peakfile;
+       FdFileDescriptor* _peakfile_descriptor;
+       int        _peakfile_fd;
        framecnt_t peak_leftover_cnt;
        framecnt_t peak_leftover_size;
        Sample*    peak_leftovers;
index 57b66a87937428669f0b7590976f89577f45497e..33d973389ad3af90f099745740d9557388ad39b5 100644 (file)
@@ -46,6 +46,7 @@ namespace PBD {
                 extern uint64_t MidiClock;
                 extern uint64_t Monitor;
                 extern uint64_t Solo;
+               extern uint64_t FileManager;
        }
 }
 
diff --git a/libs/ardour/ardour/file_manager.h b/libs/ardour/ardour/file_manager.h
new file mode 100644 (file)
index 0000000..86eb967
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+    Copyright (C) 2010 Paul Davis
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#ifndef __file_manager_h__
+#define __file_manager_h__
+
+#include <sys/types.h>
+#include <string>
+#include <map>
+#include <sndfile.h>
+#include <glibmm/thread.h>
+#include "pbd/signals.h"
+
+namespace ARDOUR {
+
+class FileManager;
+
+/** Parent class for FileDescriptors.
+ *
+ *  When a subclass is instantiated, the file it describes is added to a
+ *  list.  The FileDescriptor can be `allocated', meaning that its
+ *  file will be opened on the filesystem, and can then be `released'.
+ *  FileDescriptors are reference counted as they are allocated and
+ *  released.  When a descriptor's refcount is 0, the file on the
+ *  filesystem is eligible to be closed if necessary to free up file
+ *  handles for other files.
+ *
+ *  The upshot of all this is that Ardour can manage the number of
+ *  open files to stay within limits imposed by the operating system.
+ */
+       
+class FileDescriptor
+{
+public:
+       FileDescriptor (std::string const &, bool);
+       virtual ~FileDescriptor () {}
+
+       void release ();
+
+       /** Emitted when the file is closed */
+       PBD::Signal0<void> Closed;
+
+protected:
+
+       friend class FileManager;
+
+       /* These methods and variables must be called / accessed
+          with a lock held on the FileManager's mutex
+       */
+
+       /** @return false on success, true on failure */
+       virtual bool open () = 0;
+       virtual void close () = 0;
+       virtual bool is_open () const = 0;
+
+       int refcount; ///< number of active users of this file
+       double last_used; ///< monotonic time that this file was last allocated
+       std::string name; ///< filename
+       bool writeable; ///< true if it should be opened writeable, otherwise false
+
+       FileManager* manager ();
+       
+private:
+       
+       static FileManager* _manager;
+};
+
+/** FileDescriptor for a file to be opened using libsndfile */ 
+class SndFileDescriptor : public FileDescriptor
+{
+public:
+       SndFileDescriptor (std::string const &, bool, SF_INFO *);
+       ~SndFileDescriptor ();
+
+       SNDFILE* allocate ();
+
+private:       
+
+       friend class FileManager;
+
+       bool open ();
+       void close ();
+       bool is_open () const;
+
+       SNDFILE* _sndfile; ///< SNDFILE* pointer, or 0 if the file is closed
+       SF_INFO* _info; ///< libsndfile's info for this file
+};
+
+/** FileDescriptor for a file to be opened using POSIX open */ 
+class FdFileDescriptor : public FileDescriptor
+{
+public:
+       FdFileDescriptor (std::string const &, bool, mode_t);
+       ~FdFileDescriptor ();
+
+       int allocate ();
+
+private:
+
+       friend class FileManager;
+
+       bool open ();
+       void close ();
+       bool is_open () const;
+
+       int _fd; ///< file descriptor, or -1 if the file is closed
+       mode_t _mode; ///< mode to use when creating files
+};
+
+}
+
+#endif
index 96f39de2c8e2b358fd5d8c8c4d6f16c2c9f77a5a..2338e35bb0ca53255b2a2f29deb7fd631ff2d340 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "ardour/audiofilesource.h"
 #include "ardour/broadcast_info.h"
+#include "ardour/file_manager.h"
 
 namespace ARDOUR {
 
@@ -72,13 +73,14 @@ class SndFileSource : public AudioFileSource {
        framecnt_t write_float (Sample* data, framepos_t pos, framecnt_t cnt);
 
   private:
-       SNDFILE *sf;
+       SndFileDescriptor* _descriptor;
        SF_INFO _info;
        BroadcastInfo *_broadcast_info;
 
        void init_sndfile ();
        int open();
        int setup_broadcast_info (framepos_t when, struct tm&, time_t);
+       void file_closed ();
 
        /* destructive */
 
@@ -100,6 +102,7 @@ class SndFileSource : public AudioFileSource {
        framecnt_t nondestructive_write_unlocked (Sample *dst, framecnt_t cnt);
        void handle_header_position_change ();
        PBD::ScopedConnection header_position_connection;
+       PBD::ScopedConnection file_manager_connection;
 };
 
 } // namespace ARDOUR
index d8dd58844f8c45f44f7348b8c7b6734e79d77317..d8640306463708c61bedeb64c23d08c546478686 100644 (file)
@@ -63,7 +63,7 @@ AudioSource::AudioSource (Session& s, ustring name)
 {
        _peaks_built = false;
        _peak_byte_max = 0;
-       peakfile = -1;
+       _peakfile_descriptor = 0;
        _read_data_count = 0;
        _write_data_count = 0;
        peak_leftover_cnt = 0;
@@ -78,7 +78,7 @@ AudioSource::AudioSource (Session& s, const XMLNode& node)
 
        _peaks_built = false;
        _peak_byte_max = 0;
-       peakfile = -1;
+       _peakfile_descriptor = 0;
        _read_data_count = 0;
        _write_data_count = 0;
        peak_leftover_cnt = 0;
@@ -98,10 +98,7 @@ AudioSource::~AudioSource ()
                cerr << "AudioSource destroyed with leftover peak data pending" << endl;
        }
 
-       if (peakfile >= 0) {
-               ::close (peakfile);
-       }
-
+       delete _peakfile_descriptor;
        delete [] peak_leftovers;
 }
 
@@ -316,7 +313,9 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
        int ret = -1;
        PeakData* staging = 0;
        Sample* raw_staging = 0;
-       int _peakfile = -1;
+
+       FdFileDescriptor* peakfile_descriptor = new FdFileDescriptor (peakpath, false, 0664);
+       int peakfile_fd = -1;
 
        expected_peaks = (cnt / (double) samples_per_file_peak);
        scale = npeaks/expected_peaks;
@@ -367,6 +366,7 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
                        peaks[i].min = raw_staging[i];
                }
 
+               delete peakfile_descriptor;
                delete [] raw_staging;
                return 0;
        }
@@ -377,8 +377,9 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
 
                /* open, read, close */
 
-               if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
+               if ((peakfile_fd = peakfile_descriptor->allocate ()) < 0) {
                        error << string_compose(_("AudioSource: cannot open peakpath (a) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
+                       delete peakfile_descriptor;
                        return -1;
                }
 
@@ -386,8 +387,8 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
                cerr << "DIRECT PEAKS\n";
 #endif
 
-               nread = ::pread (_peakfile, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
-               close (_peakfile);
+               nread = ::pread (peakfile_fd, peaks, sizeof (PeakData)* npeaks, first_peak_byte);
+               delete peakfile_descriptor;
 
                if (nread != sizeof (PeakData) * npeaks) {
                        cerr << "AudioSource["
@@ -402,6 +403,7 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
                             << first_peak_byte
                             << ')'
                             << endl;
+                       delete peakfile_descriptor;
                        return -1;
                }
 
@@ -409,6 +411,7 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
                        memset (&peaks[npeaks], 0, sizeof (PeakData) * zero_fill);
                }
 
+               delete peakfile_descriptor;
                return 0;
        }
 
@@ -451,8 +454,9 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
 
                /* open ... close during out: handling */
 
-               if ((_peakfile = ::open (peakpath.c_str(), O_RDONLY, 0664)) < 0) {
+               if ((peakfile_fd = peakfile_descriptor->allocate ()) < 0) {
                        error << string_compose(_("AudioSource: cannot open peakpath (b) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
+                       delete peakfile_descriptor;
                        delete [] staging;
                        return 0;
                }
@@ -469,10 +473,10 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
                                cerr << "read " << sizeof (PeakData) * to_read << " from peakfile @ " << start_byte << endl;
 #endif
 
-                               if ((nread = ::pread (_peakfile, staging, sizeof (PeakData) * to_read, start_byte))
+                               if ((nread = ::pread (peakfile_fd, staging, sizeof (PeakData) * to_read, start_byte))
                                    != sizeof (PeakData) * to_read) {
 
-                                       off_t fend = lseek (_peakfile, 0, SEEK_END);
+                                       off_t fend = lseek (peakfile_fd, 0, SEEK_END);
 
                                        cerr << "AudioSource["
                                             << _name
@@ -611,9 +615,7 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t
        }
 
   out:
-       if (_peakfile >= 0) {
-               close (_peakfile);
-       }
+       delete peakfile_descriptor;
 
        delete [] staging;
        delete [] raw_staging;
@@ -700,7 +702,8 @@ AudioSource::build_peaks_from_scratch ()
 int
 AudioSource::prepare_for_peakfile_writes ()
 {
-       if ((peakfile = ::open (peakpath.c_str(), O_RDWR|O_CREAT, 0664)) < 0) {
+       _peakfile_descriptor = new FdFileDescriptor (peakpath, true, 0664);
+       if ((_peakfile_fd = _peakfile_descriptor->allocate()) < 0) {
                error << string_compose(_("AudioSource: cannot open peakpath (c) \"%1\" (%2)"), peakpath, strerror (errno)) << endmsg;
                return -1;
        }
@@ -718,10 +721,8 @@ AudioSource::done_with_peakfile_writes (bool done)
                _peaks_built = true;
        }
 
-       if (peakfile >= 0) {
-               close (peakfile);
-               peakfile = -1;
-       }
+       delete _peakfile_descriptor;
+       _peakfile_descriptor = 0;
 }
 
 int
@@ -745,7 +746,7 @@ AudioSource::compute_and_write_peaks (Sample* buf, framepos_t first_frame, frame
        const size_t blocksize = (128 * 1024);
        off_t first_peak_byte;
 
-       if (peakfile < 0) {
+       if (_peakfile_descriptor == 0) {
                prepare_for_peakfile_writes ();
        }
 
@@ -766,7 +767,7 @@ AudioSource::compute_and_write_peaks (Sample* buf, framepos_t first_frame, frame
 
                        off_t byte = (peak_leftover_frame / fpp) * sizeof (PeakData);
 
-                       if (::pwrite (peakfile, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
+                       if (::pwrite (_peakfile_fd, &x, sizeof (PeakData), byte) != sizeof (PeakData)) {
                                error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
                                goto out;
                        }
@@ -869,16 +870,16 @@ AudioSource::compute_and_write_peaks (Sample* buf, framepos_t first_frame, frame
                   less than BLOCKSIZE bytes.  only call ftruncate if we'll make the file larger.
                */
 
-               off_t endpos = lseek (peakfile, 0, SEEK_END);
+               off_t endpos = lseek (_peakfile_fd, 0, SEEK_END);
                off_t target_length = blocksize * ((first_peak_byte + blocksize + 1) / blocksize);
 
                if (endpos < target_length) {
-                       ftruncate (peakfile, target_length);
+                       ftruncate (_peakfile_fd, target_length);
                        /* error doesn't actually matter though, so continue on without testing */
                }
        }
 
-       if (::pwrite (peakfile, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
+       if (::pwrite (_peakfile_fd, peakbuf, sizeof (PeakData) * peaks_computed, first_peak_byte) != (ssize_t) (sizeof (PeakData) * peaks_computed)) {
                error << string_compose(_("%1: could not write peak file data (%2)"), _name, strerror (errno)) << endmsg;
                goto out;
        }
@@ -905,7 +906,7 @@ AudioSource::compute_and_write_peaks (Sample* buf, framepos_t first_frame, frame
 void
 AudioSource::truncate_peakfile ()
 {
-       if (peakfile < 0) {
+       if (_peakfile_descriptor == 0) {
                error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor")
                      << endmsg;
                return;
@@ -913,10 +914,10 @@ AudioSource::truncate_peakfile ()
 
        /* truncate the peakfile down to its natural length if necessary */
 
-       off_t end = lseek (peakfile, 0, SEEK_END);
+       off_t end = lseek (_peakfile_fd, 0, SEEK_END);
 
        if (end > _peak_byte_max) {
-               ftruncate (peakfile, _peak_byte_max);
+               ftruncate (_peakfile_fd, _peak_byte_max);
        }
 }
 
index 268ec5d0336f83120b7b28d8ea4064098677d66b..35a30a00c4895bfdb1350de65b3854bc8142debc 100644 (file)
@@ -43,4 +43,5 @@ uint64_t PBD::DEBUG::MackieControl = PBD::new_debug_bit ("mackiecontrol");
 uint64_t PBD::DEBUG::MidiClock = PBD::new_debug_bit ("midiclock");
 uint64_t PBD::DEBUG::Monitor = PBD::new_debug_bit ("monitor");
 uint64_t PBD::DEBUG::Solo = PBD::new_debug_bit ("solo");
+uint64_t PBD::DEBUG::FileManager = PBD::new_debug_bit ("filemanager");
 
diff --git a/libs/ardour/file_manager.cc b/libs/ardour/file_manager.cc
new file mode 100644 (file)
index 0000000..bc27459
--- /dev/null
@@ -0,0 +1,327 @@
+/*
+    Copyright (C) 2010 Paul Davis
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <cassert>
+#include <iostream>
+#include "pbd/compose.h"
+#include "ardour/file_manager.h"
+#include "ardour/debug.h"
+
+using namespace std;
+using namespace PBD;
+using namespace ARDOUR;
+
+FileManager* FileDescriptor::_manager;
+
+namespace ARDOUR {
+
+/** Class to limit the number of files held open */
+class FileManager
+{
+public:
+       FileManager ();
+       
+       void add (FileDescriptor *);
+       void remove (FileDescriptor *);
+
+       void release (FileDescriptor *);
+       bool allocate (FileDescriptor *);
+
+private:
+       
+       void close (FileDescriptor *);
+
+       std::list<FileDescriptor*> _files; ///< files we know about
+       Glib::Mutex _mutex; ///< mutex for _files, _open and FileDescriptor contents
+       int _open; ///< number of open files
+       int _max_open; ///< maximum number of open files
+};
+
+}
+
+FileManager::FileManager ()
+       : _open (0)
+{
+       struct rlimit rl;
+       int const r = getrlimit (RLIMIT_NOFILE, &rl);
+       
+       /* XXX: this is a bit arbitrary */
+       if (r == 0) {
+               _max_open = rl.rlim_cur - 64;
+       } else {
+               _max_open = 256;
+       }
+
+       DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open));
+}
+
+void
+FileManager::add (FileDescriptor* d)
+{
+       Glib::Mutex::Lock lm (_mutex);
+       _files.push_back (d);
+}
+
+/** @return true on error, otherwise false */
+bool
+FileManager::allocate (FileDescriptor* d)
+{
+       Glib::Mutex::Lock lm (_mutex);
+
+       if (!d->is_open()) {
+               
+               /* this file needs to be opened */
+               
+               if (_open == _max_open) {
+
+                       /* We already have the maximum allowed number of files opened, so we must try to close one.
+                          Find the unallocated, open file with the lowest last_used time.
+                       */
+
+                       double lowest_last_used = DBL_MAX;
+                       list<FileDescriptor*>::iterator oldest = _files.end ();
+
+                       for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) {
+                               if ((*i)->is_open() && (*i)->refcount == 0) {
+                                       if ((*i)->last_used < lowest_last_used) {
+                                               lowest_last_used = (*i)->last_used;
+                                               oldest = i;
+                                       }
+                               }
+                       }
+
+                       if (oldest == _files.end()) {
+                               /* no unallocated and open files exist, so there's nothing we can do */
+                               return true;
+                       }
+
+                       close (*oldest);
+                       DEBUG_TRACE (
+                               DEBUG::FileManager,
+                               string_compose (
+                                       "closed file for %1 to release file handle; now have %2 of %3 open\n",
+                                       (*oldest)->name, _open, _max_open
+                                       )
+                               );
+               }
+
+               if (d->open ()) {
+                       return true;
+               }
+
+               _open++;
+
+               DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->name, _open, _max_open));
+       }
+
+       struct timespec t;
+       clock_gettime (CLOCK_MONOTONIC, &t);
+       d->last_used = t.tv_sec + (double) t.tv_nsec / 10e9;
+
+       d->refcount++;
+       
+       return false;
+}
+
+/** Tell FileManager that a FileDescriptor is no longer needed for a given handle */
+void
+FileManager::release (FileDescriptor* d)
+{
+       Glib::Mutex::Lock lm (_mutex);
+
+       d->refcount--;
+       assert (d->refcount >= 0);
+}
+
+/** Remove a file from our lists.  It will be closed if it is currently open. */
+void
+FileManager::remove (FileDescriptor* d)
+{
+       Glib::Mutex::Lock lm (_mutex);
+
+       if (d->is_open ()) {
+               close (d);
+               DEBUG_TRACE (
+                       DEBUG::FileManager,
+                       string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->name, _open, _max_open)
+                       );
+       }
+
+       _files.remove (d);
+}
+
+void
+FileManager::close (FileDescriptor* d)
+{
+       /* we must have a lock on our mutex */
+
+       d->close ();
+       d->Closed (); /* EMIT SIGNAL */
+       _open--;
+}
+
+FileDescriptor::FileDescriptor (string const & n, bool w)
+       : refcount (0)
+       , last_used (0)
+       , name (n)
+       , writeable (w)
+{
+
+}
+
+FileManager*
+FileDescriptor::manager ()
+{
+       if (_manager == 0) {
+               _manager = new FileManager;
+       }
+
+       return _manager;
+}
+
+/** Release a previously allocated handle to this file */
+void
+FileDescriptor::release ()
+{
+       manager()->release (this);
+}
+
+/** @param n Filename.
+ *  @param w true to open writeable, otherwise false.
+ *  @param i SF_INFO for the file.
+ */
+
+SndFileDescriptor::SndFileDescriptor (string const & n, bool w, SF_INFO* i)
+       : FileDescriptor (n, w)
+       , _sndfile (0)
+       , _info (i)
+{
+       manager()->add (this);
+}
+
+SndFileDescriptor::~SndFileDescriptor ()
+{
+       manager()->remove (this);
+}
+
+/** @return SNDFILE*, or 0 on error */
+SNDFILE*
+SndFileDescriptor::allocate ()
+{
+       bool const f = manager()->allocate (this);
+       if (f) {
+               return 0;
+       }
+
+       /* this is ok thread-wise because allocate () has incremented
+          the Descriptor's refcount, so the file will not be closed
+       */
+       return _sndfile;
+}
+
+void
+SndFileDescriptor::close ()
+{
+       /* we must have a lock on the FileManager's mutex */
+
+       sf_close (_sndfile);
+       _sndfile = 0;
+}
+
+bool
+SndFileDescriptor::is_open () const
+{
+       /* we must have a lock on the FileManager's mutex */
+
+       return _sndfile != 0;
+}
+
+bool
+SndFileDescriptor::open ()
+{
+       /* we must have a lock on the FileManager's mutex */
+       
+       _sndfile = sf_open (name.c_str(), writeable ? SFM_RDWR : SFM_READ, _info);
+       return (_sndfile == 0);
+}
+
+
+/** @param n Filename.
+ *  @param w true to open writeable, otherwise false.
+ *  @param m Open mode for the file.
+ */
+
+FdFileDescriptor::FdFileDescriptor (string const & n, bool w, mode_t m)
+       : FileDescriptor (n, w)
+       , _fd (-1)
+       , _mode (m)
+{
+       manager()->add (this);
+}
+
+FdFileDescriptor::~FdFileDescriptor ()
+{
+       manager()->remove (this);
+}
+
+bool
+FdFileDescriptor::is_open () const
+{
+       /* we must have a lock on the FileManager's mutex */
+
+       return _fd != -1;
+}
+
+bool
+FdFileDescriptor::open ()
+{
+       /* we must have a lock on the FileManager's mutex */
+       
+       _fd = ::open (name.c_str(), writeable ? (O_RDWR | O_CREAT) : O_RDONLY, _mode);
+       return (_fd == -1);
+}
+
+void
+FdFileDescriptor::close ()
+{
+       /* we must have a lock on the FileManager's mutex */
+
+       ::close (_fd);
+       _fd = -1;
+}
+
+/** @return fd, or -1 on error */
+int
+FdFileDescriptor::allocate ()
+{
+       bool const f = manager()->allocate (this);
+       if (f) {
+               return -1;
+       }
+
+       /* this is ok thread-wise because allocate () has incremented
+          the Descriptor's refcount, so the file will not be closed
+       */
+       return _fd;
+}
index 0ab55d160a12b1e233f50eceadf2f1cadda77fda..0b67cd789eb8bf50e86b75c37442a528eeef1f1b 100644 (file)
@@ -149,6 +149,12 @@ SndFileSource::SndFileSource (Session& s, const ustring& path,
 
        if (writable() && (_flags & Broadcast)) {
 
+               SNDFILE* sf = _descriptor->allocate ();
+               if (sf == 0) {
+                       error << string_compose (_("could not allocate file %1"), _path) << endmsg;
+                       throw failed_constructor ();
+               }
+
                if (!_broadcast_info) {
                        _broadcast_info = new BroadcastInfo;
                }
@@ -164,6 +170,8 @@ SndFileSource::SndFileSource (Session& s, const ustring& path,
                        delete _broadcast_info;
                        _broadcast_info = 0;
                }
+
+               _descriptor->release ();
        }
 }
 
@@ -174,7 +182,6 @@ SndFileSource::init_sndfile ()
 
        // lets try to keep the object initalizations here at the top
        xfade_buf = 0;
-       sf = 0;
        _broadcast_info = 0;
 
        /* although libsndfile says we don't need to set this,
@@ -198,7 +205,11 @@ SndFileSource::init_sndfile ()
 int
 SndFileSource::open ()
 {
-       if ((sf = sf_open (_path.c_str(), (writable() ? SFM_RDWR : SFM_READ), &_info)) == 0) {
+       _descriptor = new SndFileDescriptor (_path, writable(), &_info);
+       _descriptor->Closed.connect_same_thread (file_manager_connection, boost::bind (&SndFileSource::file_closed, this));
+       SNDFILE* sf = _descriptor->allocate ();
+       
+       if (sf == 0) {
                char errbuf[256];
                sf_error_str (0, errbuf, sizeof (errbuf) - 1);
 #ifndef HAVE_COREAUDIO
@@ -216,8 +227,8 @@ SndFileSource::open ()
 #ifndef HAVE_COREAUDIO
                error << string_compose(_("SndFileSource: file only contains %1 channels; %2 is invalid as a channel number"), _info.channels, _channel) << endmsg;
 #endif
-               sf_close (sf);
-               sf = 0;
+               delete _descriptor;
+               _descriptor = 0;
                return -1;
        }
 
@@ -241,24 +252,13 @@ SndFileSource::open ()
                sf_command (sf, SFC_SET_UPDATE_HEADER_AUTO, 0, SF_FALSE);
        }
 
+       _descriptor->release ();
        return 0;
 }
 
 SndFileSource::~SndFileSource ()
 {
-       if (sf) {
-               sf_close (sf);
-               sf = 0;
-
-               /* stupid libsndfile updated the headers on close,
-                  so touch the peakfile if it exists and has data
-                  to make sure its time is as new as the audio
-                  file.
-               */
-
-               touch_peakfile ();
-       }
-
+       delete _descriptor;
        delete _broadcast_info;
        delete [] xfade_buf;
 }
@@ -277,6 +277,12 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
        uint32_t real_cnt;
        framepos_t file_cnt;
 
+       SNDFILE* sf = _descriptor->allocate ();
+       if (sf == 0) {
+               error << string_compose (_("could not allocate file %1 for reading."), _path) << endmsg;
+               return 0;
+       }
+
        if (start > _length) {
 
                /* read starts beyond end of data, just memset to zero */
@@ -307,6 +313,7 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
                        char errbuf[256];
                        sf_error_str (0, errbuf, sizeof (errbuf) - 1);
                        error << string_compose(_("SndFileSource: could not seek to frame %1 within %2 (%3)"), start, _name.val().substr (1), errbuf) << endmsg;
+                       _descriptor->release ();
                        return 0;
                }
 
@@ -316,8 +323,9 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
                        if (ret != file_cnt) {
                                char errbuf[256];
                                sf_error_str (0, errbuf, sizeof (errbuf) - 1);
-                               cerr << string_compose(_("SndFileSource: @ %1 could not read %2 within %3 (%4) (len = %5)"), start, file_cnt, _name.val().substr (1), errbuf, _length) << endl;
+                               error << string_compose(_("SndFileSource: @ %1 could not read %2 within %3 (%4) (len = %5)"), start, file_cnt, _name.val().substr (1), errbuf, _length) << endl;
                        }
+                       _descriptor->release ();
                        return ret;
                }
        }
@@ -339,6 +347,7 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
 
        _read_data_count = cnt * sizeof(float);
 
+       _descriptor->release ();
        return nread;
 }
 
@@ -494,11 +503,21 @@ SndFileSource::update_header (sframes_t when, struct tm& now, time_t tnow)
 int
 SndFileSource::flush_header ()
 {
-       if (!writable() || (sf == 0)) {
+       if (!writable()) {
                warning << string_compose (_("attempt to flush a non-writable audio file source (%1)"), _path) << endmsg;
                return -1;
        }
-       return (sf_command (sf, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE);
+
+       SNDFILE* sf = _descriptor->allocate ();
+       if (sf == 0) {
+               error << string_compose (_("could not allocate file %1 to write header"), _path) << endmsg;
+               return -1;
+       }
+       
+       int const r = sf_command (sf, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE;
+       _descriptor->release ();
+
+       return r;
 }
 
 int
@@ -520,7 +539,9 @@ SndFileSource::setup_broadcast_info (sframes_t /*when*/, struct tm& now, time_t
 
        set_header_timeline_position ();
 
-       if (!_broadcast_info->write_to_file (sf)) {
+       SNDFILE* sf = _descriptor->allocate ();
+
+       if (sf == 0 || !_broadcast_info->write_to_file (sf)) {
                error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"),
                                           _path, _broadcast_info->get_error())
                      << endmsg;
@@ -529,6 +550,7 @@ SndFileSource::setup_broadcast_info (sframes_t /*when*/, struct tm& now, time_t
                _broadcast_info = 0;
        }
 
+       _descriptor->release ();
        return 0;
 }
 
@@ -541,7 +563,9 @@ SndFileSource::set_header_timeline_position ()
 
        _broadcast_info->set_time_reference (_timeline_position);
 
-       if (!_broadcast_info->write_to_file (sf)) {
+       SNDFILE* sf = _descriptor->allocate ();
+       
+       if (sf == 0 || !_broadcast_info->write_to_file (sf)) {
                error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"),
                                           _path, _broadcast_info->get_error())
                      << endmsg;
@@ -549,22 +573,29 @@ SndFileSource::set_header_timeline_position ()
                delete _broadcast_info;
                _broadcast_info = 0;
        }
+
+       _descriptor->release ();
 }
 
 framecnt_t
 SndFileSource::write_float (Sample* data, framepos_t frame_pos, framecnt_t cnt)
 {
-       if (sf_seek (sf, frame_pos, SEEK_SET|SFM_WRITE) < 0) {
+       SNDFILE* sf = _descriptor->allocate ();
+       
+       if (sf == 0 || sf_seek (sf, frame_pos, SEEK_SET|SFM_WRITE) < 0) {
                char errbuf[256];
                sf_error_str (0, errbuf, sizeof (errbuf) - 1);
                error << string_compose (_("%1: cannot seek to %2 (libsndfile error: %3"), _path, frame_pos, errbuf) << endmsg;
+               _descriptor->release ();
                return 0;
        }
 
        if (sf_writef_float (sf, data, cnt) != (ssize_t) cnt) {
+               _descriptor->release ();
                return 0;
        }
 
+       _descriptor->release ();
        return cnt;
 }
 
@@ -846,3 +877,15 @@ SndFileSource::clamped_at_unity () const
        /* XXX: this may not be the full list of formats that are unclamped */
        return (sub != SF_FORMAT_FLOAT && sub != SF_FORMAT_DOUBLE);
 }
+
+void
+SndFileSource::file_closed ()
+{
+       /* stupid libsndfile updated the headers on close,
+          so touch the peakfile if it exists and has data
+          to make sure its time is as new as the audio
+          file.
+       */
+       
+       touch_peakfile ();
+}
index d12ab3cc94594ce734944fa5909e5b010b81e68a..65d733e9b68a78b7cc8e1344a92ad52654698d7c 100644 (file)
@@ -92,6 +92,7 @@ libardour_sources = [
        'export_profile_manager.cc',
        'export_status.cc',
        'export_timespan.cc',
+       'file_manager.cc',
        'file_source.cc',
        'filename_extensions.cc',
        'filesystem_paths.cc',