Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/CBasic/CAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


#include <new>
#include <mutex>
#include <memory>

#include "CObject.h"
Expand Down Expand Up @@ -55,8 +54,9 @@ class CAllocator {
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static T* safeMallocTemplateCObject(Args&&... args) {
T* result = nullptr;
while (!result) {
result = new(std::nothrow) T(std::forward<Args&&>(args)...);
CUInt maxTimes = 3;
while (!result && maxTimes--) {
result = new(std::nothrow) T(std::forward<Args>(args)...);
}
return result;
}
Expand All @@ -81,7 +81,8 @@ class CAllocator {
template<class T>
static T* safeMalloc() {
T* ptr = nullptr;
while (!ptr) {
CUInt maxTimes = 3;
while (!ptr && maxTimes--) {
ptr = new(std::nothrow) T();
}
return ptr;
Expand Down
1 change: 0 additions & 1 deletion src/CBasic/CFuncType.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <functional>

#include "CStrDefine.h"
#include "CValType.h"

CGRAPH_NAMESPACE_BEGIN
Expand Down
12 changes: 6 additions & 6 deletions src/CBasic/CStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class CSTATUS {
this->error_info_ = errorInfo;
}

explicit CSTATUS(int errorCode, const std::string &errorInfo) {
explicit CSTATUS(const int& errorCode, const std::string& errorInfo) {
this->error_code_ = errorCode;
this->error_info_ = errorInfo;
}

CSTATUS(const CSTATUS &status) {
CSTATUS(const CSTATUS& status) {
if (status.error_code_ == error_code_) {
return;
}
Expand All @@ -50,7 +50,7 @@ class CSTATUS {
this->error_info_ = status.error_info_;
}

CSTATUS(const CSTATUS &&status) noexcept {
CSTATUS(const CSTATUS&& status) noexcept {
if (status.error_code_ == error_code_) {
return;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ class CSTATUS {
* @param info
* @return
*/
CSTATUS* setInfo(int code, const std::string& info) {
CSTATUS* setInfo(const int& code, const std::string& info) {
error_code_ = code;
error_info_ = (STATUS_OK == error_code_) ? CGRAPH_EMPTY : info;
return this;
Expand All @@ -163,8 +163,8 @@ class CSTATUS {
}

private:
int error_code_ = STATUS_OK; // 错误码信息
std::string error_info_; // 错误信息描述
int error_code_ { STATUS_OK }; // 错误码信息
std::string error_info_; // 错误信息描述
};

CGRAPH_INTERNAL_NAMESPACE_END
Expand Down
8 changes: 4 additions & 4 deletions src/GraphCtrl/GraphPipeline/GPipeline.inl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ CStatus GPipeline::registerGElement(GTemplateNodePtr<Args ...> *elementRef,
CGRAPH_ASSERT_INIT(false)

// 构建模板node信息
(*elementRef) = new(std::nothrow) TNode(std::forward<Args &&>(args)...);
(*elementRef) = new(std::nothrow) TNode(std::forward<Args>(args)...);
CGRAPH_ASSERT_NOT_NULL(*elementRef)

status = innerRegister(*elementRef, depends, CGRAPH_EMPTY, CGRAPH_DEFAULT_LOOP_TIMES);
Expand Down Expand Up @@ -125,7 +125,7 @@ TNode* GPipeline::createGNode(const GNodeInfo &info, Args&&... args) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT_THROW_ERROR(false)

auto* node = new(std::nothrow) TNode(std::forward<Args &&>(args)...);
auto* node = new(std::nothrow) TNode(std::forward<Args>(args)...);
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(node)
status = node->addElementInfo(info.dependence_, info.name_, info.loop_);
CGRAPH_THROW_EXCEPTION_BY_STATUS(status)
Expand All @@ -140,7 +140,7 @@ template<typename TNode, typename ...Args,
TNode* GPipeline::createGNode(const GElementPtrSet& dependence, const std::string& name,
CSize loop, Args&&... args) {
const GNodeInfo& info = GNodeInfo(dependence, name, loop);
return createGNode<TNode>(info, std::forward<Args &&>(args)...);
return createGNode<TNode>(info, std::forward<Args>(args)...);
}


Expand Down Expand Up @@ -221,7 +221,7 @@ GPipelinePtr GPipeline::addGDaemon(CMSec ms, Args&&... args) {
CGRAPH_ASSERT_INIT_THROW_ERROR(false)
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(param_manager_, daemon_manager_)

auto daemon = CAllocator::safeMallocTemplateCObject<TDaemon>(std::forward<Args &&>(args)...);
auto daemon = CAllocator::safeMallocTemplateCObject<TDaemon>(std::forward<Args>(args)...);
daemon->setInterval(ms);
daemon->setGParamManager(this->param_manager_);
daemon->setGEventManager(this->event_manager_);
Expand Down
20 changes: 10 additions & 10 deletions src/UtilsCtrl/ThreadPool/Thread/UThreadPrimary.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class UThreadPrimary : public UThreadBase {
* @return
*/
CBool popTask(UTaskRef task) {
auto result = primary_queue_.tryPop(task) || secondary_queue_.tryPop(task);
const auto& result = primary_queue_.tryPop(task) || secondary_queue_.tryPop(task);
return result;
}

Expand All @@ -173,7 +173,7 @@ class UThreadPrimary : public UThreadBase {
*/
CBool popTask(UTaskArrRef tasks) {
CBool result = primary_queue_.tryPop(tasks, config_->max_local_batch_size_);
auto leftSize = config_->max_local_batch_size_ - tasks.size();
const auto& leftSize = config_->max_local_batch_size_ - tasks.size();
if (leftSize > 0) {
// 如果凑齐了,就不需要了。没凑齐的话,就继续
result |= (secondary_queue_.tryPop(tasks, leftSize));
Expand All @@ -187,8 +187,8 @@ class UThreadPrimary : public UThreadBase {
* @param task
* @return
*/
CBool stealTask(UTaskRef task) {
if (unlikely(pool_threads_->size() < (CSize)(config_->default_thread_size_))) {
CBool stealTask(UTaskRef task) const {
if (unlikely(pool_threads_->size() < static_cast<CSize>(config_->default_thread_size_))) {
/**
* 线程池还未初始化完毕的时候,无法进行steal。
* 确保程序安全运行。
Expand Down Expand Up @@ -224,16 +224,16 @@ class UThreadPrimary : public UThreadBase {
* @param tasks
* @return
*/
CBool stealTask(UTaskArrRef tasks) {
if (unlikely(pool_threads_->size() != (CSize)(config_->default_thread_size_))) {
CBool stealTask(UTaskArrRef tasks) const {
if (unlikely(pool_threads_->size() != static_cast<CSize>(config_->default_thread_size_))) {
return false;
}

CBool result = false;
for (auto& target : steal_targets_) {
for (const auto& target : steal_targets_) {
if (likely((*pool_threads_)[target])) {
result = ((*pool_threads_)[target])->secondary_queue_.trySteal(tasks, config_->max_steal_batch_size_);
auto leftSize = config_->max_steal_batch_size_ - tasks.size();
const auto& leftSize = config_->max_steal_batch_size_ - tasks.size();
if (leftSize > 0) {
result |= ((*pool_threads_)[target])->primary_queue_.trySteal(tasks, leftSize);
}
Expand Down Expand Up @@ -268,8 +268,8 @@ class UThreadPrimary : public UThreadBase {
}

private:
CInt index_; // 线程index
CInt cur_empty_epoch_ = 0; // 当前空转的轮数信息
CInt index_ {0}; // 线程index
CInt cur_empty_epoch_ {0}; // 当前空转的轮数信息
UWorkStealingQueue<UTask> primary_queue_; // 内部队列信息
UWorkStealingQueue<UTask> secondary_queue_; // 第二个队列,用于减少触锁概率,提升性能
std::vector<UThreadPrimary *>* pool_threads_; // 用于存放线程池中的线程信息
Expand Down
4 changes: 2 additions & 2 deletions src/UtilsCtrl/ThreadPool/UThreadPool.inl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ auto UThreadPool::commitWithPriority(const FunctionType& func, int priority)


template<typename FunctionType>
CVoid UThreadPool::execute(FunctionType&& task, CIndex index) {
CIndex realIndex = dispatch(index);
CVoid UThreadPool::execute(FunctionType&& task, const CIndex index) {
const CIndex& realIndex = dispatch(index);
if (realIndex >= 0 && realIndex < config_.default_thread_size_) {
primary_threads_[realIndex]->pushTask(std::forward<FunctionType>(task));
} else if (CGRAPH_LONG_TIME_TASK_STRATEGY == realIndex) {
Expand Down
Loading