Skip to content
This repository
branch: topic/intrusiv…
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 279 lines (213 sloc) 5.733 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
// Stethoscope shared memory buffer implementation
// This file is part of SuperCollider
//
// Copyright (C) 2011 Jakob Leben
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

#ifndef SC_SCOPE_BUFFER_HPP
#define SC_SCOPE_BUFFER_HPP

#include <boost/interprocess/offset_ptr.hpp>
#include <boost/atomic.hpp>

extern "C" {
#include "tlsf.h"
}

namespace detail_server_shm {

using boost::interprocess::offset_ptr;
using boost::atomic;

class scope_buffer_writer;
class scope_buffer_reader;

class scope_buffer_pool
{
public:

void init (void * pool, size_t size_of_pool)
{
pool_ = (char*)pool;
memset(pool_, 0, size_of_pool);
init_memory_pool(size_of_pool, pool_);
}

void * allocate (size_t bytes)
{
return malloc_ex(bytes, pool_);
}

void deallocate (void * ptr)
{
free_ex(ptr, pool_);
}

private:
friend class server_shared_memory;
char * pool_;
};

class scope_buffer
{
friend class scope_buffer_writer;
friend class scope_buffer_reader;

typedef offset_ptr<float> sh_float_ptr;

enum status
{
free = 0,
initialized
};

atomic<int> _status;

unsigned int _size;
unsigned int _channels;
sh_float_ptr _data;

/*
Reader/writer synchronization mechanism:

_stage, _in and _out are indexes into _state - an array of 3 equal data regions.

_out denotes the region where the writer writes.
_in denotes the region where the reader reads.
_stage denotes the region where data is exchanged between the writer and the reader.

After the writer is done writing, it sets the changed flag of the _in region,
and swaps _in with _stage.

The reader polls the changed flag of the _stage region. If it is set, it swaps _out
with _stage, reads the new _out region, and unsets its changed flag.
*/

atomic<int> _stage;
int _in;
int _out;

struct data_desc {
data_desc(): data(0), frames(0), changed(false) {}
sh_float_ptr data;
unsigned int frames;
atomic<bool> changed;
} _state [3];

public:

scope_buffer():
_status(free),
_stage(0),
_in(1),
_out(2)
{}

private:

// writer interface

bool allocate( scope_buffer_pool & pool, unsigned int channels, unsigned int size )
{
bool available = _status.load( boost::memory_order_relaxed ) == free;
if( !available ) return false;

_size = size;
_channels = channels;

unsigned int asset_size = channels * size;
_data = (float*)pool.allocate( asset_size * 3 * sizeof(float) );
if (_data == NULL)
return false;

_state[0].data = _data;
_state[1].data = _data + asset_size;
_state[2].data = _data + asset_size + asset_size;

_status.store( initialized, boost::memory_order_release );

return true;
}

void release( scope_buffer_pool & pool )
{
bool allocated = _status.load( boost::memory_order_relaxed ) != free;
if( !allocated ) return;

pool.deallocate( _data.get() );

_status.store( free, boost::memory_order_release );
}

float * write_address() { return _state[_in].data.get(); }

void push( unsigned int frames )
{
_state[_in].frames = frames;
_state[_in].changed.store( true, boost::memory_order_relaxed );
_in = _stage.exchange( _in, boost::memory_order_release );
}

// reader interface

bool valid()
{
return _status.load( boost::memory_order_acquire ) == initialized;
}

float * read_address() { return _state[_out].data.get(); }

unsigned int pull()
{
int stage = _stage.load( boost::memory_order_relaxed );
bool changed = _state[stage].changed.load( boost::memory_order_relaxed );

if( changed )
{
_state[_out].changed.store( false, boost::memory_order_relaxed );
_out = _stage.exchange( _out, boost::memory_order_acquire );
}

return _state[_out].frames;
}
};

class scope_buffer_writer
{
public:

scope_buffer *buffer;

scope_buffer_writer( scope_buffer *buffer = 0 ):
buffer(buffer)
{}

scope_buffer_writer( scope_buffer *buf, scope_buffer_pool & pool, unsigned int channels, unsigned int size ):
buffer(buf)
{
if( !buffer->allocate( pool, channels, size ) )
buffer = 0;
}

bool valid()
{
return buffer != 0;
}

float *data()
{
return buffer->write_address();
}

unsigned int max_size()
{
return buffer->_size;
}

void push( unsigned int frames )
{
buffer->push( frames );
}

void release( scope_buffer_pool & pool )
{
buffer->release( pool );
}
};

// FIXME: how do we ensure that scope_buffer data members used in the reader
// are consistent among themselves at all times???

class scope_buffer_reader
{
scope_buffer *buffer;

public:
scope_buffer_reader( scope_buffer *buffer_ = 0 ):
buffer(buffer_)
{}

bool valid()
{
// places an acquire memory ordering fence
return (buffer && buffer->valid());
}

bool pull( unsigned int &frames )
{
unsigned int new_frames = buffer->pull();

if( new_frames )
frames = new_frames;

return new_frames != 0;
}

float *data()
{
return buffer->read_address();
}

unsigned int max_frames()
{
return buffer->_size;
}

unsigned int channels()
{
return buffer->_channels;
}
};

} /* namespace detail_server_shm */

#endif
Something went wrong with that request. Please try again.