2016-08-23 12:50:54 -04:00
|
|
|
/* FluidSynth - A Software Synthesizer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Peter Hanappe and others.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2018-10-17 18:41:02 -04:00
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2.1 of
|
2016-08-23 12:50:54 -04: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
|
2018-10-17 18:41:02 -04:00
|
|
|
* Lesser General Public License for more details.
|
2016-08-23 12:50:54 -04:00
|
|
|
*
|
2018-10-17 18:41:02 -04:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2016-08-23 12:50:54 -04:00
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Josh Green <josh@resonance.org>
|
|
|
|
* 2009-05-28
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fluid_ringbuffer.h"
|
2019-02-23 11:58:05 -05:00
|
|
|
#include "fluid_sys.h"
|
2016-08-23 12:50:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
fluid_ringbuffer_t *
|
2018-10-17 18:41:02 -04:00
|
|
|
new_fluid_ringbuffer(int count, int elementsize)
|
2016-08-23 12:50:54 -04:00
|
|
|
{
|
2018-10-17 18:41:02 -04:00
|
|
|
fluid_ringbuffer_t *queue;
|
2016-08-23 12:50:54 -04:00
|
|
|
|
2018-10-17 18:41:02 -04:00
|
|
|
fluid_return_val_if_fail(count > 0, NULL);
|
2016-08-23 12:50:54 -04:00
|
|
|
|
2018-10-17 18:41:02 -04:00
|
|
|
queue = FLUID_NEW(fluid_ringbuffer_t);
|
2016-08-23 12:50:54 -04:00
|
|
|
|
2018-10-17 18:41:02 -04:00
|
|
|
if(!queue)
|
|
|
|
{
|
|
|
|
FLUID_LOG(FLUID_ERR, "Out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-08-23 12:50:54 -04:00
|
|
|
|
2018-10-17 18:41:02 -04:00
|
|
|
queue->array = FLUID_MALLOC(elementsize * count);
|
2016-08-23 12:50:54 -04:00
|
|
|
|
2018-10-17 18:41:02 -04:00
|
|
|
if(!queue->array)
|
|
|
|
{
|
|
|
|
FLUID_LOG(FLUID_ERR, "Out of memory");
|
|
|
|
delete_fluid_ringbuffer(queue);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-08-23 12:50:54 -04:00
|
|
|
|
2018-10-17 18:41:02 -04:00
|
|
|
/* Clear array, in case dynamic pointer reclaiming is being done */
|
|
|
|
FLUID_MEMSET(queue->array, 0, elementsize * count);
|
2016-08-23 12:50:54 -04:00
|
|
|
|
2018-10-17 18:41:02 -04:00
|
|
|
queue->totalcount = count;
|
|
|
|
queue->elementsize = elementsize;
|
|
|
|
fluid_atomic_int_set(&queue->count, 0);
|
|
|
|
queue->in = 0;
|
|
|
|
queue->out = 0;
|
2016-08-23 12:50:54 -04:00
|
|
|
|
2018-10-17 18:41:02 -04:00
|
|
|
return (queue);
|
2016-08-23 12:50:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-10-17 18:41:02 -04:00
|
|
|
delete_fluid_ringbuffer(fluid_ringbuffer_t *queue)
|
2016-08-23 12:50:54 -04:00
|
|
|
{
|
2018-10-17 18:41:02 -04:00
|
|
|
fluid_return_if_fail(queue != NULL);
|
|
|
|
FLUID_FREE(queue->array);
|
|
|
|
FLUID_FREE(queue);
|
2016-08-23 12:50:54 -04:00
|
|
|
}
|