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 #ifndef __pbd_file_manager_h__
21 #define __pbd_file_manager_h__
23 #include <sys/types.h>
27 #include <glibmm/threads.h>
29 #include "pbd/libpbd_visibility.h"
30 #include "pbd/signals.h"
34 class LIBPBD_API FileManager;
36 /** Parent class for FileDescriptors.
38 * When a subclass is instantiated, the file it describes is added to a
39 * list. The FileDescriptor can be `allocated', meaning that its
40 * file will be opened on the filesystem, and can then be `released'.
41 * FileDescriptors are reference counted as they are allocated and
42 * released. When a descriptor's refcount is 0, the file on the
43 * filesystem is eligible to be closed if necessary to free up file
44 * handles for other files.
46 * The upshot of all this is that Ardour can manage the number of
47 * open files to stay within limits imposed by the operating system.
50 class LIBPBD_API FileDescriptor
53 FileDescriptor (std::string const &, bool);
54 virtual ~FileDescriptor () {}
56 const std::string& path() const { return _path; }
59 virtual void set_path (const std::string&);
61 /** Emitted when the file is closed */
62 PBD::Signal0<void> Closed;
66 friend class FileManager;
68 /* These methods and variables must be called / accessed
69 with a lock held on the FileManager's mutex
72 /** @return false on success, true on failure */
73 virtual bool open () = 0;
74 virtual void close () = 0;
75 virtual bool is_open () const = 0;
77 int _refcount; ///< number of active users of this file
78 double _last_used; ///< monotonic time that this file was last allocated
79 std::string _path; ///< file path
80 bool _writeable; ///< true if it should be opened writeable, otherwise false
82 FileManager* manager ();
86 static FileManager* _manager;
90 /** FileDescriptor for a file to be opened using POSIX open */
91 class LIBPBD_API FdFileDescriptor : public FileDescriptor
94 FdFileDescriptor (std::string const & file_name, bool writeable, mode_t mode);
101 friend class FileManager;
105 bool is_open () const;
107 int _fd; ///< file descriptor, or -1 if the file is closed
108 mode_t _mode; ///< mode to use when creating files
111 /** FileDescriptor for a file opened using stdio */
112 class LIBPBD_API StdioFileDescriptor : public FileDescriptor
115 StdioFileDescriptor (std::string const & file_name, std::string const & mode);
116 ~StdioFileDescriptor ();
122 friend class FileManager;
126 bool is_open () const;
133 /** Class to limit the number of files held open */
134 class LIBPBD_API FileManager
139 void add (FileDescriptor *);
140 void remove (FileDescriptor *);
142 void release (FileDescriptor *);
143 bool allocate (FileDescriptor *);
147 void close (FileDescriptor *);
149 std::list<FileDescriptor*> _files; ///< files we know about
150 Glib::Threads::Mutex _mutex; ///< mutex for _files, _open and FileDescriptor contents
151 int _open; ///< number of open files
152 int _max_open; ///< maximum number of open files