aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2022-07-14 17:01:18 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2022-07-14 17:01:18 +0200
commitf07a6606e44d7bba88efa55615075a917704bde1 (patch)
tree2cd1a002188fcae9bc0797dc6f58b46546d13bdc
parentfd02ec768ea360ca8d1a4a1da33bd0fef42d0653 (diff)
Set builtin result while holding mutex
While the original code was probably correct, there is suspicion this is causing a TSAN false-positive.
-rw-r--r--libbutl/builtin.cxx12
-rw-r--r--libbutl/builtin.hxx3
-rw-r--r--libbutl/builtin.ixx12
3 files changed, 14 insertions, 13 deletions
diff --git a/libbutl/builtin.cxx b/libbutl/builtin.cxx
index b13a59a..6f451bf 100644
--- a/libbutl/builtin.cxx
+++ b/libbutl/builtin.cxx
@@ -2177,17 +2177,17 @@ namespace butl
{
unique_ptr<builtin::async_state> s (
new builtin::async_state (
+ r,
[fn,
- &r,
&args,
in = move (in), out = move (out), err = move (err),
&cwd,
- &cbs] () mutable noexcept
+ &cbs] () mutable noexcept -> uint8_t
{
- r = fn (args,
- move (in), move (out), move (err),
- cwd,
- cbs);
+ return fn (args,
+ move (in), move (out), move (err),
+ cwd,
+ cbs);
}));
return builtin (r, move (s));
diff --git a/libbutl/builtin.hxx b/libbutl/builtin.hxx
index b8546be..b301f8a 100644
--- a/libbutl/builtin.hxx
+++ b/libbutl/builtin.hxx
@@ -90,8 +90,7 @@ namespace butl
// be able to capture auto_fd by value in a lambda, etc).
//
template <typename F>
- explicit
- async_state (F);
+ async_state (uint8_t&, F);
};
builtin (std::uint8_t& r, std::unique_ptr<async_state>&& s = nullptr)
diff --git a/libbutl/builtin.ixx b/libbutl/builtin.ixx
index 24fbae3..d77590b 100644
--- a/libbutl/builtin.ixx
+++ b/libbutl/builtin.ixx
@@ -47,13 +47,14 @@ namespace butl
//
template <typename F>
inline builtin::async_state::
- async_state (F f)
- : thread ([f = std::move (f), this] () mutable noexcept
+ async_state (uint8_t& r, F f)
+ : thread ([this, &r, f = std::move (f)] () mutable noexcept
{
- f ();
+ uint8_t t (f ());
{
unique_lock l (this->mutex);
+ r = t;
finished = true;
}
@@ -68,9 +69,10 @@ namespace butl
{
std::unique_ptr<builtin::async_state> s (
new builtin::async_state (
- [f = std::move (f), &r] () mutable noexcept
+ r,
+ [f = std::move (f)] () mutable noexcept -> uint8_t
{
- r = f ();
+ return f ();
}));
return builtin (r, move (s));