2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #ifndef __LOCK_FREE_H__
22 #define __LOCK_FREE_H__
27 #define SPINLOCK_ACQUIRE(l) while(cas(&l, 0, 1)) {}
28 #define SPINLOCK_RELEASE(l) l = 0
30 // A unique-valued pointer. Version is incremented with each write.
33 #if !defined(__ppc__) && !defined(__powerpc__) && !defined(__arm__)
47 #if defined(__ppc__) || defined(__powerpc__) || defined(__arm__)
48 #define atomic_ptr_to_long(p) (long) *((long*)&p)
50 // This is ugly but correct as long as sizeof(void*) == sizeof(long)...
51 #define atomic_ptr_to_long_long(p) (long long) *((long long*)&p)
59 ///////////////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////////////
71 void lf_stack_init(lf_stack* pStack);
72 void lf_stack_push(lf_stack* pStack, lf_node* pNode);
73 lf_node* lf_stack_pop(lf_stack* pStack);
75 ///////////////////////////////////////////////////////////////////////////
76 // Fast Heap for Fixed-size Blocks
77 ///////////////////////////////////////////////////////////////////////////
88 lf_heap_chunk* top_chunk;
93 void lf_heap_init(lf_heap* pHeap, size_t blockSize, size_t initialSize = 0);
94 void lf_heap_grow(lf_heap* pHeap, size_t size = 0);
95 void lf_heap_deinit(lf_heap* pHeap);
96 void* lf_heap_alloc(lf_heap* pHeap);
97 void lf_heap_free(lf_heap* pHeap, void* p);
99 ///////////////////////////////////////////////////////////////////////////
101 ///////////////////////////////////////////////////////////////////////////
105 void* value; // TODO: Convert to a template
116 #define lf_queue_new_node(q) (lf_queue_node*)lf_heap_alloc(&q->node_heap)
117 #define lf_queue_free_node(q,n) lf_heap_free(&q->node_heap, n)
119 void lf_queue_init(lf_queue* pQueue);
120 void lf_queue_deinit(lf_queue* pQueue);
121 void lf_queue_enqueue(lf_queue* pQueue, void* pVal);
122 void* lf_queue_dequeue(lf_queue* pQueue);