2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #ifndef __GST_TRASH_STACK_H__
21 #define __GST_TRASH_STACK_H__
24 #include "gstmacros.h"
28 typedef struct _GstTrashStack GstTrashStack;
29 typedef struct _GstTrashStackElement GstTrashStackElement;
31 struct _GstTrashStackElement {
32 GstTrashStackElement *next;
35 typedef volatile gpointer gst_vgpointer;/* gtk-doc volatile workaround */
36 typedef volatile gulong gst_vgulong; /* gtk-doc volatile workaround */
38 struct _GstTrashStack {
40 gst_vgulong count; /* for the ABA problem */
41 GMutex *lock; /* lock for C fallback */
44 GST_INLINE_FUNC GstTrashStack* gst_trash_stack_new (void);
45 GST_INLINE_FUNC void gst_trash_stack_init (GstTrashStack *stack);
46 GST_INLINE_FUNC void gst_trash_stack_destroy (GstTrashStack *stack);
47 GST_INLINE_FUNC void gst_trash_stack_free (GstTrashStack *stack);
49 GST_INLINE_FUNC void gst_trash_stack_push (GstTrashStack *stack, gpointer mem);
50 GST_INLINE_FUNC gpointer gst_trash_stack_pop (GstTrashStack *stack);
52 #if defined (GST_CAN_INLINE) || defined (__GST_TRASH_STACK_C__)
54 #if defined (USE_FAST_STACK_TRASH) && defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
56 #ifdef GST_CONFIG_NO_SMP
59 #define SMP_LOCK "lock ; "
63 * intel ia32 optimized lockfree implementations
66 gst_trash_stack_init (GstTrashStack *stack)
73 gst_trash_stack_destroy (GstTrashStack *stack)
78 gst_trash_stack_push (GstTrashStack *stack, gpointer mem)
80 __asm__ __volatile__ (
82 " movl %2, (%1); \n\t" /* mem->next == stack->head */
83 SMP_LOCK "cmpxchg %1, %0; \n\t" /* if head unchanged, move mem into it */
84 " jnz 1b; \n" /* head changed, retry */
92 GST_INLINE_FUNC gpointer
93 gst_trash_stack_pop (GstTrashStack *stack)
95 GstTrashStackElement *head;
97 /* pop is a little more complicated as we need to avoid the so called ABA
98 * problem that arises when a pop and push of the same element happens
99 * right between when we read head->next and try to swing the new pointer
100 * into place. This is usually solved using a counter which makes it highly
101 * unlikely that we manage to grab the wrong head->next value.
103 __asm__ __volatile__ (
105 " testl %%eax, %%eax; \n\t" /* if (head == NULL) return */
108 " movl (%%eax), %%ebx; \n\t" /* take value pointed to by head (head->next) */
109 " movl %%edx, %%ecx; \n\t" /* take counter */
110 " incl %%ecx; \n\t" /* and increment */
111 SMP_LOCK "cmpxchg8b %1; \n\t" /* if eax:edx == *stack, move ebx:ecx to *stack,
112 * else *stack is moved into eax:edx again... */
113 " jz 20f; \n\t" /* success */
114 " testl %%eax, %%eax; \n\t" /* if (head == NULL) return */
115 " jnz 10b; \n\t" /* else we retry */
131 * generic implementation
134 gst_trash_stack_init (GstTrashStack *stack)
137 stack->lock = g_mutex_new();
141 gst_trash_stack_destroy (GstTrashStack *stack)
143 g_mutex_free (stack->lock);
147 gst_trash_stack_push (GstTrashStack *stack, gpointer mem)
149 GstTrashStackElement *elem = (GstTrashStackElement *) mem;
151 g_mutex_lock (stack->lock);
152 elem->next = stack->head;
154 g_mutex_unlock (stack->lock);
157 GST_INLINE_FUNC gpointer
158 gst_trash_stack_pop (GstTrashStack *stack)
160 GstTrashStackElement *head;
162 g_mutex_lock (stack->lock);
163 head = (GstTrashStackElement *) stack->head;
165 stack->head = head->next;
166 g_mutex_unlock (stack->lock);
176 GST_INLINE_FUNC GstTrashStack*
177 gst_trash_stack_new (void)
179 GstTrashStack *stack;
181 stack = g_new (GstTrashStack, 1);
182 gst_trash_stack_init (stack);
188 gst_trash_stack_free (GstTrashStack *stack)
190 gst_trash_stack_destroy (stack);
194 #endif /* defined (GST_CAN_INLINE) || defined (__GST_TRASH_STACK_C__)*/
198 #endif /* __GST_TRASH_STACK_H__ */