AUTO_LT_SYNC
[tore.git] / libtorrent / src / alert.cpp
blob14c5c4e5cc40afd7cf4c020300223aa2a5d4d038
1 /*
3 Copyright (c) 2003, Arvid Norberg, Daniel Wallin
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/pch.hpp"
35 #include "libtorrent/alert.hpp"
36 #include <boost/thread/xtime.hpp>
38 namespace libtorrent {
40 alert::alert() : m_timestamp(time_now()) {}
41 alert::~alert() {}
42 ptime alert::timestamp() const { return m_timestamp; }
44 alert_manager::alert_manager()
45 : m_alert_mask(alert::error_notification)
46 , m_queue_size_limit(queue_size_limit_default)
49 alert_manager::~alert_manager()
51 while (!m_alerts.empty())
53 delete m_alerts.front();
54 m_alerts.pop();
58 alert const* alert_manager::wait_for_alert(time_duration max_wait)
60 boost::mutex::scoped_lock lock(m_mutex);
62 if (!m_alerts.empty()) return m_alerts.front();
64 int secs = total_seconds(max_wait);
65 max_wait -= seconds(secs);
66 boost::xtime xt;
67 boost::xtime_get(&xt, boost::TIME_UTC);
68 xt.sec += secs;
69 boost::int64_t nsec = xt.nsec + total_microseconds(max_wait) * 1000;
70 if (nsec > 1000000000)
72 nsec -= 1000000000;
73 xt.sec += 1;
75 xt.nsec = boost::xtime::xtime_nsec_t(nsec);
76 // apparently this call can be interrupted
77 // prematurely if there are other signals
78 if (!m_condition.timed_wait(lock, xt)) return 0;
79 if (m_alerts.empty()) return 0;
80 return m_alerts.front();
83 void alert_manager::post_alert(const alert& alert_)
85 boost::mutex::scoped_lock lock(m_mutex);
87 if (m_alerts.size() >= m_queue_size_limit) return;
88 m_alerts.push(alert_.clone().release());
89 m_condition.notify_all();
92 std::auto_ptr<alert> alert_manager::get()
94 boost::mutex::scoped_lock lock(m_mutex);
96 TORRENT_ASSERT(!m_alerts.empty());
98 alert* result = m_alerts.front();
99 m_alerts.pop();
100 return std::auto_ptr<alert>(result);
103 bool alert_manager::pending() const
105 boost::mutex::scoped_lock lock(m_mutex);
107 return !m_alerts.empty();
110 size_t alert_manager::set_alert_queue_size_limit(size_t queue_size_limit_)
112 boost::mutex::scoped_lock lock(m_mutex);
114 std::swap(m_queue_size_limit, queue_size_limit_);
115 return queue_size_limit_;
118 } // namespace libtorrent