mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
upgrade limonp to version v0.5.1
This commit is contained in:
parent
3c5ad24260
commit
3ab9a34909
204
deps/limonp/Closure.hpp
vendored
204
deps/limonp/Closure.hpp
vendored
@ -3,28 +3,202 @@
|
|||||||
|
|
||||||
namespace limonp {
|
namespace limonp {
|
||||||
|
|
||||||
class ITask {
|
class ClosureInterface {
|
||||||
public:
|
public:
|
||||||
virtual ~ITask() {
|
virtual ~ClosureInterface() {
|
||||||
}
|
}
|
||||||
virtual void Run() = 0;
|
virtual void Run() = 0;
|
||||||
}; // class ITask
|
};
|
||||||
|
|
||||||
template <class TaskType>
|
template <class Funct>
|
||||||
ITask* CreateTask() {
|
class Closure0: public ClosureInterface {
|
||||||
return new TaskType();
|
public:
|
||||||
|
Closure0(Funct fun) {
|
||||||
|
fun_ = fun;
|
||||||
|
}
|
||||||
|
virtual ~Closure0() {
|
||||||
|
}
|
||||||
|
virtual void Run() {
|
||||||
|
(*fun_)();
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
Funct fun_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class Funct, class Arg1>
|
||||||
|
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 Funct, class Arg1, class Arg2>
|
||||||
|
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 Funct, class Arg1, class Arg2, class Arg3>
|
||||||
|
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 Obj, class Funct>
|
||||||
|
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 Obj, class Funct, class Arg1>
|
||||||
|
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 Obj, class Funct, class Arg1, class Arg2>
|
||||||
|
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 Obj, class Funct, class Arg1, class Arg2, class Arg3>
|
||||||
|
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<class R>
|
||||||
|
ClosureInterface* NewClosure(R (*fun)()) {
|
||||||
|
return new Closure0<R (*)()>(fun);
|
||||||
}
|
}
|
||||||
template <class TaskType, class ArgType>
|
|
||||||
ITask* CreateTask(ArgType arg) {
|
template<class R, class Arg1>
|
||||||
return new TaskType(arg);
|
ClosureInterface* NewClosure(R (*fun)(Arg1), Arg1 arg1) {
|
||||||
|
return new Closure1<R (*)(Arg1), Arg1>(fun, arg1);
|
||||||
}
|
}
|
||||||
template <class TaskType, class ArgType0, class ArgType1>
|
|
||||||
ITask* CreateTask(ArgType0 arg0, ArgType1 arg1) {
|
template<class R, class Arg1, class Arg2>
|
||||||
return new TaskType(arg0, arg1);
|
ClosureInterface* NewClosure(R (*fun)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) {
|
||||||
|
return new Closure2<R (*)(Arg1, Arg2), Arg1, Arg2>(fun, arg1, arg2);
|
||||||
}
|
}
|
||||||
template <class TaskType, class ArgType0, class ArgType1, class ArgType2>
|
|
||||||
ITask* CreateTask(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2) {
|
template<class R, class Arg1, class Arg2, class Arg3>
|
||||||
return new TaskType(arg0, arg1, arg2);
|
ClosureInterface* NewClosure(R (*fun)(Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
||||||
|
return new Closure3<R (*)(Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(fun, arg1, arg2, arg3);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class R, class Obj>
|
||||||
|
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)()) {
|
||||||
|
return new ObjClosure0<Obj, R (Obj::* )()>(obj, fun);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class R, class Obj, class Arg1>
|
||||||
|
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1), Arg1 arg1) {
|
||||||
|
return new ObjClosure1<Obj, R (Obj::* )(Arg1), Arg1>(obj, fun, arg1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class R, class Obj, class Arg1, class Arg2>
|
||||||
|
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) {
|
||||||
|
return new ObjClosure2<Obj, R (Obj::*)(Arg1, Arg2), Arg1, Arg2>(obj, fun, arg1, arg2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class R, class Obj, class Arg1, class Arg2, class Arg3>
|
||||||
|
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
||||||
|
return new ObjClosure3<Obj, R (Obj::*)(Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(obj, fun, arg1, arg2, arg3);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace limonp
|
} // namespace limonp
|
||||||
|
49
deps/limonp/ThreadPool.hpp
vendored
49
deps/limonp/ThreadPool.hpp
vendored
@ -22,46 +22,35 @@ class ThreadPool: NonCopyable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual void Run() {
|
virtual void Run() {
|
||||||
while(true) {
|
while (true) {
|
||||||
ITask * task = ptThreadPool_->queue_.Pop();
|
ClosureInterface* closure = ptThreadPool_->queue_.Pop();
|
||||||
if(task == NULL) {
|
if (closure == NULL) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
task->Run();
|
closure->Run();
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
cerr << "file:" << __FILE__
|
LOG(ERROR) << e.what();
|
||||||
<< ", line:" << __LINE__
|
|
||||||
<< ", " << e.what() << endl;
|
|
||||||
} catch(...) {
|
} catch(...) {
|
||||||
cerr << "file:" << __FILE__
|
LOG(ERROR) << " unknown exception.";
|
||||||
<< ", line:" << __LINE__
|
|
||||||
<< ", unknown exception." << endl;
|
|
||||||
}
|
}
|
||||||
delete task;
|
delete closure;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
ThreadPool * ptThreadPool_;
|
ThreadPool * ptThreadPool_;
|
||||||
}; // class Worker
|
}; // class Worker
|
||||||
|
|
||||||
ThreadPool(size_t threadNum, size_t queueMaxSize)
|
ThreadPool(size_t thread_num)
|
||||||
: threads_(threadNum),
|
: threads_(thread_num),
|
||||||
queue_(queueMaxSize) {
|
queue_(thread_num) {
|
||||||
assert(threadNum);
|
assert(thread_num);
|
||||||
assert(queueMaxSize);
|
|
||||||
for(size_t i = 0; i < threads_.size(); i ++) {
|
for(size_t i = 0; i < threads_.size(); i ++) {
|
||||||
threads_[i] = new Worker(this);
|
threads_[i] = new Worker(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~ThreadPool() {
|
~ThreadPool() {
|
||||||
for(size_t i = 0; i < threads_.size(); i ++) {
|
Stop();
|
||||||
queue_.Push(NULL);
|
|
||||||
}
|
|
||||||
for(size_t i = 0; i < threads_.size(); i ++) {
|
|
||||||
threads_[i]->Join();
|
|
||||||
delete threads_[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start() {
|
void Start() {
|
||||||
@ -69,8 +58,18 @@ class ThreadPool: NonCopyable {
|
|||||||
threads_[i]->Start();
|
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);
|
assert(task);
|
||||||
queue_.Push(task);
|
queue_.Push(task);
|
||||||
}
|
}
|
||||||
@ -79,7 +78,7 @@ class ThreadPool: NonCopyable {
|
|||||||
friend class Worker;
|
friend class Worker;
|
||||||
|
|
||||||
vector<IThread*> threads_;
|
vector<IThread*> threads_;
|
||||||
BoundedBlockingQueue<ITask*> queue_;
|
BoundedBlockingQueue<ClosureInterface*> queue_;
|
||||||
}; // class ThreadPool
|
}; // class ThreadPool
|
||||||
|
|
||||||
} // namespace limonp
|
} // namespace limonp
|
||||||
|
Loading…
x
Reference in New Issue
Block a user