Skip to content

Commit

Permalink
add runloop files to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Dstonecool committed Dec 14, 2015
1 parent 97bb91f commit d7ccb78
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 0 deletions.
56 changes: 56 additions & 0 deletions TaskLoop/RunLoop.cpp
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();
}
}
34 changes: 34 additions & 0 deletions TaskLoop/RunLoop.h
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 */
52 changes: 52 additions & 0 deletions TaskLoop/Runner.hpp
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 */
94 changes: 94 additions & 0 deletions TaskLoop/WaitQueue.hpp
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 */
32 changes: 32 additions & 0 deletions TaskLoop/main.cpp
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;
}

0 comments on commit d7ccb78

Please sign in to comment.