LMMS
Loading...
Searching...
No Matches
LocklessList.h
Go to the documentation of this file.
1/*
2 * LocklessList.h - list with lockless push and pop
3 *
4 * Copyright (c) 2016 Javier Serrano Polo <javier@jasp.net>
5 *
6 * This file is part of LMMS - https://lmms.io
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program (see COPYING); if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
22 *
23 */
24
25#ifndef LMMS_LOCKLESS_LIST_H
26#define LMMS_LOCKLESS_LIST_H
27
28#include "LocklessAllocator.h"
29
30#include <atomic>
31
32namespace lmms
33{
34
35template<typename T>
37{
38public:
39 struct Element
40 {
43 } ;
44
45 LocklessList( size_t size ) :
48 {
49 }
50
52 {
53 delete m_allocator;
54 }
55
56 void push( T value )
57 {
58 Element * e = m_allocator->alloc();
59 e->value = value;
60 e->next = m_first.load(std::memory_order_relaxed);
61
62 while (!m_first.compare_exchange_weak(e->next, e,
63 std::memory_order_release,
64 std::memory_order_relaxed))
65 {
66 // Empty loop (compare_exchange_weak updates e->next)
67 }
68 }
69
70 Element * popList()
71 {
72 return m_first.exchange(nullptr);
73 }
74
75 Element * first()
76 {
77 return m_first.load(std::memory_order_acquire);
78 }
79
80 void setFirst( Element * e )
81 {
82 m_first.store(e, std::memory_order_release);
83 }
84
85 void free( Element * e )
86 {
87 m_allocator->free( e );
88 }
89
90
91private:
92 std::atomic<Element*> m_first;
94
95} ;
96
97
98} // namespace lmms
99
100#endif // LMMS_LOCKLESS_LIST_H
#define nullptr
Definition DistrhoDefines.h:75
Definition LocklessAllocator.h:63
LocklessList(size_t size)
Definition LocklessList.h:45
Element * popList()
Definition LocklessList.h:70
void free(Element *e)
Definition LocklessList.h:85
~LocklessList()
Definition LocklessList.h:51
LocklessAllocatorT< Element > * m_allocator
Definition LocklessList.h:93
void push(T value)
Definition LocklessList.h:56
Element * first()
Definition LocklessList.h:75
void setFirst(Element *e)
Definition LocklessList.h:80
std::atomic< Element * > m_first
Definition LocklessList.h:92
* e
Definition inflate.c:1404
static PuglViewHint int value
Definition pugl.h:1708
Definition AudioAlsa.cpp:35
Definition LocklessList.h:40
Element * next
Definition LocklessList.h:42
T value
Definition LocklessList.h:41
ulg size
Definition extract.c:2350