From 3ab9a349091a4cbe3b58d4b233f61f59f2a6b7bf Mon Sep 17 00:00:00 2001 From: yanyiwu Date: Mon, 11 Jan 2016 14:30:38 +0800 Subject: [PATCH] upgrade limonp to version v0.5.1 --- deps/limonp/Closure.hpp | 204 ++++++++++++++++++++++++++++++++++--- deps/limonp/ThreadPool.hpp | 49 +++++---- 2 files changed, 213 insertions(+), 40 deletions(-) diff --git a/deps/limonp/Closure.hpp b/deps/limonp/Closure.hpp index 0d135ea..c9d9dd4 100644 --- a/deps/limonp/Closure.hpp +++ b/deps/limonp/Closure.hpp @@ -3,28 +3,202 @@ namespace limonp { -class ITask { +class ClosureInterface { public: - virtual ~ITask() { + virtual ~ClosureInterface() { } virtual void Run() = 0; -}; // class ITask +}; -template -ITask* CreateTask() { - return new TaskType(); +template +class Closure0: public ClosureInterface { + public: + Closure0(Funct fun) { + fun_ = fun; + } + virtual ~Closure0() { + } + virtual void Run() { + (*fun_)(); + } + private: + Funct fun_; +}; + +template +class Closure1: public ClosureInterface { + public: + Closure1(Funct fun, Arg1 arg1) { + fun_ = fun; + arg1_ = arg1; + } + virtual ~Closure1() { + } + virtual void Run() { + (*fun_)(arg1_); + } + private: + Funct fun_; + Arg1 arg1_; +}; + +template +class Closure2: public ClosureInterface { + public: + Closure2(Funct fun, Arg1 arg1, Arg2 arg2) { + fun_ = fun; + arg1_ = arg1; + arg2_ = arg2; + } + virtual ~Closure2() { + } + virtual void Run() { + (*fun_)(arg1_, arg2_); + } + private: + Funct fun_; + Arg1 arg1_; + Arg2 arg2_; +}; + +template +class Closure3: public ClosureInterface { + public: + Closure3(Funct fun, Arg1 arg1, Arg2 arg2, Arg3 arg3) { + fun_ = fun; + arg1_ = arg1; + arg2_ = arg2; + arg3_ = arg3; + } + virtual ~Closure3() { + } + virtual void Run() { + (*fun_)(arg1_, arg2_, arg3_); + } + private: + Funct fun_; + Arg1 arg1_; + Arg2 arg2_; + Arg3 arg3_; +}; + +template +class ObjClosure0: public ClosureInterface { + public: + ObjClosure0(Obj* p, Funct fun) { + p_ = p; + fun_ = fun; + } + virtual ~ObjClosure0() { + } + virtual void Run() { + (p_->*fun_)(); + } + private: + Obj* p_; + Funct fun_; +}; + +template +class ObjClosure1: public ClosureInterface { + public: + ObjClosure1(Obj* p, Funct fun, Arg1 arg1) { + p_ = p; + fun_ = fun; + arg1_ = arg1; + } + virtual ~ObjClosure1() { + } + virtual void Run() { + (p_->*fun_)(arg1_); + } + private: + Obj* p_; + Funct fun_; + Arg1 arg1_; +}; + +template +class ObjClosure2: public ClosureInterface { + public: + ObjClosure2(Obj* p, Funct fun, Arg1 arg1, Arg2 arg2) { + p_ = p; + fun_ = fun; + arg1_ = arg1; + arg2_ = arg2; + } + virtual ~ObjClosure2() { + } + virtual void Run() { + (p_->*fun_)(arg1_, arg2_); + } + private: + Obj* p_; + Funct fun_; + Arg1 arg1_; + Arg2 arg2_; +}; +template +class ObjClosure3: public ClosureInterface { + public: + ObjClosure3(Obj* p, Funct fun, Arg1 arg1, Arg2 arg2, Arg3 arg3) { + p_ = p; + fun_ = fun; + arg1_ = arg1; + arg2_ = arg2; + arg3_ = arg3; + } + virtual ~ObjClosure3() { + } + virtual void Run() { + (p_->*fun_)(arg1_, arg2_, arg3_); + } + private: + Obj* p_; + Funct fun_; + Arg1 arg1_; + Arg2 arg2_; + Arg3 arg3_; +}; + +template +ClosureInterface* NewClosure(R (*fun)()) { + return new Closure0(fun); } -template -ITask* CreateTask(ArgType arg) { - return new TaskType(arg); + +template +ClosureInterface* NewClosure(R (*fun)(Arg1), Arg1 arg1) { + return new Closure1(fun, arg1); } -template -ITask* CreateTask(ArgType0 arg0, ArgType1 arg1) { - return new TaskType(arg0, arg1); + +template +ClosureInterface* NewClosure(R (*fun)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) { + return new Closure2(fun, arg1, arg2); } -template -ITask* CreateTask(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2) { - return new TaskType(arg0, arg1, arg2); + +template +ClosureInterface* NewClosure(R (*fun)(Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3) { + return new Closure3(fun, arg1, arg2, arg3); +} + +template +ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)()) { + return new ObjClosure0(obj, fun); +} + +template +ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1), Arg1 arg1) { + return new ObjClosure1(obj, fun, arg1); +} + +template +ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) { + return new ObjClosure2(obj, fun, arg1, arg2); +} + +template +ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3) { + return new ObjClosure3(obj, fun, arg1, arg2, arg3); } } // namespace limonp diff --git a/deps/limonp/ThreadPool.hpp b/deps/limonp/ThreadPool.hpp index 13e77f9..dda6bd9 100644 --- a/deps/limonp/ThreadPool.hpp +++ b/deps/limonp/ThreadPool.hpp @@ -22,46 +22,35 @@ class ThreadPool: NonCopyable { } virtual void Run() { - while(true) { - ITask * task = ptThreadPool_->queue_.Pop(); - if(task == NULL) { + while (true) { + ClosureInterface* closure = ptThreadPool_->queue_.Pop(); + if (closure == NULL) { break; } try { - task->Run(); + closure->Run(); } catch(std::exception& e) { - cerr << "file:" << __FILE__ - << ", line:" << __LINE__ - << ", " << e.what() << endl; + LOG(ERROR) << e.what(); } catch(...) { - cerr << "file:" << __FILE__ - << ", line:" << __LINE__ - << ", unknown exception." << endl; + LOG(ERROR) << " unknown exception."; } - delete task; + delete closure; } } private: ThreadPool * ptThreadPool_; }; // class Worker - ThreadPool(size_t threadNum, size_t queueMaxSize) - : threads_(threadNum), - queue_(queueMaxSize) { - assert(threadNum); - assert(queueMaxSize); + ThreadPool(size_t thread_num) + : threads_(thread_num), + queue_(thread_num) { + assert(thread_num); for(size_t i = 0; i < threads_.size(); i ++) { threads_[i] = new Worker(this); } } ~ThreadPool() { - for(size_t i = 0; i < threads_.size(); i ++) { - queue_.Push(NULL); - } - for(size_t i = 0; i < threads_.size(); i ++) { - threads_[i]->Join(); - delete threads_[i]; - } + Stop(); } void Start() { @@ -69,8 +58,18 @@ class ThreadPool: NonCopyable { threads_[i]->Start(); } } + void Stop() { + for(size_t i = 0; i < threads_.size(); i ++) { + queue_.Push(NULL); + } + for(size_t i = 0; i < threads_.size(); i ++) { + threads_[i]->Join(); + delete threads_[i]; + } + threads_.clear(); + } - void Add(ITask* task) { + void Add(ClosureInterface* task) { assert(task); queue_.Push(task); } @@ -79,7 +78,7 @@ class ThreadPool: NonCopyable { friend class Worker; vector threads_; - BoundedBlockingQueue queue_; + BoundedBlockingQueue queue_; }; // class ThreadPool } // namespace limonp