-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97bb91f
commit d7ccb78
Showing
5 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// RunLoop.cpp | ||
// TaskLoop | ||
// | ||
// Created by pansafeimager on 15/12/9. | ||
// Copyright © 2015年 imager. All rights reserved. | ||
// | ||
|
||
#include <thread> | ||
#include <future> | ||
#include "RunLoop.h" | ||
|
||
namespace task | ||
{ | ||
Runloop::Runloop() | ||
{ | ||
|
||
} | ||
|
||
Runloop::~Runloop() | ||
{ | ||
|
||
} | ||
void Runloop::AddRunner(const Clouser& clouser) | ||
{ | ||
_waitqueue.Add(clouser); | ||
} | ||
void Runloop::DoLoop() | ||
{ | ||
for (;;) | ||
{ | ||
if (_waitqueue.Empty()) { | ||
_waitqueue.WaitforWork(); | ||
} | ||
WaitQueue<Clouser>::QueueType queue; | ||
_waitqueue.ReloadWaitQueue(queue); | ||
|
||
while(!queue.empty()){ | ||
Clouser clouser = queue.front(); | ||
Schedule(clouser); | ||
queue.pop(); | ||
} | ||
|
||
} | ||
} | ||
Runloop* Runloop::Create() { | ||
auto ploop = new Runloop(); | ||
std::thread(std::bind(&Runloop::DoLoop,ploop)).detach(); | ||
return ploop; | ||
} | ||
|
||
void Runloop::Schedule(Clouser& clouser) | ||
{ | ||
clouser.Run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// RunLoop.hpp | ||
// TaskLoop | ||
// | ||
// Created by pansafeimager on 15/12/9. | ||
// Copyright © 2015年 imager. All rights reserved. | ||
// | ||
|
||
#ifndef RunLoop_hpp | ||
#define RunLoop_hpp | ||
|
||
#include "Runner.hpp" | ||
#include "WaitQueue.hpp" | ||
namespace task { | ||
class Runloop | ||
{ | ||
public: | ||
static Runloop* Create(); | ||
|
||
void AddRunner(const Clouser& clouser); | ||
|
||
void DoLoop(); | ||
|
||
virtual void Schedule(Clouser& clouser); | ||
|
||
protected: | ||
|
||
Runloop(); | ||
~Runloop(); | ||
WaitQueue<Clouser> _waitqueue; | ||
}; | ||
} | ||
|
||
#endif /* RunLoop_hpp */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// Runner.hpp | ||
// TaskLoop | ||
// | ||
// Created by pansafeimager on 15/12/9. | ||
// Copyright © 2015年 imager. All rights reserved. | ||
// | ||
|
||
#ifndef Runner_hpp | ||
#define Runner_hpp | ||
|
||
#include <functional> | ||
|
||
namespace task { | ||
|
||
template<class T> | ||
class Runner; | ||
|
||
template<class R,class... Args> | ||
class Runner<R(Args...)> | ||
{ | ||
typedef R return_type; | ||
typedef std::function<R(Args...)> func_type; | ||
|
||
public: | ||
|
||
Runner(const func_type& func) | ||
:_func(func) | ||
{ | ||
|
||
} | ||
|
||
Runner(const Runner& other) | ||
{ | ||
_func = other._func; | ||
} | ||
|
||
Runner& operator=(const Runner& other) | ||
{ | ||
_func = other._func; | ||
} | ||
return_type Run() | ||
{ | ||
return _func(); | ||
} | ||
protected: | ||
func_type _func; | ||
}; | ||
typedef Runner<void(void)> Clouser; | ||
} | ||
|
||
#endif /* Runner_hpp */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// WaitQueue.h | ||
// TaskLoop | ||
// | ||
// Created by pansafeimager on 15/12/9. | ||
// Copyright © 2015年 imager. All rights reserved. | ||
// | ||
|
||
#ifndef WaitQueue_h | ||
#define WaitQueue_h | ||
#include <queue> | ||
|
||
#include <condition_variable> | ||
|
||
namespace task { | ||
|
||
template<typename T> | ||
class RunnerQueue: public std::queue<T> | ||
{ | ||
public: | ||
void Swap(RunnerQueue<T>& queue) | ||
{ | ||
this->c.swap(queue.c); | ||
} | ||
}; | ||
|
||
template<typename T> | ||
class WaitQueue | ||
{ | ||
public: | ||
|
||
typedef RunnerQueue<T> QueueType; | ||
typedef typename QueueType::value_type value_type; | ||
|
||
WaitQueue():_waitqueue(new QueueType) | ||
{ | ||
|
||
} | ||
|
||
~WaitQueue(){} | ||
void Add(value_type v) | ||
{ | ||
{ | ||
std::lock_guard<std::mutex> lock(_mutex_queue); | ||
_waitqueue->push(v); | ||
} | ||
Notify(); | ||
} | ||
|
||
//is empty | ||
bool Empty() | ||
{ | ||
std::lock_guard<std::mutex> lock(_mutex_queue); | ||
return _waitqueue->empty(); | ||
} | ||
//get size of waitqueue | ||
size_t Size() | ||
{ | ||
std::lock_guard<std::mutex> lk(_mutex_queue); | ||
return _waitqueue->size(); | ||
} | ||
|
||
//swap queue | ||
size_t ReloadWaitQueue(QueueType& queue) | ||
{ | ||
std::lock_guard<std::mutex> lk(_mutex_queue); | ||
_waitqueue->Swap(queue); | ||
return queue.size(); | ||
} | ||
|
||
//wait for new task | ||
void WaitforWork() | ||
{ | ||
std::unique_lock<std::mutex> lk(_mutex_con); | ||
while(_waitqueue->empty()) | ||
{ | ||
_con_var.wait(lk); | ||
} | ||
lk.release(); | ||
} | ||
//notify all waiting thread | ||
void Notify() | ||
{ | ||
_con_var.notify_all(); | ||
} | ||
protected: | ||
QueueType* _waitqueue; | ||
std::mutex _mutex_queue; | ||
std::mutex _mutex_con; | ||
std::condition_variable _con_var; | ||
}; | ||
} | ||
|
||
#endif /* WaitQueue_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// main.cpp | ||
// TaskLoop | ||
// | ||
// Created by pansafeimager on 15/12/9. | ||
// Copyright © 2015年 imager. All rights reserved. | ||
// | ||
|
||
#include <iostream> | ||
#include "RunLoop.h" | ||
|
||
void Func() | ||
{ | ||
std::cout<<"hello world!" << std::endl; | ||
} | ||
|
||
int Add(int a, int b){ | ||
std::cout<< "call Add()" << std::endl; | ||
return a + b; | ||
} | ||
int main(int argc, const char * argv[]) { | ||
|
||
task::Runloop* loop = task::Runloop::Create(); | ||
|
||
task::Clouser clouser([]{Add(1,2);}); | ||
|
||
loop->AddRunner(clouser); | ||
loop->AddRunner(clouser); | ||
|
||
while(1); | ||
return 1; | ||
} |