fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / buffer_manager.cc
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 #include <iostream>
21
22 #include "pbd/compose.h"
23
24 #include "ardour/buffer_manager.h"
25 #include "ardour/thread_buffers.h"
26
27 using namespace ARDOUR;
28 using namespace PBD;
29
30 RingBufferNPT<ThreadBuffers*>* BufferManager::thread_buffers = 0;
31 std::list<ThreadBuffers*>* BufferManager::thread_buffers_list = 0;
32 Glib::Threads::Mutex BufferManager::rb_mutex;
33
34 using std::cerr;
35 using std::endl;
36
37 void
38 BufferManager::init (uint32_t size)
39 {
40         thread_buffers = new ThreadBufferFIFO (size+1); // must be one larger than requested
41         thread_buffers_list = new ThreadBufferList;
42
43         /* and populate with actual ThreadBuffers
44          */
45
46         for (uint32_t n = 0; n < size; ++n) {
47                 ThreadBuffers* ts = new ThreadBuffers;
48                 thread_buffers->write (&ts, 1);
49                 thread_buffers_list->push_back (ts);
50         }
51         // cerr << "Initialized thread buffers, readable count now " << thread_buffers->read_space() << endl;
52
53 }
54
55 ThreadBuffers*
56 BufferManager::get_thread_buffers ()
57 {
58         Glib::Threads::Mutex::Lock em (rb_mutex);
59         ThreadBuffers* tbp;
60
61         if (thread_buffers->read (&tbp, 1) == 1) {
62                 // cerr << "Got thread buffers, readable count now " << thread_buffers->read_space() << endl;
63                 return tbp;
64         }
65
66         return 0;
67 }
68
69 void
70 BufferManager::put_thread_buffers (ThreadBuffers* tbp)
71 {
72         Glib::Threads::Mutex::Lock em (rb_mutex);
73         thread_buffers->write (&tbp, 1);
74         // cerr << "Put back thread buffers, readable count now " << thread_buffers->read_space() << endl;
75 }
76
77 void
78 BufferManager::ensure_buffers (ChanCount howmany, size_t custom)
79 {
80         /* this is protected by the audioengine's process lock: we do not  */
81
82         for (ThreadBufferList::iterator i = thread_buffers_list->begin(); i != thread_buffers_list->end(); ++i) {
83                 (*i)->ensure_buffers (howmany, custom);
84         }
85 }