3 Copyright (c) 2003, Arvid Norberg, Daniel Wallin
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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 enum { queue_size_limit
= 1000 };
40 namespace libtorrent
{
42 alert::alert() : m_timestamp(time_now()) {}
44 ptime
alert::timestamp() const { return m_timestamp
; }
46 alert_manager::alert_manager()
47 : m_alert_mask(alert::error_notification
)
50 alert_manager::~alert_manager()
52 while (!m_alerts
.empty())
54 delete m_alerts
.front();
59 alert
const* alert_manager::wait_for_alert(time_duration max_wait
)
61 boost::mutex::scoped_lock
lock(m_mutex
);
63 if (!m_alerts
.empty()) return m_alerts
.front();
65 int secs
= total_seconds(max_wait
);
66 max_wait
-= seconds(secs
);
68 boost::xtime_get(&xt
, boost::TIME_UTC
);
70 boost::int64_t nsec
= xt
.nsec
+ total_microseconds(max_wait
) * 1000;
71 if (nsec
> 1000000000)
76 xt
.nsec
= boost::xtime::xtime_nsec_t(nsec
);
77 if (!m_condition
.timed_wait(lock
, xt
)) return 0;
78 TORRENT_ASSERT(!m_alerts
.empty());
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() >= 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();
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 } // namespace libtorrent