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>
|
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)
|
|
|
|
{
|
|
|
|
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-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),
|
|
|
|
[this] { return wait_impl(); }))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Timed out waiting to get queue item.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
const auto msg = std::string(value.data(), value.size());
|
|
|
|
_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); }
|
|
|
|
|
|
|
|
protected:
|
2016-11-26 21:47:17 -06:00
|
|
|
std::vector<Payload> _queue;
|
2017-01-01 12:04:24 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex _mutex;
|
|
|
|
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:
|
|
|
|
int Part;
|
|
|
|
int X;
|
|
|
|
int Y;
|
|
|
|
int Width;
|
|
|
|
int Height;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2016-11-29 20:55:44 -06:00
|
|
|
|
|
|
|
TileQueue() :
|
|
|
|
_lastTileGetTime(std::chrono::steady_clock::now()),
|
|
|
|
_lastGetTime(_lastTileGetTime),
|
|
|
|
_lastGetTile(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
void updateCursorPosition(int viewId, int part, int x, int y, int width, int height)
|
|
|
|
{
|
2017-01-01 12:05:52 -06:00
|
|
|
const auto cursorPosition = CursorPosition({ part, x, y, width, height });
|
2017-01-01 12:04:24 -06:00
|
|
|
|
|
|
|
auto lock = getLock();
|
|
|
|
|
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)
|
|
|
|
{
|
2017-01-01 12:04:24 -06:00
|
|
|
auto lock = getLock();
|
|
|
|
|
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).
|
|
|
|
void removeDuplicate(const std::string& tileMsg);
|
2016-09-14 16:42:50 -05:00
|
|
|
|
2016-11-29 20:55:44 -06:00
|
|
|
/// Find the index of the first non-preview entry.
|
|
|
|
/// When preferTiles is false, it'll return index of
|
|
|
|
/// the first non-tile, otherwise, the index of the
|
|
|
|
/// first tile is returned.
|
|
|
|
/// Returns -1 if only previews are left.
|
|
|
|
int findFirstNonPreview(bool preferTiles) const;
|
|
|
|
|
|
|
|
/// Returns true if we should try to return
|
|
|
|
/// a tile, otherwise a non-tile.
|
|
|
|
bool shouldPreferTiles() const;
|
|
|
|
|
|
|
|
/// Update the tile/non-tile timestamps to
|
|
|
|
/// track how much time we spend for each.
|
|
|
|
/// isTile marks if the current message
|
|
|
|
/// is a tile or not.
|
|
|
|
void updateTimestamps(const bool isTile);
|
|
|
|
|
|
|
|
/// Given a positive index, move it to the top.
|
|
|
|
void bumpToTop(const size_t index);
|
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;
|
2016-11-29 20:55:44 -06:00
|
|
|
|
|
|
|
std::chrono::steady_clock::time_point _lastTileGetTime;
|
|
|
|
std::chrono::steady_clock::time_point _lastGetTime;
|
|
|
|
bool _lastGetTile;
|
|
|
|
|
|
|
|
/// For responsiveness, we shouldn't have higher latency.
|
|
|
|
static const int MaxTileSkipDurationMs = 100;
|
2015-11-09 04:36:37 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|