2013-04-09 04:38:11 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2003-2016, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_ALERT_MANAGER_HPP_INCLUDED
|
|
|
|
#define TORRENT_ALERT_MANAGER_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
#include "libtorrent/alert.hpp"
|
2015-04-03 22:15:48 +02:00
|
|
|
#include "libtorrent/heterogeneous_queue.hpp"
|
|
|
|
#include "libtorrent/stack_allocator.hpp"
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2016-08-02 18:13:05 +02:00
|
|
|
#include <boost/function.hpp>
|
2013-04-09 04:38:11 +02:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2015-04-19 08:28:21 +02:00
|
|
|
#include <boost/config.hpp>
|
2013-04-09 04:38:11 +02:00
|
|
|
#include <list>
|
2015-04-03 22:15:48 +02:00
|
|
|
#include <utility> // for std::forward
|
2016-05-01 00:54:23 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2013-04-09 04:38:11 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
struct plugin;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class TORRENT_EXTRA_EXPORT alert_manager
|
|
|
|
{
|
|
|
|
public:
|
2013-07-20 01:31:44 +02:00
|
|
|
alert_manager(int queue_limit
|
2016-06-18 20:01:38 +02:00
|
|
|
, std::uint32_t alert_mask = alert::error_notification);
|
2013-04-09 04:38:11 +02:00
|
|
|
~alert_manager();
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
template <class T, typename... Args>
|
|
|
|
void emplace_alert(Args&&... args)
|
|
|
|
{
|
2016-05-01 00:54:23 +02:00
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
2016-05-13 14:19:44 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
// don't add more than this number of alerts, unless it's a
|
|
|
|
// high priority alert, in which case we try harder to deliver it
|
|
|
|
// for high priority alerts, double the upper limit
|
|
|
|
if (m_alerts[m_generation].size() >= m_queue_size_limit
|
|
|
|
* (1 + T::priority))
|
|
|
|
return;
|
|
|
|
|
2016-04-28 16:30:29 +02:00
|
|
|
T& alert = m_alerts[m_generation].emplace_back<T>(
|
|
|
|
m_allocations[m_generation], std::forward<Args>(args)...);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-04-07 02:50:21 +02:00
|
|
|
maybe_notify(&alert, lock);
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
|
2013-04-09 04:38:11 +02:00
|
|
|
bool pending() const;
|
2015-12-06 18:45:20 +01:00
|
|
|
void get_all(std::vector<alert*>& alerts);
|
2013-04-09 04:38:11 +02:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool should_post() const
|
|
|
|
{
|
2016-05-01 00:54:23 +02:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2015-04-03 22:15:48 +02:00
|
|
|
if (m_alerts[m_generation].size() >= m_queue_size_limit
|
|
|
|
* (1 + T::priority))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-09 04:38:11 +02:00
|
|
|
return (m_alert_mask & T::static_category) != 0;
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
alert* wait_for_alert(time_duration max_wait);
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
void set_alert_mask(std::uint32_t m)
|
2013-04-09 04:38:11 +02:00
|
|
|
{
|
2016-05-01 00:54:23 +02:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2013-04-09 04:38:11 +02:00
|
|
|
m_alert_mask = m;
|
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t alert_mask() const
|
2015-04-03 22:15:48 +02:00
|
|
|
{
|
2016-05-01 00:54:23 +02:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2015-04-03 22:15:48 +02:00
|
|
|
return m_alert_mask;
|
|
|
|
}
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-04 21:11:14 +02:00
|
|
|
int alert_queue_size_limit() const { return m_queue_size_limit; }
|
|
|
|
int set_alert_queue_size_limit(int queue_size_limit_);
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
void set_notify_function(boost::function<void()> const& fun);
|
|
|
|
|
2013-04-09 04:38:11 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
void add_extension(boost::shared_ptr<plugin> ext);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
// non-copyable
|
|
|
|
alert_manager(alert_manager const&);
|
|
|
|
alert_manager& operator=(alert_manager const&);
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
void maybe_notify(alert* a, std::unique_lock<std::mutex>& lock);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
mutable std::mutex m_mutex;
|
|
|
|
std::condition_variable m_condition;
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t m_alert_mask;
|
2015-04-04 21:11:14 +02:00
|
|
|
int m_queue_size_limit;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
// this function (if set) is called whenever the number of alerts in
|
|
|
|
// the alert queue goes from 0 to 1. The client is expected to wake up
|
|
|
|
// its main message loop for it to poll for alerts (using get_alerts()).
|
|
|
|
// That call will drain every alert in one atomic operation and this
|
|
|
|
// notification function will be called again the next time an alert is
|
|
|
|
// posted to the queue
|
|
|
|
boost::function<void()> m_notify;
|
2013-04-09 04:38:11 +02:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
// this is either 0 or 1, it indicates which m_alerts and m_allocations
|
|
|
|
// the alert_manager is allowed to use right now. This is swapped when
|
2015-05-28 16:46:12 +02:00
|
|
|
// the client calls get_all(), at which point all of the alert objects
|
|
|
|
// passed to the client will be owned by libtorrent again, and reset.
|
2015-04-03 22:15:48 +02:00
|
|
|
int m_generation;
|
|
|
|
|
2016-08-02 18:13:05 +02:00
|
|
|
// this is where all alerts are queued up. There are two heterogeneous
|
2016-05-01 00:54:23 +02:00
|
|
|
// queues to double buffer the thread access. The std::mutex in the alert
|
2015-04-03 22:15:48 +02:00
|
|
|
// manager gives exclusive access to m_alerts[m_generation] and
|
|
|
|
// m_allocations[m_generation] whereas the other copy is exclusively
|
|
|
|
// used by the client thread.
|
|
|
|
heterogeneous_queue<alert> m_alerts[2];
|
|
|
|
|
|
|
|
// this is a stack where alerts can allocate variable length content,
|
|
|
|
// such as strings, to go with the alerts.
|
|
|
|
aux::stack_allocator m_allocations[2];
|
|
|
|
|
2013-04-09 04:38:11 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2016-06-18 04:02:21 +02:00
|
|
|
std::list<boost::shared_ptr<plugin>> m_ses_extensions;
|
2013-04-09 04:38:11 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-05 07:37:01 +02:00
|
|
|
#endif
|