Use lambdas in the ConfigItem class

refs #12509
This commit is contained in:
Gunnar Beutner 2016-08-29 08:16:33 +02:00
parent 11e1cc9939
commit f12b52f465
4 changed files with 35 additions and 40 deletions

View File

@ -64,7 +64,7 @@ String WorkQueue::GetName(void) const
* allowInterleaved is true in which case the new task might be run
* immediately if it's being enqueued from within the WorkQueue thread.
*/
void WorkQueue::Enqueue(const boost::function<void (void)>& function, WorkQueuePriority priority,
void WorkQueue::Enqueue(boost::function<void (void)>&& function, WorkQueuePriority priority,
bool allowInterleaved)
{
bool wq_thread = IsWorkerThread();
@ -93,7 +93,7 @@ void WorkQueue::Enqueue(const boost::function<void (void)>& function, WorkQueueP
m_CVFull.wait(lock);
}
m_Tasks.push(Task(function, priority, ++m_NextTaskID));
m_Tasks.emplace(std::move(function), priority, ++m_NextTaskID);
m_CVEmpty.notify_one();
}

View File

@ -86,7 +86,7 @@ public:
void SetName(const String& name);
String GetName(void) const;
void Enqueue(const boost::function<void (void)>& function, WorkQueuePriority priority = PriorityNormal,
void Enqueue(boost::function<void (void)>&& function, WorkQueuePriority priority = PriorityNormal,
bool allowInterleaved = false);
void Join(bool stop = false);

View File

@ -384,35 +384,6 @@ ConfigItem::Ptr ConfigItem::GetByTypeAndName(const String& type, const String& n
return it2->second;
}
void ConfigItem::OnAllConfigLoadedHelper(void)
{
try {
m_Object->OnAllConfigLoaded();
} catch (const std::exception& ex) {
if (m_IgnoreOnError) {
Log(LogNotice, "ConfigObject")
<< "Ignoring config object '" << m_Name << "' of type '" << m_Type << "' due to errors: " << DiagnosticInformation(ex);
Unregister();
{
boost::mutex::scoped_lock lock(m_Mutex);
m_IgnoredItems.push_back(m_DebugInfo.Path);
}
return;
}
throw;
}
}
void ConfigItem::CreateChildObjectsHelper(const Type::Ptr& type)
{
ActivationScope ascope(m_ActivationContext);
m_Object->CreateChildObjects(type);
}
bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems)
{
typedef std::pair<ConfigItem::Ptr, bool> ItemPair;
@ -455,7 +426,9 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
for (const ItemPair& ip : items) {
newItems.push_back(ip.first);
upq.Enqueue(boost::bind(&ConfigItem::Commit, ip.first, ip.second));
upq.Enqueue([&]() {
ip.first->Commit(ip.second);
});
}
upq.Join();
@ -497,8 +470,29 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
if (!item->m_Object)
continue;
if (item->m_Type == type)
upq.Enqueue(boost::bind(&ConfigItem::OnAllConfigLoadedHelper, item));
if (item->m_Type == type) {
upq.Enqueue([&]() {
try {
item->m_Object->OnAllConfigLoaded();
} catch (const std::exception& ex) {
if (item->m_IgnoreOnError) {
Log(LogNotice, "ConfigObject")
<< "Ignoring config object '" << item->m_Name << "' of type '" << item->m_Type << "' due to errors: " << DiagnosticInformation(ex);
item->Unregister();
{
boost::mutex::scoped_lock lock(item->m_Mutex);
item->m_IgnoredItems.push_back(item->m_DebugInfo.Path);
}
return;
}
throw;
}
});
}
}
completed_types.insert(type);
@ -515,8 +509,12 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
if (!item->m_Object)
continue;
if (item->m_Type == loadDep)
upq.Enqueue(boost::bind(&ConfigItem::CreateChildObjectsHelper, item, ptype));
if (item->m_Type == loadDep) {
upq.Enqueue([&]() {
ActivationScope ascope(item->m_ActivationContext);
item->m_Object->CreateChildObjects(ptype);
});
}
}
}

View File

@ -114,9 +114,6 @@ private:
ConfigObject::Ptr Commit(bool discard = true);
static bool CommitNewItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems);
void OnAllConfigLoadedHelper(void);
void CreateChildObjectsHelper(const Type::Ptr& type);
};
}