From a9bfeabe70a05b4e0776d31a4ae62983bb9fc6e3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 20 Mar 2020 05:47:12 +0200 Subject: Tighten add_adhoc_member() against races --- libbuild2/algorithm.cxx | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'libbuild2') diff --git a/libbuild2/algorithm.cxx b/libbuild2/algorithm.cxx index 34283f9..7788b90 100644 --- a/libbuild2/algorithm.cxx +++ b/libbuild2/algorithm.cxx @@ -278,20 +278,23 @@ namespace build2 const_ptr* mp (&t.member); for (; *mp != nullptr && !(*mp)->is_a (tt); mp = &(*mp)->member) ; - target& m (*mp != nullptr // Might already be there. - ? **mp - : t.ctx.targets.insert (tt, - dir, - out, - move (n), - nullopt /* ext */, - true /* implied */, - trace).first); - if (*mp == nullptr) - { - *mp = &m; - m.group = &t; - } + if (*mp != nullptr) // Might already be there. + return **mp; + + pair r ( + t.ctx.targets.insert_locked (tt, + dir, + out, + move (n), + nullopt /* ext */, + true /* implied */, + trace)); + + assert (r.second.owns_lock ()); + + target& m (r.first); + *mp = &m; + m.group = &t; return m; }; -- cgit v1.1