2013-04-09 04:38:11 +02:00
|
|
|
/*
|
|
|
|
|
2015-06-03 07:18:48 +02:00
|
|
|
Copyright (c) 2003-2015, Arvid Norberg, Daniel Wallin
|
2013-04-09 04:38:11 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
#include "libtorrent/alert_manager.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
#include "libtorrent/extensions.hpp"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2013-07-20 01:31:44 +02:00
|
|
|
alert_manager::alert_manager(int queue_limit, boost::uint32_t alert_mask)
|
2013-04-09 04:38:11 +02:00
|
|
|
: m_alert_mask(alert_mask)
|
|
|
|
, m_queue_size_limit(queue_limit)
|
2014-07-21 05:03:59 +02:00
|
|
|
, m_num_queued_resume(0)
|
2015-04-03 22:15:48 +02:00
|
|
|
, m_generation(0)
|
2013-04-09 04:38:11 +02:00
|
|
|
{}
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
alert_manager::~alert_manager() {}
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2014-07-21 05:03:59 +02:00
|
|
|
int alert_manager::num_queued_resume() const
|
|
|
|
{
|
|
|
|
mutex::scoped_lock lock(m_mutex);
|
|
|
|
return m_num_queued_resume;
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
alert* alert_manager::wait_for_alert(time_duration max_wait)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
|
|
|
mutex::scoped_lock lock(m_mutex);
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
if (!m_alerts[m_generation].empty())
|
|
|
|
return m_alerts[m_generation].front();
|
2015-05-18 01:32:13 +02:00
|
|
|
|
2013-04-09 04:38:11 +02:00
|
|
|
// this call can be interrupted prematurely by other signals
|
|
|
|
m_condition.wait_for(lock, max_wait);
|
2015-04-03 22:15:48 +02:00
|
|
|
if (!m_alerts[m_generation].empty())
|
|
|
|
return m_alerts[m_generation].front();
|
2013-04-09 04:38:11 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-07 02:50:21 +02:00
|
|
|
void alert_manager::maybe_notify(alert* a, mutex::scoped_lock& lock)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
2015-04-07 02:50:21 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (ses_extension_list_t::iterator i = m_ses_extensions.begin()
|
|
|
|
, end(m_ses_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
(*i)->on_alert(a);
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-11 20:44:43 +02:00
|
|
|
if (a->type() == save_resume_data_failed_alert::alert_type
|
2015-04-23 07:34:34 +02:00
|
|
|
|| a->type() == save_resume_data_alert::alert_type)
|
2015-04-11 20:44:43 +02:00
|
|
|
++m_num_queued_resume;
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
if (m_alerts[m_generation].size() == 1)
|
|
|
|
{
|
|
|
|
lock.unlock();
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
// we just posted to an empty queue. If anyone is waiting for
|
|
|
|
// alerts, we need to notify them. Also (potentially) call the
|
|
|
|
// user supplied m_notify callback to let the client wake up its
|
|
|
|
// message loop to poll for alerts.
|
|
|
|
if (m_notify) m_notify();
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
// TODO: 2 keep a count of the number of threads waiting. Only if it's
|
|
|
|
// > 0 notify them
|
|
|
|
m_condition.notify_all();
|
2013-04-09 04:38:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
bool alert_manager::maybe_dispatch(alert const& a)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
if (m_dispatch)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
m_dispatch(a.clone());
|
|
|
|
return true;
|
2013-04-09 04:38:11 +02:00
|
|
|
}
|
2015-04-03 22:15:48 +02:00
|
|
|
return false;
|
2013-04-09 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
void alert_manager::set_dispatch_function(
|
|
|
|
boost::function<void(std::auto_ptr<alert>)> const& fun)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
mutex::scoped_lock lock(m_mutex);
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
m_dispatch = fun;
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
heterogeneous_queue<alert> storage;
|
|
|
|
m_alerts[m_generation].swap(storage);
|
|
|
|
lock.unlock();
|
2014-07-21 05:03:59 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
std::vector<alert*> alerts;
|
|
|
|
storage.get_pointers(alerts);
|
2014-07-21 05:03:59 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
for (std::vector<alert*>::iterator i = alerts.begin()
|
|
|
|
, end(alerts.end()); i != end; ++i)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
m_dispatch((*i)->clone());
|
2013-04-09 04:38:11 +02:00
|
|
|
}
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void alert_manager::set_notify_function(boost::function<void()> const& fun)
|
|
|
|
{
|
|
|
|
mutex::scoped_lock lock(m_mutex);
|
|
|
|
m_notify = fun;
|
|
|
|
if (!m_alerts[m_generation].empty())
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
// never call a callback with the lock held!
|
|
|
|
lock.unlock();
|
|
|
|
m_notify();
|
2013-04-09 04:38:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
void alert_manager::add_extension(boost::shared_ptr<plugin> ext)
|
|
|
|
{
|
|
|
|
m_ses_extensions.push_back(ext);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
void alert_manager::get_all(std::vector<alert*>& alerts, int& num_resume)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
|
|
|
mutex::scoped_lock lock(m_mutex);
|
2015-04-03 22:15:48 +02:00
|
|
|
TORRENT_ASSERT(m_num_queued_resume <= m_alerts[m_generation].size());
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
alerts.clear();
|
|
|
|
if (m_alerts[m_generation].empty()) return;
|
2014-07-21 05:03:59 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
m_alerts[m_generation].get_pointers(alerts);
|
2014-07-21 05:03:59 +02:00
|
|
|
num_resume = m_num_queued_resume;
|
|
|
|
m_num_queued_resume = 0;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
// swap buffers
|
|
|
|
m_generation = (m_generation + 1) & 1;
|
|
|
|
// clear the one we will start writing to now
|
|
|
|
m_alerts[m_generation].clear();
|
|
|
|
m_allocations[m_generation].reset();
|
2013-04-09 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool alert_manager::pending() const
|
|
|
|
{
|
|
|
|
mutex::scoped_lock lock(m_mutex);
|
2015-04-03 22:15:48 +02:00
|
|
|
return !m_alerts[m_generation].empty();
|
2013-04-09 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2015-04-04 21:11:14 +02:00
|
|
|
int alert_manager::set_alert_queue_size_limit(int queue_size_limit_)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
|
|
|
mutex::scoped_lock lock(m_mutex);
|
|
|
|
|
|
|
|
std::swap(m_queue_size_limit, queue_size_limit_);
|
|
|
|
return queue_size_limit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|