Rev 1599 | Rev 1646 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1599 | Rev 1601 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Manager.h" |
21 | #include "Manager.h" |
| 22 | 22 | ||
| 23 | namespace atom { |
23 | namespace atom { |
| 24 | namespace timer { |
24 | namespace timer { |
| 25 | 25 | ||
| 26 | Manager::Pointer Manager::instance_; |
26 | Manager::Pointer Manager::instance_; |
| 27 | 27 | ||
| 28 | Manager::Manager( |
28 | Manager::Manager() |
| 29 | { |
29 | { |
| 30 | boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_)); |
- | |
| 31 | this->thread_ = thread.move(); |
- | |
| 32 | } |
30 | } |
| 33 | 31 | ||
| 34 | Manager::~Manager() |
32 | Manager::~Manager() |
| 35 | { |
33 | { |
| 36 | this-> |
34 | this->io_service_.stop(); |
| - | 35 | ||
| 37 | this->timers_.clear(); |
36 | this->timers_.clear(); |
| 38 | this->mutex_timers_.unlock(); |
- | |
| 39 | - | ||
| 40 | this->io_service_.stop(); |
- | |
| 41 | - | ||
| 42 | this->thread_.interrupt(); |
- | |
| 43 | this->thread_.join(); |
- | |
| 44 | } |
37 | } |
| 45 | 38 | ||
| 46 | Manager::Pointer Manager::Instance() |
39 | Manager::Pointer Manager::Instance() |
| 47 | { |
40 | { |
| 48 | return Manager::instance_; |
41 | return Manager::instance_; |
| 49 | } |
42 | } |
| 50 | 43 | ||
| 51 | void Manager::Create() |
44 | void Manager::Create() |
| 52 | { |
45 | { |
| 53 | Manager::instance_ = Manager::Pointer(new Manager()); |
46 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 54 | } |
47 | } |
| 55 | 48 | ||
| 56 | void Manager::Delete() |
49 | void Manager::Delete() |
| 57 | { |
50 | { |
| 58 | Manager::instance_.reset(); |
51 | Manager::instance_.reset(); |
| 59 | } |
52 | } |
| 60 | 53 | ||
| 61 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
54 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
| 62 | { |
55 | { |
| 63 | this->signal_on_timeout_.connect(slot_on_timeout); |
56 | this->signal_on_timeout_.connect(slot_on_timeout); |
| 64 | } |
57 | } |
| 65 | 58 | ||
| 66 | void Manager::SlotOnTimeout(TimerId id) |
59 | void Manager::SlotOnTimeout(TimerId id) |
| 67 | { |
60 | { |
| - | 61 | bool repeat = false; |
|
| - | 62 | ||
| 68 | this->mutex_timers_.lock(); |
63 | this->mutex_timers_.lock(); |
| 69 | 64 | ||
| 70 | TimerList::iterator it = this->timers_.find(id); |
65 | TimerList::iterator it = this->timers_.find(id); |
| 71 | 66 | ||
| 72 | if (it != this->timers_.end()) |
67 | if (it != this->timers_.end()) |
| 73 | { |
68 | { |
| 74 |
|
69 | repeat = it->second->GetRepeat(); |
| - | 70 | ||
| - | 71 | if (!repeat) |
|
| 75 | { |
72 | { |
| 76 | this->timers_.erase(id); |
73 | this->timers_.erase(id); |
| 77 | } |
74 | } |
| 78 | } |
75 | } |
| 79 | 76 | ||
| 80 | this->mutex_timers_.unlock(); |
77 | this->mutex_timers_.unlock(); |
| 81 | 78 | ||
| 82 | this->signal_on_timeout_( |
79 | this->signal_on_timeout_(id, repeat); |
| 83 | } |
80 | } |
| 84 | 81 | ||
| 85 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
82 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
| 86 | { |
83 | { |
| 87 | this->mutex_timers_.lock(); |
84 | this->mutex_timers_.lock(); |
| 88 | 85 | ||
| 89 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
86 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
| 90 | 87 | ||
| 91 | this->timers_[timer->GetId()] = timer; |
88 | this->timers_[timer->GetId()] = timer; |
| 92 | 89 | ||
| 93 | this->mutex_timers_.unlock(); |
90 | this->mutex_timers_.unlock(); |
| 94 | 91 | ||
| 95 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
92 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
| 96 | 93 | ||
| 97 | timer->Start(); |
94 | timer->Start(); |
| 98 | 95 | ||
| 99 | return timer->GetId(); |
96 | return timer->GetId(); |
| 100 | } |
97 | } |
| 101 | 98 | ||
| 102 | void Manager::Cancel(TimerId id) |
99 | void Manager::Cancel(TimerId id) |
| 103 | { |
100 | { |
| 104 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
101 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
| 105 | } |
102 | } |
| 106 | 103 | ||
| 107 | void Manager::CancelHandler(TimerId id) |
104 | void Manager::CancelHandler(TimerId id) |
| 108 | { |
105 | { |
| 109 | this->mutex_timers_.lock(); |
106 | this->mutex_timers_.lock(); |
| 110 | 107 | ||
| 111 | TimerList::iterator it = this->timers_.find(id); |
108 | TimerList::iterator it = this->timers_.find(id); |
| 112 | 109 | ||
| 113 | if (it != this->timers_.end()) |
110 | if (it != this->timers_.end()) |
| 114 | { |
111 | { |
| 115 | it->second->Cancel(); |
112 | it->second->Cancel(); |
| 116 | } |
113 | } |
| 117 | 114 | ||
| 118 | this->mutex_timers_.unlock(); |
115 | this->mutex_timers_.unlock(); |
| 119 | } |
116 | } |
| 120 | 117 | ||
| 121 | TimerId Manager::GetFreeId() |
118 | TimerId Manager::GetFreeId() |
| 122 | { |
119 | { |
| 123 | TimerId id = 1; |
120 | TimerId id = 1; |
| 124 | 121 | ||
| 125 | while (this->timers_.find(id) != this->timers_.end()) |
122 | while (this->timers_.find(id) != this->timers_.end()) |
| 126 | { |
123 | { |
| 127 | id++; |
124 | id++; |
| 128 | } |
125 | } |
| 129 | 126 | ||
| 130 | return id; |
127 | return id; |
| 131 | } |
128 | } |
| 132 | 129 | ||
| 133 | }; // namespace timer |
130 | }; // namespace timer |
| 134 | }; // namespace atom |
131 | }; // namespace atom |
| 135 | 132 | ||