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/thread.h>
28 #include "pbd/signals.h"
34 /** Parent class for FileDescriptors.
36 * When a subclass is instantiated, the file it describes is added to a
37 * list. The FileDescriptor can be `allocated', meaning that its
38 * file will be opened on the filesystem, and can then be `released'.
39 * FileDescriptors are reference counted as they are allocated and
40 * released. When a descriptor's refcount is 0, the file on the
41 * filesystem is eligible to be closed if necessary to free up file
42 * handles for other files.
44 * The upshot of all this is that Ardour can manage the number of
45 * open files to stay within limits imposed by the operating system.
51 FileDescriptor (std::string
const &, bool);
52 virtual ~FileDescriptor () {}
56 /** Emitted when the file is closed */
57 PBD::Signal0
<void> Closed
;
61 friend class FileManager
;
63 /* These methods and variables must be called / accessed
64 with a lock held on the FileManager's mutex
67 /** @return false on success, true on failure */
68 virtual bool open () = 0;
69 virtual void close () = 0;
70 virtual bool is_open () const = 0;
72 int _refcount
; ///< number of active users of this file
73 double _last_used
; ///< monotonic time that this file was last allocated
74 std::string _name
; ///< filename
75 bool _writeable
; ///< true if it should be opened writeable, otherwise false
77 FileManager
* manager ();
81 static FileManager
* _manager
;
85 /** FileDescriptor for a file to be opened using POSIX open */
86 class FdFileDescriptor
: public FileDescriptor
89 FdFileDescriptor (std::string
const &, bool, mode_t
);
96 friend class FileManager
;
100 bool is_open () const;
102 int _fd
; ///< file descriptor, or -1 if the file is closed
103 mode_t _mode
; ///< mode to use when creating files
106 /** FileDescriptor for a file opened using stdio */
107 class StdioFileDescriptor
: public FileDescriptor
110 StdioFileDescriptor (std::string
const &, std::string
const &);
111 ~StdioFileDescriptor ();
117 friend class FileManager
;
121 bool is_open () const;
128 /** Class to limit the number of files held open */
134 void add (FileDescriptor
*);
135 void remove (FileDescriptor
*);
137 void release (FileDescriptor
*);
138 bool allocate (FileDescriptor
*);
142 void close (FileDescriptor
*);
144 std::list
<FileDescriptor
*> _files
; ///< files we know about
145 Glib::Mutex _mutex
; ///< mutex for _files, _open and FileDescriptor contents
146 int _open
; ///< number of open files
147 int _max_open
; ///< maximum number of open files