2015-11-09 04:36:37 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_MESSAGEQUEUE_HPP
|
|
|
|
#define INCLUDED_MESSAGEQUEUE_HPP
|
|
|
|
|
2016-09-23 03:37:33 -05:00
|
|
|
#include <algorithm>
|
2015-11-09 04:36:37 -06:00
|
|
|
#include <condition_variable>
|
2016-11-16 07:47:11 -06:00
|
|
|
#include <functional>
|
2016-09-14 16:42:50 -05:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2018-08-29 11:38:19 -05:00
|
|
|
#include <string>
|
2016-03-26 21:32:29 -05:00
|
|
|
#include <vector>
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
/// Thread-safe message queue (FIFO).
|
|
|
|
template <typename T>
|
|
|
|
class MessageQueueBase
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
|
|
|
public:
|
2017-01-09 12:38:07 -06:00
|
|
|
typedef T Payload;
|
2016-03-26 21:32:29 -05:00
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
MessageQueueBase()
|
2015-11-24 02:41:59 -06:00
|
|
|
{
|
|
|
|
}
|
2016-03-26 21:32:29 -05:00
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
virtual ~MessageQueueBase()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
MessageQueueBase(const MessageQueueBase&) = delete;
|
|
|
|
MessageQueueBase& operator=(const MessageQueueBase&) = delete;
|
2015-11-09 04:36:37 -06:00
|
|
|
|
|
|
|
/// Thread safe insert the message.
|
2017-01-09 12:38:07 -06:00
|
|
|
void put(const Payload& value)
|
|
|
|
{
|
2017-01-19 22:24:23 -06:00
|
|
|
if (value.empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Cannot queue empty item.");
|
|
|
|
}
|
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
put_impl(value);
|
|
|
|
lock.unlock();
|
|
|
|
_cv.notify_one();
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:56:10 -05:00
|
|
|
void put(const std::string& value)
|
|
|
|
{
|
|
|
|
put(Payload(value.data(), value.data() + value.size()));
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
|
|
|
/// Thread safe obtaining of the message.
|
2016-10-09 10:59:00 -05:00
|
|
|
/// timeoutMs can be 0 to signify infinity.
|
2017-01-19 22:24:23 -06:00
|
|
|
/// Returns an empty payload on timeout.
|
2017-01-09 12:38:07 -06:00
|
|
|
Payload get(const unsigned timeoutMs = 0)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
if (timeoutMs > 0)
|
|
|
|
{
|
|
|
|
if (!_cv.wait_for(lock, std::chrono::milliseconds(timeoutMs),
|
2017-01-19 22:24:23 -06:00
|
|
|
[this] { return wait_impl(); }))
|
2017-01-09 12:38:07 -06:00
|
|
|
{
|
2017-01-19 22:24:23 -06:00
|
|
|
return Payload();
|
2017-01-09 12:38:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_cv.wait(lock, [this] { return wait_impl(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_impl();
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
/// Thread safe removal of all the pending messages.
|
2017-01-09 12:38:07 -06:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
clear_impl();
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2015-12-13 22:36:46 -06:00
|
|
|
/// Thread safe remove_if.
|
2017-01-09 12:38:07 -06:00
|
|
|
void remove_if(const std::function<bool(const Payload&)>& pred)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
std::remove_if(_queue.begin(), _queue.end(), pred);
|
|
|
|
}
|
2015-12-13 22:36:46 -06:00
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
protected:
|
2017-01-09 12:38:07 -06:00
|
|
|
virtual void put_impl(const Payload& value)
|
|
|
|
{
|
|
|
|
_queue.push_back(value);
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
bool wait_impl() const
|
|
|
|
{
|
|
|
|
return _queue.size() > 0;
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
virtual Payload get_impl()
|
|
|
|
{
|
|
|
|
Payload result = _queue.front();
|
|
|
|
_queue.erase(_queue.begin());
|
|
|
|
return result;
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
void clear_impl()
|
|
|
|
{
|
|
|
|
_queue.clear();
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2017-01-01 12:04:24 -06:00
|
|
|
/// Get the queue lock when accessing members of derived classes.
|
|
|
|
std::unique_lock<std::mutex> getLock() { return std::unique_lock<std::mutex>(_mutex); }
|
|
|
|
|
2018-11-16 02:06:08 -06:00
|
|
|
std::vector<Payload>& getQueue() { return _queue; }
|
2017-01-01 12:04:24 -06:00
|
|
|
|
|
|
|
private:
|
2018-11-16 02:06:08 -06:00
|
|
|
std::vector<Payload> _queue;
|
2017-01-19 22:24:23 -06:00
|
|
|
mutable std::mutex _mutex;
|
2017-01-01 12:04:24 -06:00
|
|
|
std::condition_variable _cv;
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
};
|
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
typedef MessageQueueBase<std::vector<char>> MessageQueue;
|
|
|
|
|
|
|
|
/// MessageQueue specialized for priority handling of tiles.
|
2016-09-25 10:50:01 -05:00
|
|
|
class TileQueue : public MessageQueue
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-09-30 15:02:22 -05:00
|
|
|
friend class TileQueueTests;
|
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
private:
|
|
|
|
class CursorPosition
|
|
|
|
{
|
|
|
|
public:
|
2018-11-15 02:07:27 -06:00
|
|
|
CursorPosition() {}
|
|
|
|
CursorPosition(int part, int x, int y, int width, int height)
|
|
|
|
: _part(part)
|
|
|
|
, _x(x)
|
|
|
|
, _y(y)
|
|
|
|
, _width(width)
|
|
|
|
, _height(height)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int getPart() const { return _part; }
|
|
|
|
int getX() const { return _x; }
|
|
|
|
int getY() const { return _y; }
|
|
|
|
int getWidth() const { return _width; }
|
|
|
|
int getHeight() const { return _height; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int _part = 0;
|
|
|
|
int _x = 0;
|
|
|
|
int _y = 0;
|
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
2016-09-14 16:42:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
void updateCursorPosition(int viewId, int part, int x, int y, int width, int height)
|
|
|
|
{
|
2018-11-15 02:07:27 -06:00
|
|
|
const TileQueue::CursorPosition cursorPosition = CursorPosition(part, x, y, width, height);
|
2017-01-01 12:04:24 -06:00
|
|
|
|
2018-02-07 03:17:19 -06:00
|
|
|
std::unique_lock<std::mutex> lock = getLock();
|
2017-01-01 12:04:24 -06:00
|
|
|
|
2017-01-01 12:05:52 -06:00
|
|
|
auto it = _cursorPositions.lower_bound(viewId);
|
|
|
|
if (it != _cursorPositions.end() && it->first == viewId)
|
2016-09-14 16:42:50 -05:00
|
|
|
{
|
|
|
|
it->second = cursorPosition;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-01 12:05:52 -06:00
|
|
|
_cursorPositions.insert(it, std::make_pair(viewId, cursorPosition));
|
2016-09-14 16:42:50 -05:00
|
|
|
}
|
2016-09-23 03:37:33 -05:00
|
|
|
|
2016-09-25 11:38:17 -05:00
|
|
|
// Move to front, so the current front view
|
|
|
|
// becomes the second.
|
|
|
|
const auto view = std::find(_viewOrder.begin(), _viewOrder.end(), viewId);
|
2016-09-23 03:37:33 -05:00
|
|
|
if (view != _viewOrder.end())
|
|
|
|
{
|
2016-09-25 11:38:17 -05:00
|
|
|
_viewOrder.erase(view);
|
2016-09-23 03:37:33 -05:00
|
|
|
}
|
2016-09-25 11:38:17 -05:00
|
|
|
|
2016-09-30 15:38:20 -05:00
|
|
|
_viewOrder.push_back(viewId);
|
2016-09-14 16:42:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeCursorPosition(int viewId)
|
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
std::unique_lock<std::mutex> lock = getLock();
|
2017-01-01 12:04:24 -06:00
|
|
|
|
2016-09-25 11:38:17 -05:00
|
|
|
const auto view = std::find(_viewOrder.begin(), _viewOrder.end(), viewId);
|
|
|
|
if (view != _viewOrder.end())
|
|
|
|
{
|
|
|
|
_viewOrder.erase(view);
|
|
|
|
}
|
2016-09-23 03:37:33 -05:00
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
_cursorPositions.erase(viewId);
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
protected:
|
2016-04-01 07:31:26 -05:00
|
|
|
virtual void put_impl(const Payload& value) override;
|
2016-09-14 16:42:50 -05:00
|
|
|
|
2016-09-15 07:40:26 -05:00
|
|
|
virtual Payload get_impl() override;
|
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
private:
|
2016-09-26 03:35:20 -05:00
|
|
|
/// Search the queue for a duplicate tile and remove it (if present).
|
2017-01-20 08:56:47 -06:00
|
|
|
void removeTileDuplicate(const std::string& tileMsg);
|
|
|
|
|
|
|
|
/// Search the queue for a duplicate callback and remove it (if present).
|
|
|
|
///
|
|
|
|
/// This removes also callbacks that are made invalid by the current
|
|
|
|
/// message, like the new cursor position invalidates the old one etc.
|
2017-01-25 11:21:56 -06:00
|
|
|
///
|
|
|
|
/// @return New message to put into the queue. If empty, use what was in callbackMsg.
|
|
|
|
std::string removeCallbackDuplicate(const std::string& callbackMsg);
|
2016-09-14 16:42:50 -05:00
|
|
|
|
2017-01-25 12:14:38 -06:00
|
|
|
/// De-prioritize the previews (tiles with 'id') - move them to the end of
|
|
|
|
/// the queue.
|
|
|
|
void deprioritizePreviews();
|
2016-10-04 07:23:42 -05:00
|
|
|
|
2016-09-30 15:00:20 -05:00
|
|
|
/// Priority of the given tile message.
|
2016-09-30 15:38:20 -05:00
|
|
|
/// -1 means the lowest prio (the tile does not intersect any of the cursors),
|
|
|
|
/// the higher the number, the bigger is priority [up to _viewOrder.size()-1].
|
2016-09-30 15:00:20 -05:00
|
|
|
int priority(const std::string& tileMsg);
|
2016-09-14 16:42:50 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<int, CursorPosition> _cursorPositions;
|
2016-09-23 03:37:33 -05:00
|
|
|
|
|
|
|
/// Check the views in the order of how the editing (cursor movement) has
|
2016-09-30 15:38:20 -05:00
|
|
|
/// been happening (0 == oldest, size() - 1 == newest).
|
|
|
|
std::vector<int> _viewOrder;
|
2015-11-09 04:36:37 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|