Rev 1593 | Rev 1599 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1593 | Rev 1595 | ||
|---|---|---|---|
| 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_ = Manager::Pointer(new Manager()); |
26 | Manager::Pointer Manager::instance_ = Manager::Pointer(new Manager()); |
| 27 | 27 | ||
| 28 | Manager::Manager() : io_service_work_(io_service_) |
28 | Manager::Manager() : io_service_work_(io_service_) |
| 29 | { |
29 | { |
| 30 | boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_)); |
30 | boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_)); |
| 31 | this->thread_ = thread.move(); |
31 | this->thread_ = thread.move(); |
| 32 | } |
32 | } |
| 33 | 33 | ||
| 34 | Manager::~Manager() |
34 | Manager::~Manager() |
| 35 | { |
35 | { |
| - | 36 | this->mutex_timers_.lock(); |
|
| 36 | this->timers_.clear(); |
37 | this->timers_.clear(); |
| - | 38 | this->mutex_timers_.unlock(); |
|
| 37 | 39 | ||
| 38 | this->thread_.interrupt(); |
40 | this->thread_.interrupt(); |
| 39 | this->thread_.join(); |
41 | this->thread_.join(); |
| 40 | } |
42 | } |
| 41 | 43 | ||
| 42 | Manager::Pointer Manager::Instance() |
44 | Manager::Pointer Manager::Instance() |
| 43 | { |
45 | { |
| 44 | return Manager::instance_; |
46 | return Manager::instance_; |
| 45 | } |
47 | } |
| 46 | 48 | ||
| 47 | void Manager::Delete() |
49 | void Manager::Delete() |
| 48 | { |
50 | { |
| 49 | Manager::instance_.reset(); |
51 | Manager::instance_.reset(); |
| 50 | } |
52 | } |
| 51 | 53 | ||
| 52 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
54 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
| 53 | { |
55 | { |
| 54 | this->signal_on_timeout_.connect(slot_on_timeout); |
56 | this->signal_on_timeout_.connect(slot_on_timeout); |
| 55 | } |
57 | } |
| 56 | 58 | ||
| 57 | void Manager::SlotOnTimeout(TimerId id) |
59 | void Manager::SlotOnTimeout(TimerId id) |
| 58 | { |
60 | { |
| - | 61 | this->mutex_timers_.lock(); |
|
| - | 62 | ||
| 59 | TimerList::iterator it = this->timers_.find(id); |
63 | TimerList::iterator it = this->timers_.find(id); |
| 60 | 64 | ||
| 61 | if (it != this->timers_.end()) |
65 | if (it != this->timers_.end()) |
| 62 | { |
66 | { |
| 63 | if (!it->second->GetRepeat()) |
67 | if (!it->second->GetRepeat()) |
| 64 | { |
68 | { |
| 65 | this->timers_.erase(id); |
69 | this->timers_.erase(id); |
| 66 | } |
70 | } |
| 67 | } |
71 | } |
| - | 72 | ||
| - | 73 | this->mutex_timers_.unlock(); |
|
| 68 | 74 | ||
| 69 | this->signal_on_timeout_(id); |
75 | this->signal_on_timeout_(id); |
| 70 | } |
76 | } |
| 71 | 77 | ||
| 72 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
78 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
| 73 | { |
79 | { |
| - | 80 | this->mutex_timers_.lock(); |
|
| - | 81 | ||
| 74 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
82 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
| 75 | 83 | ||
| 76 | this->timers_[timer->GetId()] = timer; |
84 | this->timers_[timer->GetId()] = timer; |
| - | 85 | ||
| - | 86 | this->mutex_timers_.unlock(); |
|
| 77 | 87 | ||
| 78 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
88 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
| 79 | 89 | ||
| 80 | timer->Start(); |
90 | timer->Start(); |
| 81 | 91 | ||
| 82 | return timer->GetId(); |
92 | return timer->GetId(); |
| 83 | } |
93 | } |
| 84 | 94 | ||
| 85 | void Manager::Cancel(TimerId id) |
95 | void Manager::Cancel(TimerId id) |
| 86 | { |
96 | { |
| 87 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
97 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
| 88 | } |
98 | } |
| 89 | 99 | ||
| 90 | void Manager::CancelHandler(TimerId id) |
100 | void Manager::CancelHandler(TimerId id) |
| 91 | { |
101 | { |
| - | 102 | this->mutex_timers_.lock(); |
|
| - | 103 | ||
| 92 | TimerList::iterator it = this->timers_.find(id); |
104 | TimerList::iterator it = this->timers_.find(id); |
| 93 | 105 | ||
| 94 | if (it != this->timers_.end()) |
106 | if (it != this->timers_.end()) |
| 95 | { |
107 | { |
| 96 | it->second->Cancel(); |
108 | it->second->Cancel(); |
| 97 | } |
109 | } |
| - | 110 | ||
| - | 111 | this->mutex_timers_.unlock(); |
|
| 98 | } |
112 | } |
| 99 | 113 | ||
| 100 | TimerId Manager::GetFreeId() |
114 | TimerId Manager::GetFreeId() |
| 101 | { |
115 | { |
| 102 | TimerId id = 1; |
116 | TimerId id = 1; |
| 103 | 117 | ||
| 104 | while (this->timers_.find(id) != this->timers_.end()) |
118 | while (this->timers_.find(id) != this->timers_.end()) |
| 105 | { |
119 | { |
| 106 | id++; |
120 | id++; |
| 107 | } |
121 | } |
| 108 | 122 | ||
| 109 | return id; |
123 | return id; |
| 110 | } |
124 | } |
| 111 | - | ||
| 112 | 125 | ||
| 113 | }; // namespace timer |
126 | }; // namespace timer |
| 114 | }; // namespace atom |
127 | }; // namespace atom |
| 115 | 128 | ||