substantive change: use the JACK wait API and provide "thread buffers" separately...
[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
29 void
30 BufferManager::init (uint32_t size)
31 {
32         thread_buffers = new ThreadBufferFIFO (size+1); // must be one larger than requested
33
34         /* and populate with actual ThreadBuffers 
35          */
36
37         std::cerr << "BM: initial read space: " << thread_buffers->read_space() << std::endl;
38         
39         for (uint32_t n = 0; n < size; ++n) {        
40                 ThreadBuffers* ts = new ThreadBuffers;
41                 thread_buffers->write (&ts, 1);
42                 std::cerr << "BM: added one, read =  " << thread_buffers->read_space() 
43                           << " write = " << thread_buffers->write_space() 
44                           << std::endl;
45         }
46
47         std::cerr << "BM: final, read =  " << thread_buffers->read_space() 
48                   << " write = " << thread_buffers->write_space() 
49                   << std::endl;
50
51         std::cerr << "BUFFER MANAGER INITIALIZED WITH " << size << " BUFFERs\n";
52 }
53
54 ThreadBuffers*
55 BufferManager::get_thread_buffers ()
56 {
57         ThreadBuffers* tbp;
58
59         if (thread_buffers->read (&tbp, 1) == 1) {
60                 return tbp;
61         }
62
63         return 0;
64 }
65
66 void
67 BufferManager::put_thread_buffers (ThreadBuffers* tbp)
68 {
69         thread_buffers->write (&tbp, 1);
70 }
71
72 void
73 BufferManager::ensure_buffers (ChanCount howmany)
74 {
75         /* this is protected by the audioengine's process lock: we do not  */
76
77         std::cerr << "BufMgr: ensure " << thread_buffers->bufsize() - 1 << " buffers match " << howmany << std::endl;
78
79         for (uint32_t n = 0; n < thread_buffers->bufsize() - 1; ++n) {
80                 thread_buffers->buffer()[n]->ensure_buffers (howmany);
81         }
82 }