]> git.sesse.net Git - mlt/blob - src/modules/jackrack/lock_free_fifo.c
even better close handling?
[mlt] / src / modules / jackrack / lock_free_fifo.c
1 /*
2  *   JACK Rack
3  *    
4  *   Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
5  *    
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <glib.h>
26
27 #include "lock_free_fifo.h"
28
29 /** initialise a lock free fifo */
30
31 void
32 lff_init (lff_t * lff, unsigned int size, size_t object_size)
33 {
34   lff->size = size;
35   lff->object_size = object_size;
36   lff->read_index = 0;
37   lff->write_index = 0;
38   lff->data = g_malloc (object_size * size);
39 }
40
41 lff_t *
42 lff_new (unsigned int size, size_t object_size)
43 {
44   lff_t * lff;
45   
46   lff = g_malloc (sizeof (lff_t));
47   
48   lff_init (lff, size, object_size);
49   
50   return lff;
51 }
52
53 void
54 lff_free (lff_t * lff)
55 {
56   g_free (lff->data);
57 }
58
59 void
60 lff_destroy (lff_t * lff)
61 {
62   lff_free (lff);
63   g_free (lff);
64 }
65
66 /** read an element from the fifo into data.
67 returns 0 on success, non-zero if there were no elements to read */
68 int lff_read (lff_t * lff, void * data) {
69   if (lff->read_index == lff->write_index) {
70     return -1;
71   } else {
72     memcpy (data, ((char *)lff->data) + (lff->read_index * lff->object_size),
73             lff->object_size);
74     lff->read_index++;
75     if (lff->read_index >= lff->size) {
76       lff->read_index = 0;
77     }
78     return 0;
79   }
80 }
81
82 /** write an element from data to the fifo.
83 returns 0 on success, non-zero if there was no space */
84 int lff_write (lff_t * lff, void * data) {
85   static unsigned int ri;
86   
87   /* got to read read_index only once for safety */
88   ri = lff->read_index;
89
90   /* lots of logic for when we're allowed to write to the fifo which basically
91      boils down to "don't write if we're one element behind the read index" */  
92   if ((ri > lff->write_index && ri - lff->write_index > 1) ||
93       (lff->write_index >= ri && lff->write_index != lff->size - 1) ||
94       (lff->write_index >= ri && lff->write_index == lff->size - 1 && ri != 0)) { 
95
96 /*  if ((ri > lff->write_index && ri - lff->write_index > 1) ||
97       (lff->write_index >= ri && (lff->write_index != lff->size - 1 || ri != 0))) { */
98
99     memcpy (((char *)lff->data) + (lff->write_index * lff->object_size),
100             data, lff->object_size);
101
102     /* FIXME: is this safe? */
103     lff->write_index++;
104     if (lff->write_index >= lff->size) {
105       lff->write_index = 0;
106     }
107
108     return 0;
109   } else {
110     return -1;
111   }
112 }