torbenh's buffer manager fixes from 3.0P
[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 #include "ardour/buffer_manager.h"
22 #include "ardour/thread_buffers.h"
23
24 using namespace ARDOUR;
25 using namespace PBD;
26
27 RingBufferNPT<ThreadBuffers*>* BufferManager::thread_buffers = 0;
28 std::list<ThreadBuffers*>* BufferManager::thread_buffers_list = 0;
29 Glib::StaticMutex BufferManager::rb_mutex = GLIBMM_STATIC_MUTEX_INIT;
30
31 void
32 BufferManager::init (uint32_t size)
33 {
34         thread_buffers = new ThreadBufferFIFO (size+1); // must be one larger than requested
35         thread_buffers_list = new ThreadBufferList;
36
37         /* and populate with actual ThreadBuffers 
38          */
39
40         for (uint32_t n = 0; n < size; ++n) {        
41                 ThreadBuffers* ts = new ThreadBuffers;
42                 thread_buffers->write (&ts, 1);
43                 thread_buffers_list->push_back (ts);
44         }
45 }
46
47 ThreadBuffers*
48 BufferManager::get_thread_buffers ()
49 {
50         Glib::Mutex::Lock em (rb_mutex);
51         ThreadBuffers* tbp;
52
53         if (thread_buffers->read (&tbp, 1) == 1) {
54                 return tbp;
55         }
56
57         return 0;
58 }
59
60 void
61 BufferManager::put_thread_buffers (ThreadBuffers* tbp)
62 {
63         Glib::Mutex::Lock em (rb_mutex);
64         thread_buffers->write (&tbp, 1);
65 }
66
67 void
68 BufferManager::ensure_buffers (ChanCount howmany)
69 {
70         /* this is protected by the audioengine's process lock: we do not  */
71
72         for (ThreadBufferList::iterator i = thread_buffers_list->begin(); i != thread_buffers_list_end(); ++i) {
73                 (*i)->ensure_buffers (howmany);
74         }
75 }