Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
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
brpc
Commits
4d2509f6
Commit
4d2509f6
authored
Oct 09, 2017
by
gejun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
r35361: Fix potential memory fence issue in DoublyBuffferedData
parent
50f17da8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
6 deletions
+14
-6
doubly_buffered_data.h
src/butil/containers/doubly_buffered_data.h
+13
-6
patch_from_svn
tools/patch_from_svn
+1
-0
No files found.
src/butil/containers/doubly_buffered_data.h
View file @
4d2509f6
...
...
@@ -26,6 +26,7 @@
#include "butil/macros.h"
#include "butil/type_traits.h"
#include "butil/errno.h"
#include "butil/atomicops.h"
namespace
butil
{
...
...
@@ -157,7 +158,8 @@ private:
const
Arg2
&
_arg2
;
};
const
T
*
UnsafeRead
()
const
{
return
_data
+
_index
;
}
const
T
*
UnsafeRead
()
const
{
return
_data
+
_index
.
load
(
butil
::
memory_order_acquire
);
}
Wrapper
*
AddWrapper
();
void
RemoveWrapper
(
Wrapper
*
);
...
...
@@ -165,7 +167,7 @@ private:
T
_data
[
2
];
// Index of foreground instance.
short
_index
;
butil
::
atomic
<
int
>
_index
;
// Key to access thread-local wrappers.
bool
_created_key
;
...
...
@@ -344,15 +346,20 @@ size_t DoublyBufferedData<T, TLS>::Modify(Fn& fn) {
// AddWrapper() or RemoveWrapper() too long. Most of the time, modifications
// are done by one thread, contention should be negligible.
BAIDU_SCOPED_LOCK
(
_modify_mutex
);
int
bg_index
=
!
_index
.
load
(
butil
::
memory_order_relaxed
);
// background instance is not accessed by other threads, being safe to
// modify.
const
size_t
ret
=
fn
(
_data
[
!
_index
]);
const
size_t
ret
=
fn
(
_data
[
bg
_index
]);
if
(
!
ret
)
{
return
0
;
}
// Publish, flip background and foreground.
_index
=
!
_index
;
// The release fence matches with the acquire fence in UnsafeRead() to
// make readers which just begin to read the new foreground instance see
// all changes made in fn.
_index
.
store
(
bg_index
,
butil
::
memory_order_release
);
bg_index
=
!
bg_index
;
// Wait until all threads finishes current reading. When they begin next
// read, they should see updated _index.
...
...
@@ -363,8 +370,8 @@ size_t DoublyBufferedData<T, TLS>::Modify(Fn& fn) {
}
}
const
size_t
ret2
=
fn
(
_data
[
!
_index
]);
CHECK_EQ
(
ret2
,
ret
)
<<
"index="
<<
_index
;
const
size_t
ret2
=
fn
(
_data
[
bg
_index
]);
CHECK_EQ
(
ret2
,
ret
)
<<
"index="
<<
_index
.
load
(
butil
::
memory_order_relaxed
)
;
return
ret2
;
}
...
...
tools/patch_from_svn
View file @
4d2509f6
...
...
@@ -58,6 +58,7 @@ cat $PATCHFILE | sed -e 's/src\/baidu\/rpc\//src\/brpc\//g' \
-e
's/<\(brpc\/[^>]*\)>/"\1"/g'
\
-e
's/<\(bvar\/[^>]*\)>/"\1"/g'
\
-e
's/<base\/\([^>]*\)>/"butil\/\1"/g'
\
-e
's/"base\/\([^"]*\)"/"butil\/\1"/g'
\
-e
's/<\(bthread\/[^>]*\)>/"\1"/g'
\
-e
's/<\(mcpack2pb\/[^>]*\)>/"\1"/g'
\
-e
's/\<protobuf_json\>/json2pb/g'
\
...
...
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