Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
spdlog
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
spdlog
Commits
94a7152a
Commit
94a7152a
authored
Jul 03, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
async queue - overrun oldsest policy option
parent
0358d115
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
22 deletions
+18
-22
common.h
include/spdlog/common.h
+2
-2
circular_q.h
include/spdlog/details/circular_q.h
+12
-11
mpmc_blocking_q.h
include/spdlog/details/mpmc_blocking_q.h
+3
-8
test_async.cpp
tests/test_async.cpp
+1
-1
No files found.
include/spdlog/common.h
View file @
94a7152a
...
...
@@ -126,8 +126,8 @@ using level_hasher = std::hash<int>;
//
enum
class
async_overflow_policy
{
block_retry
,
// Block
/ yield / sleep
until message can be enqueued
discard_log_msg
// Discard the message it enqueue fails
block_retry
,
// Block until message can be enqueued
overrun_oldeset
// Discard oldest message in the queue if full when trying to add new item.
};
//
...
...
include/spdlog/details/circular_q.h
View file @
94a7152a
...
...
@@ -15,20 +15,20 @@ public:
using
item_type
=
T
;
explicit
circular_q
(
size_t
max_items
)
:
max_items_
(
max_items
+
1
)
:
max_items_
(
max_items
+
1
)
// one item is reserved as marker for full q
,
v_
(
max_items_
)
{
}
// push back, overrun
last
item if no room left
// push back, overrun
(oldest)
item if no room left
void
push_back
(
T
&&
item
)
{
v_
[
head
_
]
=
std
::
move
(
item
);
head_
=
(
head
_
+
1
)
%
max_items_
;
v_
[
tail
_
]
=
std
::
move
(
item
);
tail_
=
(
tail
_
+
1
)
%
max_items_
;
if
(
head_
==
tail_
)
if
(
tail_
==
head_
)
// overrun last item if full
{
tail_
=
(
tail
_
+
1
)
%
max_items_
;
head_
=
(
head
_
+
1
)
%
max_items_
;
}
}
...
...
@@ -36,25 +36,26 @@ public:
// If there are no elements in the container, the behavior is undefined.
void
pop_front
(
T
&
popped_item
)
{
popped_item
=
std
::
move
(
v_
[
tail
_
]);
tail_
=
(
tail
_
+
1
)
%
max_items_
;
popped_item
=
std
::
move
(
v_
[
head
_
]);
head_
=
(
head
_
+
1
)
%
max_items_
;
}
bool
empty
()
{
return
head_
==
tail
_
;
return
tail_
==
head
_
;
}
bool
full
()
{
//
tail is ahead of the head
by 1
return
((
head_
+
1
)
%
max_items_
)
==
tail
_
;
//
head is ahead of the tail
by 1
return
((
tail_
+
1
)
%
max_items_
)
==
head
_
;
}
private
:
size_t
max_items_
;
typename
std
::
vector
<
T
>::
size_type
head_
=
0
;
typename
std
::
vector
<
T
>::
size_type
tail_
=
0
;
std
::
vector
<
T
>
v_
;
};
}
// namespace details
...
...
include/spdlog/details/mpmc_blocking_q.h
View file @
94a7152a
...
...
@@ -39,19 +39,14 @@ public:
push_cv_
.
notify_one
();
}
//
try to enqueue and return immediately false if no room left
bool
enqueue_nowait
(
T
&&
item
)
//
enqueue immediately. overrun oldest message in the queue if no room left.
void
enqueue_nowait
(
T
&&
item
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
if
(
q_
.
full
())
{
return
false
;
}
q_
.
push_back
(
std
::
forward
<
T
>
(
item
));
q_
.
push_back
(
std
::
move
(
item
));
}
push_cv_
.
notify_one
();
return
true
;
}
// try to dequeue item. if no item found. wait upto timeout and try again
...
...
tests/test_async.cpp
View file @
94a7152a
...
...
@@ -30,7 +30,7 @@ TEST_CASE("discard policy ", "[async]")
size_t
messages
=
1024
;
{
auto
tp
=
std
::
make_shared
<
details
::
thread_pool
>
(
queue_size
,
1
);
auto
logger
=
std
::
make_shared
<
async_logger
>
(
"as"
,
test_sink
,
tp
,
async_overflow_policy
::
discard_log_msg
);
auto
logger
=
std
::
make_shared
<
async_logger
>
(
"as"
,
test_sink
,
tp
,
async_overflow_policy
::
overrun_oldeset
);
for
(
size_t
i
=
0
;
i
<
messages
;
i
++
)
{
logger
->
info
(
"Hello message #{}"
,
i
);
...
...
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