Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
f5fe681a
Commit
f5fe681a
authored
Nov 05, 2017
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed issue #546 by adding an "is_empty" method to the queue instead of the buggy approx_size
parent
6fd5f3c8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
116 additions
and
120 deletions
+116
-120
include/spdlog/details/async_log_helper.h
include/spdlog/details/async_log_helper.h
+1
-1
include/spdlog/details/mpmc_bounded_q.h
include/spdlog/details/mpmc_bounded_q.h
+115
-119
No files found.
include/spdlog/details/async_log_helper.h
View file @
f5fe681a
...
@@ -384,7 +384,7 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_
...
@@ -384,7 +384,7 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_
inline
void
spdlog
::
details
::
async_log_helper
::
wait_empty_q
()
inline
void
spdlog
::
details
::
async_log_helper
::
wait_empty_q
()
{
{
auto
last_op
=
details
::
os
::
now
();
auto
last_op
=
details
::
os
::
now
();
while
(
_q
.
approx_size
()
>
0
)
while
(
!
_q
.
is_empty
()
)
{
{
sleep_or_yield
(
details
::
os
::
now
(),
last_op
);
sleep_or_yield
(
details
::
os
::
now
(),
last_op
);
}
}
...
...
include/spdlog/details/mpmc_bounded_q.h
View file @
f5fe681a
...
@@ -50,123 +50,119 @@ Distributed under the MIT License (http://opensource.org/licenses/MIT)
...
@@ -50,123 +50,119 @@ Distributed under the MIT License (http://opensource.org/licenses/MIT)
namespace
spdlog
namespace
spdlog
{
{
namespace
details
namespace
details
{
{
template
<
typename
T
>
template
<
typename
T
>
class
mpmc_bounded_queue
class
mpmc_bounded_queue
{
{
public:
public:
using
item_type
=
T
;
using
item_type
=
T
;
mpmc_bounded_queue
(
size_t
buffer_size
)
mpmc_bounded_queue
(
size_t
buffer_size
)
:
max_size_
(
buffer_size
),
:
max_size_
(
buffer_size
),
buffer_
(
new
cell_t
[
buffer_size
]),
buffer_
(
new
cell_t
[
buffer_size
]),
buffer_mask_
(
buffer_size
-
1
)
buffer_mask_
(
buffer_size
-
1
)
{
{
//queue size must be power of two
//queue size must be power of two
if
(
!
((
buffer_size
>=
2
)
&&
((
buffer_size
&
(
buffer_size
-
1
))
==
0
)))
if
(
!
((
buffer_size
>=
2
)
&&
((
buffer_size
&
(
buffer_size
-
1
))
==
0
)))
throw
spdlog_ex
(
"async logger queue size must be power of two"
);
throw
spdlog_ex
(
"async logger queue size must be power of two"
);
for
(
size_t
i
=
0
;
i
!=
buffer_size
;
i
+=
1
)
for
(
size_t
i
=
0
;
i
!=
buffer_size
;
i
+=
1
)
buffer_
[
i
].
sequence_
.
store
(
i
,
std
::
memory_order_relaxed
);
buffer_
[
i
].
sequence_
.
store
(
i
,
std
::
memory_order_relaxed
);
enqueue_pos_
.
store
(
0
,
std
::
memory_order_relaxed
);
enqueue_pos_
.
store
(
0
,
std
::
memory_order_relaxed
);
dequeue_pos_
.
store
(
0
,
std
::
memory_order_relaxed
);
dequeue_pos_
.
store
(
0
,
std
::
memory_order_relaxed
);
}
}
~
mpmc_bounded_queue
()
~
mpmc_bounded_queue
()
{
{
delete
[]
buffer_
;
delete
[]
buffer_
;
}
}
bool
enqueue
(
T
&&
data
)
bool
enqueue
(
T
&&
data
)
{
{
cell_t
*
cell
;
cell_t
*
cell
;
size_t
pos
=
enqueue_pos_
.
load
(
std
::
memory_order_relaxed
);
size_t
pos
=
enqueue_pos_
.
load
(
std
::
memory_order_relaxed
);
for
(;;)
for
(;;)
{
{
cell
=
&
buffer_
[
pos
&
buffer_mask_
];
cell
=
&
buffer_
[
pos
&
buffer_mask_
];
size_t
seq
=
cell
->
sequence_
.
load
(
std
::
memory_order_acquire
);
size_t
seq
=
cell
->
sequence_
.
load
(
std
::
memory_order_acquire
);
intptr_t
dif
=
static_cast
<
intptr_t
>
(
seq
)
-
static_cast
<
intptr_t
>
(
pos
);
intptr_t
dif
=
static_cast
<
intptr_t
>
(
seq
)
-
static_cast
<
intptr_t
>
(
pos
);
if
(
dif
==
0
)
{
if
(
dif
==
0
)
if
(
enqueue_pos_
.
compare_exchange_weak
(
pos
,
pos
+
1
,
std
::
memory_order_relaxed
))
{
break
;
if
(
enqueue_pos_
.
compare_exchange_weak
(
pos
,
pos
+
1
,
std
::
memory_order_relaxed
))
}
break
;
else
if
(
dif
<
0
)
{
}
return
false
;
else
if
(
dif
<
0
)
}
{
else
{
return
false
;
pos
=
enqueue_pos_
.
load
(
std
::
memory_order_relaxed
);
}
}
else
}
{
cell
->
data_
=
std
::
move
(
data
);
pos
=
enqueue_pos_
.
load
(
std
::
memory_order_relaxed
);
cell
->
sequence_
.
store
(
pos
+
1
,
std
::
memory_order_release
);
}
return
true
;
}
}
cell
->
data_
=
std
::
move
(
data
);
cell
->
sequence_
.
store
(
pos
+
1
,
std
::
memory_order_release
);
bool
dequeue
(
T
&
data
)
return
true
;
{
}
cell_t
*
cell
;
size_t
pos
=
dequeue_pos_
.
load
(
std
::
memory_order_relaxed
);
bool
dequeue
(
T
&
data
)
for
(;;)
{
{
cell
=
&
buffer_
[
pos
&
buffer_mask_
];
cell_t
*
cell
;
size_t
seq
=
size_t
pos
=
dequeue_pos_
.
load
(
std
::
memory_order_relaxed
);
cell
->
sequence_
.
load
(
std
::
memory_order_acquire
);
for
(;;)
intptr_t
dif
=
static_cast
<
intptr_t
>
(
seq
)
-
static_cast
<
intptr_t
>
(
pos
+
1
);
{
if
(
dif
==
0
)
{
cell
=
&
buffer_
[
pos
&
buffer_mask_
];
if
(
dequeue_pos_
.
compare_exchange_weak
(
pos
,
pos
+
1
,
std
::
memory_order_relaxed
))
size_t
seq
=
break
;
cell
->
sequence_
.
load
(
std
::
memory_order_acquire
);
}
intptr_t
dif
=
static_cast
<
intptr_t
>
(
seq
)
-
static_cast
<
intptr_t
>
(
pos
+
1
);
else
if
(
dif
<
0
)
if
(
dif
==
0
)
return
false
;
{
else
if
(
dequeue_pos_
.
compare_exchange_weak
(
pos
,
pos
+
1
,
std
::
memory_order_relaxed
))
pos
=
dequeue_pos_
.
load
(
std
::
memory_order_relaxed
);
break
;
}
}
data
=
std
::
move
(
cell
->
data_
);
else
if
(
dif
<
0
)
cell
->
sequence_
.
store
(
pos
+
buffer_mask_
+
1
,
std
::
memory_order_release
);
return
false
;
return
true
;
else
}
pos
=
dequeue_pos_
.
load
(
std
::
memory_order_relaxed
);
}
bool
is_empty
()
data
=
std
::
move
(
cell
->
data_
);
{
cell
->
sequence_
.
store
(
pos
+
buffer_mask_
+
1
,
std
::
memory_order_release
);
unsigned
front
,
front1
,
back
;
return
true
;
// try to take a consistent snapshot of front/tail.
}
do
{
front
=
enqueue_pos_
.
load
(
std
::
memory_order_acquire
);
size_t
approx_size
()
back
=
dequeue_pos_
.
load
(
std
::
memory_order_acquire
);
{
front1
=
enqueue_pos_
.
load
(
std
::
memory_order_relaxed
);
size_t
first_pos
=
dequeue_pos_
.
load
(
std
::
memory_order_relaxed
);
}
while
(
front
!=
front1
);
size_t
last_pos
=
enqueue_pos_
.
load
(
std
::
memory_order_relaxed
);
return
back
==
front
;
if
(
last_pos
<=
first_pos
)
}
return
0
;
auto
size
=
last_pos
-
first_pos
;
private:
return
size
<
max_size_
?
size
:
max_size_
;
struct
cell_t
}
{
std
::
atomic
<
size_t
>
sequence_
;
private:
T
data_
;
struct
cell_t
};
{
std
::
atomic
<
size_t
>
sequence_
;
size_t
const
max_size_
;
T
data_
;
};
static
size_t
const
cacheline_size
=
64
;
typedef
char
cacheline_pad_t
[
cacheline_size
];
size_t
const
max_size_
;
cacheline_pad_t
pad0_
;
static
size_t
const
cacheline_size
=
64
;
cell_t
*
const
buffer_
;
typedef
char
cacheline_pad_t
[
cacheline_size
];
size_t
const
buffer_mask_
;
cacheline_pad_t
pad1_
;
cacheline_pad_t
pad0_
;
std
::
atomic
<
size_t
>
enqueue_pos_
;
cell_t
*
const
buffer_
;
cacheline_pad_t
pad2_
;
size_t
const
buffer_mask_
;
std
::
atomic
<
size_t
>
dequeue_pos_
;
cacheline_pad_t
pad1_
;
cacheline_pad_t
pad3_
;
std
::
atomic
<
size_t
>
enqueue_pos_
;
cacheline_pad_t
pad2_
;
mpmc_bounded_queue
(
mpmc_bounded_queue
const
&
)
=
delete
;
std
::
atomic
<
size_t
>
dequeue_pos_
;
void
operator
=
(
mpmc_bounded_queue
const
&
)
=
delete
;
cacheline_pad_t
pad3_
;
};
mpmc_bounded_queue
(
mpmc_bounded_queue
const
&
)
=
delete
;
}
// ns details
void
operator
=
(
mpmc_bounded_queue
const
&
)
=
delete
;
};
}
// ns details
}
// ns spdlog
}
// ns spdlog
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment