Skip to content

Commit

Permalink
chore: fix data race in executor group
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jun 15, 2023
1 parent 2bbb606 commit d49dcb1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
27 changes: 18 additions & 9 deletions include/mrdox/Support/ExecutorGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,24 @@
namespace clang {
namespace mrdox {

class MRDOX_DECL
ExecutorGroupBase
{
protected:
struct MRDOX_DECL
AnyAgent
{
virtual ~AnyAgent() = 0;
virtual void* get() noexcept = 0;
};

public:
};

/** A set of execution agents for performing concurrent work.
*/
template<class Agent>
class ExecutorGroup
class ExecutorGroup : public ExecutorGroupBase
{
struct Impl
{
Expand Down Expand Up @@ -124,7 +138,6 @@ class ExecutorGroup
: group_(group)
, agent_(std::move(agent))
{
MRDOX_ASSERT(agent_.get() != nullptr);
}

~scoped_agent()
Expand All @@ -136,32 +149,28 @@ class ExecutorGroup

Agent& operator*() const noexcept
{
MRDOX_ASSERT(agent_.get() != nullptr);
return *agent_;
}
};

void
run(std::unique_lock<std::mutex> lock)
{
MRDOX_ASSERT(lock.owns_lock());
std::unique_ptr<Agent> agent(std::move(agents_.back()));
MRDOX_ASSERT(agent.get() != nullptr);
agents_.pop_back();
++busy_;

threadPool_.async(
[this, agent = std::move(agent)]() mutable
{
MRDOX_ASSERT(agent.get() != nullptr);
scoped_agent scope(*this, std::move(agent));
MRDOX_ASSERT(agent.get() == nullptr);
std::unique_lock<std::mutex> lock(impl_->mutex_);
scoped_agent scope(*this, std::move(agent));
for(;;)
{
if(work_.empty())
break;
any_callable<void(Agent&)> work(std::move(work_.front()));
any_callable<void(Agent&)> work(
std::move(work_.front()));
work_.pop_front();
unlock_guard unlock(impl_->mutex_);
work(*scope);
Expand Down
17 changes: 17 additions & 0 deletions source/Support/ExecutorGroup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Vinnie Falco ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdox
//

#include <mrdox/Support/ExecutorGroup.hpp>

namespace clang {
namespace mrdox {

} // mrdox
} // clang

0 comments on commit d49dcb1

Please sign in to comment.