Give the Windows MMTimer test a better chance of passing
[ardour.git] / libs / fluidsynth / src / fluid_ringbuffer.c
1 /* FluidSynth - A Software Synthesizer
2  *
3  * Copyright (C) 2003  Peter Hanappe and others.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public License
7  * as published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  */
20
21 /*
22  * Josh Green <josh@resonance.org>
23  * 2009-05-28
24  */
25
26 #include "fluid_ringbuffer.h"
27 #include "fluidsynth_priv.h"
28
29
30 /**
31  * Create a lock free queue with a fixed maximum count and size of elements.
32  * @param count Count of elements in queue (fixed max number of queued elements)
33  * @return New lock free queue or NULL if out of memory (error message logged)
34  *
35  * Lockless FIFO queues don't use any locking mechanisms and can therefore be
36  * advantageous in certain situations, such as passing data between a lower
37  * priority thread and a higher "real time" thread, without potential lock
38  * contention which could stall the high priority thread.  Note that there may
39  * only be one producer thread and one consumer thread.
40  */
41 fluid_ringbuffer_t *
42 new_fluid_ringbuffer (int count, int elementsize)
43 {
44   fluid_ringbuffer_t *queue;
45
46   fluid_return_val_if_fail (count > 0, NULL);
47
48   queue = FLUID_NEW (fluid_ringbuffer_t);
49
50   if (!queue)
51   {
52     FLUID_LOG (FLUID_ERR, "Out of memory");
53     return NULL;
54   }
55
56   queue->array = FLUID_MALLOC (elementsize * count);
57
58   if (!queue->array)
59   {
60     FLUID_FREE (queue);
61     FLUID_LOG (FLUID_ERR, "Out of memory");
62     return NULL;
63   }
64
65   /* Clear array, in case dynamic pointer reclaiming is being done */
66   FLUID_MEMSET (queue->array, 0, elementsize * count);
67
68   queue->totalcount = count;
69   queue->elementsize = elementsize;
70   queue->count = 0;
71   queue->in = 0;
72   queue->out = 0;
73
74   return (queue);
75 }
76
77 /**
78  * Free an event queue.
79  * @param queue Lockless queue instance
80  *
81  * Care must be taken when freeing a queue, to ensure that the consumer and
82  * producer threads will no longer access it.
83  */
84 void
85 delete_fluid_ringbuffer (fluid_ringbuffer_t *queue)
86 {
87   FLUID_FREE (queue->array);
88   FLUID_FREE (queue);
89 }