lmms
Cross-platform music production software
Toggle main menu visibility
Loading...
Searching...
No Matches
include
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
32
namespace
lmms
33
{
34
35
template
<
typename
T>
36
class
LocklessList
37
{
38
public
:
39
struct
Element
40
{
41
T
value
;
42
Element
*
next
;
43
} ;
44
45
LocklessList
(
size_t
size ) :
46
m_first
(nullptr),
47
m_allocator
(new
LocklessAllocatorT
<Element>(size))
48
{
49
}
50
51
~LocklessList
()
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
91
private
:
92
std::atomic<Element*>
m_first
;
93
LocklessAllocatorT<Element>
*
m_allocator
;
94
95
} ;
96
97
98
}
// namespace lmms
99
100
#endif
// LMMS_LOCKLESS_LIST_H
LocklessAllocator.h
lmms::LocklessAllocatorT
Definition
LocklessAllocator.h:63
lmms::LocklessList::LocklessList
LocklessList(size_t size)
Definition
LocklessList.h:45
lmms::LocklessList::popList
Element * popList()
Definition
LocklessList.h:70
lmms::LocklessList::free
void free(Element *e)
Definition
LocklessList.h:85
lmms::LocklessList::~LocklessList
~LocklessList()
Definition
LocklessList.h:51
lmms::Element::m_allocator
LocklessAllocatorT< Element > * m_allocator
Definition
LocklessList.h:93
lmms::LocklessList::push
void push(T value)
Definition
LocklessList.h:56
lmms::LocklessList::first
Element * first()
Definition
LocklessList.h:75
lmms::LocklessList::setFirst
void setFirst(Element *e)
Definition
LocklessList.h:80
lmms::Element::m_first
std::atomic< Element * > m_first
Definition
LocklessList.h:92
lmms
Definition
RemotePluginBase.cpp:34
lmms::LocklessList::Element
Definition
LocklessList.h:40
lmms::LocklessList::Element::next
Element * next
Definition
LocklessList.h:42
lmms::LocklessList::Element::value
T value
Definition
LocklessList.h:41
Generated on
for lmms by
1.18.0