File tree Expand file tree Collapse file tree 5 files changed +268
-0
lines changed Expand file tree Collapse file tree 5 files changed +268
-0
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // RunLoop.cpp
3
+ // TaskLoop
4
+ //
5
+ // Created by pansafeimager on 15/12/9.
6
+ // Copyright © 2015年 imager. All rights reserved.
7
+ //
8
+
9
+ #include < thread>
10
+ #include < future>
11
+ #include " RunLoop.h"
12
+
13
+ namespace task
14
+ {
15
+ Runloop::Runloop ()
16
+ {
17
+
18
+ }
19
+
20
+ Runloop::~Runloop ()
21
+ {
22
+
23
+ }
24
+ void Runloop::AddRunner (const Clouser& clouser)
25
+ {
26
+ _waitqueue.Add (clouser);
27
+ }
28
+ void Runloop::DoLoop ()
29
+ {
30
+ for (;;)
31
+ {
32
+ if (_waitqueue.Empty ()) {
33
+ _waitqueue.WaitforWork ();
34
+ }
35
+ WaitQueue<Clouser>::QueueType queue;
36
+ _waitqueue.ReloadWaitQueue (queue);
37
+
38
+ while (!queue.empty ()){
39
+ Clouser clouser = queue.front ();
40
+ Schedule (clouser);
41
+ queue.pop ();
42
+ }
43
+
44
+ }
45
+ }
46
+ Runloop* Runloop::Create () {
47
+ auto ploop = new Runloop ();
48
+ std::thread (std::bind (&Runloop::DoLoop,ploop)).detach ();
49
+ return ploop;
50
+ }
51
+
52
+ void Runloop::Schedule (Clouser& clouser)
53
+ {
54
+ clouser.Run ();
55
+ }
56
+ }
Original file line number Diff line number Diff line change
1
+ //
2
+ // RunLoop.hpp
3
+ // TaskLoop
4
+ //
5
+ // Created by pansafeimager on 15/12/9.
6
+ // Copyright © 2015年 imager. All rights reserved.
7
+ //
8
+
9
+ #ifndef RunLoop_hpp
10
+ #define RunLoop_hpp
11
+
12
+ #include " Runner.hpp"
13
+ #include " WaitQueue.hpp"
14
+ namespace task {
15
+ class Runloop
16
+ {
17
+ public:
18
+ static Runloop* Create ();
19
+
20
+ void AddRunner (const Clouser& clouser);
21
+
22
+ void DoLoop ();
23
+
24
+ virtual void Schedule (Clouser& clouser);
25
+
26
+ protected:
27
+
28
+ Runloop ();
29
+ ~Runloop ();
30
+ WaitQueue<Clouser> _waitqueue;
31
+ };
32
+ }
33
+
34
+ #endif /* RunLoop_hpp */
Original file line number Diff line number Diff line change
1
+ //
2
+ // Runner.hpp
3
+ // TaskLoop
4
+ //
5
+ // Created by pansafeimager on 15/12/9.
6
+ // Copyright © 2015年 imager. All rights reserved.
7
+ //
8
+
9
+ #ifndef Runner_hpp
10
+ #define Runner_hpp
11
+
12
+ #include < functional>
13
+
14
+ namespace task {
15
+
16
+ template <class T >
17
+ class Runner ;
18
+
19
+ template <class R ,class ... Args>
20
+ class Runner <R(Args...)>
21
+ {
22
+ typedef R return_type;
23
+ typedef std::function<R(Args...)> func_type;
24
+
25
+ public:
26
+
27
+ Runner (const func_type& func)
28
+ :_func(func)
29
+ {
30
+
31
+ }
32
+
33
+ Runner (const Runner& other)
34
+ {
35
+ _func = other._func ;
36
+ }
37
+
38
+ Runner& operator =(const Runner& other)
39
+ {
40
+ _func = other._func ;
41
+ }
42
+ return_type Run ()
43
+ {
44
+ return _func ();
45
+ }
46
+ protected:
47
+ func_type _func;
48
+ };
49
+ typedef Runner<void (void )> Clouser;
50
+ }
51
+
52
+ #endif /* Runner_hpp */
Original file line number Diff line number Diff line change
1
+ //
2
+ // WaitQueue.h
3
+ // TaskLoop
4
+ //
5
+ // Created by pansafeimager on 15/12/9.
6
+ // Copyright © 2015年 imager. All rights reserved.
7
+ //
8
+
9
+ #ifndef WaitQueue_h
10
+ #define WaitQueue_h
11
+ #include < queue>
12
+
13
+ #include < condition_variable>
14
+
15
+ namespace task {
16
+
17
+ template <typename T>
18
+ class RunnerQueue : public std ::queue<T>
19
+ {
20
+ public:
21
+ void Swap (RunnerQueue<T>& queue)
22
+ {
23
+ this ->c .swap (queue.c );
24
+ }
25
+ };
26
+
27
+ template <typename T>
28
+ class WaitQueue
29
+ {
30
+ public:
31
+
32
+ typedef RunnerQueue<T> QueueType;
33
+ typedef typename QueueType::value_type value_type;
34
+
35
+ WaitQueue ():_waitqueue(new QueueType)
36
+ {
37
+
38
+ }
39
+
40
+ ~WaitQueue (){}
41
+ void Add (value_type v)
42
+ {
43
+ {
44
+ std::lock_guard<std::mutex> lock (_mutex_queue);
45
+ _waitqueue->push (v);
46
+ }
47
+ Notify ();
48
+ }
49
+
50
+ // is empty
51
+ bool Empty ()
52
+ {
53
+ std::lock_guard<std::mutex> lock (_mutex_queue);
54
+ return _waitqueue->empty ();
55
+ }
56
+ // get size of waitqueue
57
+ size_t Size ()
58
+ {
59
+ std::lock_guard<std::mutex> lk (_mutex_queue);
60
+ return _waitqueue->size ();
61
+ }
62
+
63
+ // swap queue
64
+ size_t ReloadWaitQueue (QueueType& queue)
65
+ {
66
+ std::lock_guard<std::mutex> lk (_mutex_queue);
67
+ _waitqueue->Swap (queue);
68
+ return queue.size ();
69
+ }
70
+
71
+ // wait for new task
72
+ void WaitforWork ()
73
+ {
74
+ std::unique_lock<std::mutex> lk (_mutex_con);
75
+ while (_waitqueue->empty ())
76
+ {
77
+ _con_var.wait (lk);
78
+ }
79
+ lk.release ();
80
+ }
81
+ // notify all waiting thread
82
+ void Notify ()
83
+ {
84
+ _con_var.notify_all ();
85
+ }
86
+ protected:
87
+ QueueType* _waitqueue;
88
+ std::mutex _mutex_queue;
89
+ std::mutex _mutex_con;
90
+ std::condition_variable _con_var;
91
+ };
92
+ }
93
+
94
+ #endif /* WaitQueue_h */
Original file line number Diff line number Diff line change
1
+ //
2
+ // main.cpp
3
+ // TaskLoop
4
+ //
5
+ // Created by pansafeimager on 15/12/9.
6
+ // Copyright © 2015年 imager. All rights reserved.
7
+ //
8
+
9
+ #include < iostream>
10
+ #include " RunLoop.h"
11
+
12
+ void Func ()
13
+ {
14
+ std::cout<<" hello world!" << std::endl;
15
+ }
16
+
17
+ int Add (int a, int b){
18
+ std::cout<< " call Add()" << std::endl;
19
+ return a + b;
20
+ }
21
+ int main (int argc, const char * argv[]) {
22
+
23
+ task::Runloop* loop = task::Runloop::Create ();
24
+
25
+ task::Clouser clouser ([]{Add (1 ,2 );});
26
+
27
+ loop->AddRunner (clouser);
28
+ loop->AddRunner (clouser);
29
+
30
+ while (1 );
31
+ return 1 ;
32
+ }
You can’t perform that action at this time.
0 commit comments