2009-09-22 07:04:07 +00:00
|
|
|
/* FluidSynth - A Software Synthesizer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Peter Hanappe and others.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2017-07-12 15:45:23 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
2017-07-12 15:53:03 +00:00
|
|
|
* as published by the Free Software Foundation; either version 2.1 of
|
2009-09-22 07:04:07 +00:00
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
2017-07-12 15:45:23 +00:00
|
|
|
* Lesser General Public License for more details.
|
2009-09-22 07:04:07 +00:00
|
|
|
*
|
2017-07-12 15:54:54 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-09-22 07:04:07 +00:00
|
|
|
* License along with this library; if not, write to the Free
|
2011-08-15 12:57:10 +00:00
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA
|
2009-09-22 07:04:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Josh Green <josh@resonance.org>
|
|
|
|
* 2009-05-28
|
|
|
|
*/
|
|
|
|
|
2010-06-28 20:03:12 +00:00
|
|
|
#include "fluid_ringbuffer.h"
|
2009-09-22 07:04:07 +00:00
|
|
|
#include "fluidsynth_priv.h"
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a lock free queue with a fixed maximum count and size of elements.
|
|
|
|
* @param count Count of elements in queue (fixed max number of queued elements)
|
|
|
|
* @return New lock free queue or NULL if out of memory (error message logged)
|
|
|
|
*
|
|
|
|
* Lockless FIFO queues don't use any locking mechanisms and can therefore be
|
|
|
|
* advantageous in certain situations, such as passing data between a lower
|
|
|
|
* priority thread and a higher "real time" thread, without potential lock
|
|
|
|
* contention which could stall the high priority thread. Note that there may
|
|
|
|
* only be one producer thread and one consumer thread.
|
|
|
|
*/
|
2010-06-28 20:03:12 +00:00
|
|
|
fluid_ringbuffer_t *
|
|
|
|
new_fluid_ringbuffer (int count, int elementsize)
|
2009-09-22 07:04:07 +00:00
|
|
|
{
|
2010-06-28 20:03:12 +00:00
|
|
|
fluid_ringbuffer_t *queue;
|
2009-09-22 07:04:07 +00:00
|
|
|
|
|
|
|
fluid_return_val_if_fail (count > 0, NULL);
|
|
|
|
|
2010-06-28 20:03:12 +00:00
|
|
|
queue = FLUID_NEW (fluid_ringbuffer_t);
|
2009-09-22 07:04:07 +00:00
|
|
|
|
|
|
|
if (!queue)
|
|
|
|
{
|
|
|
|
FLUID_LOG (FLUID_ERR, "Out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-28 20:03:12 +00:00
|
|
|
queue->array = FLUID_MALLOC (elementsize * count);
|
2009-09-22 07:04:07 +00:00
|
|
|
|
|
|
|
if (!queue->array)
|
|
|
|
{
|
|
|
|
FLUID_LOG (FLUID_ERR, "Out of memory");
|
2017-10-29 12:23:08 +00:00
|
|
|
delete_fluid_ringbuffer(queue);
|
2009-09-22 07:04:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear array, in case dynamic pointer reclaiming is being done */
|
2010-06-28 20:03:12 +00:00
|
|
|
FLUID_MEMSET (queue->array, 0, elementsize * count);
|
2009-09-22 07:04:07 +00:00
|
|
|
|
|
|
|
queue->totalcount = count;
|
2010-06-28 20:03:12 +00:00
|
|
|
queue->elementsize = elementsize;
|
2017-10-25 11:22:36 +00:00
|
|
|
fluid_atomic_int_set(&queue->count, 0);
|
2009-09-22 07:04:07 +00:00
|
|
|
queue->in = 0;
|
|
|
|
queue->out = 0;
|
|
|
|
|
|
|
|
return (queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free an event queue.
|
|
|
|
* @param queue Lockless queue instance
|
|
|
|
*
|
|
|
|
* Care must be taken when freeing a queue, to ensure that the consumer and
|
|
|
|
* producer threads will no longer access it.
|
|
|
|
*/
|
|
|
|
void
|
2010-06-28 20:03:12 +00:00
|
|
|
delete_fluid_ringbuffer (fluid_ringbuffer_t *queue)
|
2009-09-22 07:04:07 +00:00
|
|
|
{
|
2017-10-29 12:23:08 +00:00
|
|
|
fluid_return_if_fail(queue != NULL);
|
2009-09-22 07:04:07 +00:00
|
|
|
FLUID_FREE (queue->array);
|
|
|
|
FLUID_FREE (queue);
|
|
|
|
}
|