~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/pbd/pbd/file_manager.h

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
ImportĀ upstreamĀ versionĀ 3.4~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2010 Paul Davis
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
17
 
 
18
*/
 
19
 
 
20
#ifndef __pbd_file_manager_h__
 
21
#define __pbd_file_manager_h__
 
22
 
 
23
#include <sys/types.h>
 
24
#include <string>
 
25
#include <map>
 
26
#include <list>
 
27
#include <glibmm/threads.h>
 
28
#include "pbd/signals.h"
 
29
 
 
30
namespace PBD {
 
31
 
 
32
class FileManager;
 
33
 
 
34
/** Parent class for FileDescriptors.
 
35
 *
 
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.
 
43
 *
 
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.
 
46
 */
 
47
        
 
48
class FileDescriptor
 
49
{
 
50
public:
 
51
        FileDescriptor (std::string const &, bool);
 
52
        virtual ~FileDescriptor () {}
 
53
 
 
54
        const std::string& path() const { return _path; }
 
55
 
 
56
        void release ();
 
57
        virtual void set_path (const std::string&);
 
58
 
 
59
        /** Emitted when the file is closed */
 
60
        PBD::Signal0<void> Closed;
 
61
 
 
62
protected:
 
63
 
 
64
        friend class FileManager;
 
65
 
 
66
        /* These methods and variables must be called / accessed
 
67
           with a lock held on the FileManager's mutex
 
68
        */
 
69
 
 
70
        /** @return false on success, true on failure */
 
71
        virtual bool open () = 0;
 
72
        virtual void close () = 0;
 
73
        virtual bool is_open () const = 0;
 
74
 
 
75
        int _refcount; ///< number of active users of this file
 
76
        double _last_used; ///< monotonic time that this file was last allocated
 
77
        std::string _path; ///< file path
 
78
        bool _writeable; ///< true if it should be opened writeable, otherwise false
 
79
 
 
80
        FileManager* manager ();
 
81
        
 
82
private:
 
83
        
 
84
        static FileManager* _manager;
 
85
};
 
86
 
 
87
 
 
88
/** FileDescriptor for a file to be opened using POSIX open */  
 
89
class FdFileDescriptor : public FileDescriptor
 
90
{
 
91
public:
 
92
        FdFileDescriptor (std::string const & file_name, bool writeable, mode_t mode);
 
93
        ~FdFileDescriptor ();
 
94
 
 
95
        int allocate ();
 
96
 
 
97
private:
 
98
 
 
99
        friend class FileManager;
 
100
 
 
101
        bool open ();
 
102
        void close ();
 
103
        bool is_open () const;
 
104
 
 
105
        int _fd; ///< file descriptor, or -1 if the file is closed
 
106
        mode_t _mode; ///< mode to use when creating files
 
107
};
 
108
 
 
109
/** FileDescriptor for a file opened using stdio */
 
110
class StdioFileDescriptor : public FileDescriptor
 
111
{
 
112
public:
 
113
        StdioFileDescriptor (std::string const & file_name, std::string const & mode);
 
114
        ~StdioFileDescriptor ();
 
115
 
 
116
        FILE* allocate ();
 
117
 
 
118
private:
 
119
 
 
120
        friend class FileManager;
 
121
 
 
122
        bool open ();
 
123
        void close ();
 
124
        bool is_open () const;
 
125
 
 
126
        FILE* _file;
 
127
        std::string _mode;
 
128
};
 
129
 
 
130
 
 
131
/** Class to limit the number of files held open */
 
132
class FileManager
 
133
{
 
134
public:
 
135
        FileManager ();
 
136
        
 
137
        void add (FileDescriptor *);
 
138
        void remove (FileDescriptor *);
 
139
 
 
140
        void release (FileDescriptor *);
 
141
        bool allocate (FileDescriptor *);
 
142
 
 
143
private:
 
144
        
 
145
        void close (FileDescriptor *);
 
146
 
 
147
        std::list<FileDescriptor*> _files; ///< files we know about
 
148
        Glib::Threads::Mutex _mutex; ///< mutex for _files, _open and FileDescriptor contents
 
149
        int _open; ///< number of open files
 
150
        int _max_open; ///< maximum number of open files
 
151
};
 
152
 
 
153
}
 
154
 
 
155
#endif