1 /*
2  * Copyright (C) 2010-2012 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2010 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2011-2012 David Robillard <d@drobilla.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <iostream>
22 
23 #include "pbd/compose.h"
24 
25 #include "ardour/buffer_manager.h"
26 #include "ardour/thread_buffers.h"
27 
28 using namespace ARDOUR;
29 using namespace PBD;
30 
31 RingBufferNPT<ThreadBuffers*>* BufferManager::thread_buffers      = 0;
32 std::list<ThreadBuffers*>*     BufferManager::thread_buffers_list = 0;
33 Glib::Threads::Mutex           BufferManager::rb_mutex;
34 
35 using std::cerr;
36 using std::endl;
37 
38 void
init(uint32_t size)39 BufferManager::init (uint32_t size)
40 {
41 	thread_buffers      = new ThreadBufferFIFO (size + 1); // must be one larger than requested
42 	thread_buffers_list = new ThreadBufferList;
43 
44 	/* and populate with actual ThreadBuffers */
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 ThreadBuffers*
get_thread_buffers()55 BufferManager::get_thread_buffers ()
56 {
57 	Glib::Threads::Mutex::Lock em (rb_mutex);
58 	ThreadBuffers*             tbp;
59 
60 	if (thread_buffers->read (&tbp, 1) == 1) {
61 		// cerr << "Got thread buffers, readable count now " << thread_buffers->read_space() << endl;
62 		return tbp;
63 	}
64 
65 	return 0;
66 }
67 
68 void
put_thread_buffers(ThreadBuffers * tbp)69 BufferManager::put_thread_buffers (ThreadBuffers* tbp)
70 {
71 	Glib::Threads::Mutex::Lock em (rb_mutex);
72 	thread_buffers->write (&tbp, 1);
73 	// cerr << "Put back thread buffers, readable count now " << thread_buffers->read_space() << endl;
74 }
75 
76 void
ensure_buffers(ChanCount howmany,size_t custom)77 BufferManager::ensure_buffers (ChanCount howmany, size_t custom)
78 {
79 	/* this is protected by the audioengine's process lock: we do not  */
80 
81 	for (ThreadBufferList::iterator i = thread_buffers_list->begin (); i != thread_buffers_list->end (); ++i) {
82 		(*i)->ensure_buffers (howmany, custom);
83 	}
84 }
85