Skip to content

Commit

Permalink
mwcu_operationchain: fix race condition
Browse files Browse the repository at this point in the history
Signed-off-by: DMITRII DRAGAN <[email protected]>
  • Loading branch information
ddragan-bloomberg committed Jun 19, 2024
1 parent 3f733ac commit d511dd8
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 238 deletions.
25 changes: 9 additions & 16 deletions src/groups/mwc/mwcu/mwcu_operationchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,16 @@
// mwcu_operationchain.cpp -*-C++-*-
#include <mwcu_operationchain.h>

// MWC
#include <mwcscm_version.h>

// BDE
#include <bsl_iterator.h>
#include <bslmt_lockguard.h>
#include <bslmt_mutexassert.h>

namespace BloombergLP {
namespace mwcu {

namespace {

// TYPES
typedef bslmt::LockGuard<bslmt::Mutex> LockGuard;

typedef bslmt::LockGuardUnlock<bslmt::Mutex> UnlockGuard;

} // close unnamed namespace

// ------------------------------------
// class OperationChain_Job::TargetBase
// ------------------------------------
Expand Down Expand Up @@ -69,17 +61,18 @@ void OperationChain::onOperationCompleted(JobHandle handle)
// decrement the number of links in this chain and continue executing
// operations, given the chain is started and is not empty
if (--d_numLinks != 0 && d_isStarted) {
run();
return; // RETURN
run(&lock); // UNLOCK
return; // RETURN
}

// notify all waiting threads
d_condition.broadcast();
}

void OperationChain::run() BSLS_KEYWORD_NOEXCEPT
void OperationChain::run(LockGuard* lock) BSLS_KEYWORD_NOEXCEPT
{
// PRECONDITIONS
BSLS_ASSERT(lock);
BSLMT_MUTEXASSERT_IS_LOCKED(&d_mutex);
BSLS_ASSERT(d_isStarted);
BSLS_ASSERT(d_numLinks != 0);
Expand All @@ -93,7 +86,7 @@ void OperationChain::run() BSLS_KEYWORD_NOEXCEPT
unsigned numJobs = d_numJobsRunning;
JobList::iterator jobIt = d_jobList.begin();

UnlockGuard unlock(&d_mutex); // UNLOCK
lock->release()->unlock(); // UNLOCK

// run jobs
for (; numJobs != 0; --numJobs) {
Expand Down Expand Up @@ -166,7 +159,7 @@ void OperationChain::start()
}

// start executing operations
run();
run(&lock); // UNLOCK
}

void OperationChain::stop() BSLS_KEYWORD_NOEXCEPT
Expand Down Expand Up @@ -218,7 +211,7 @@ void OperationChain::append(Link* const* links, size_t count)
// if the chain is started and the first appended link is the first one in
// the chain, start executing operations right away
if (d_isStarted && d_numLinks != 0 && prevNumLinks == 0) {
run();
run(&lock); // UNLOCK
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/groups/mwc/mwcu/mwcu_operationchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
//..

// MWC

#include <mwcu_noop.h>
#include <mwcu_objectplaceholder.h>

Expand All @@ -178,6 +177,7 @@
#include <bslmf_nestedtraitdeclaration.h>
#include <bslmf_util.h>
#include <bslmt_condition.h>
#include <bslmt_lockguard.h>
#include <bslmt_mutex.h>
#include <bsls_assert.h>
#include <bsls_compilerfeatures.h>
Expand All @@ -193,7 +193,7 @@

#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
// Include version that can be compiled with C++03
// Generated on Tue Jun 28 12:18:48 2022
// Generated on Wed Jun 19 15:52:47 2024
// Command line: sim_cpp11_features.pl mwcu_operationchain.h
#define COMPILING_MWCU_OPERATIONCHAIN_H
#include <mwcu_operationchain_cpp03.h>
Expand Down Expand Up @@ -475,6 +475,8 @@ class OperationChain {

typedef JobList::iterator JobHandle;

typedef bslmt::LockGuard<bslmt::Mutex> LockGuard;

private:
// PRIVATE DATA

Expand Down Expand Up @@ -508,7 +510,9 @@ class OperationChain {
void onOperationCompleted(JobHandle handle) BSLS_KEYWORD_NOEXCEPT;

/// Execute all operations in the first link of this operation chain.
void run() BSLS_KEYWORD_NOEXCEPT;
/// Unlock the mutex associated with the specified 'lock' guard and
/// release the guard.
void run(LockGuard* lock) BSLS_KEYWORD_NOEXCEPT;

private:
// NOT IMPLEMENTED
Expand Down
4 changes: 2 additions & 2 deletions src/groups/mwc/mwcu/mwcu_operationchain_cpp03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// Automatically generated file. **DO NOT EDIT**

// Generated on Thu Jul 14 06:32:40 2022
// Generated on Wed Jun 19 14:58:49 2024
// Command line: sim_cpp11_features.pl mwcu_operationchain.cpp

#define INCLUDED_MWCU_OPERATIONCHAIN_CPP03 // Disable inclusion
Expand All @@ -31,7 +31,7 @@
#endif // defined(COMPILING_MWCU_OPERATIONCHAIN_CPP)

// ----------------------------------------------------------------------------
// Copyright 2022-2023 Bloomberg Finance L.P.
// Copyright 2024 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit d511dd8

Please sign in to comment.