libreoffice-online/loolwsd/tsqueue.h
Tor Lillqvist 629ba87139 Add a "canceltiles" message to the protocol and handle it
Implementing this Was harder than I first expected. The basic idea is as
follows: The master process puts each message arriving from a client that
isn't "canceltiles" into a (client-specific) queue. A separate thread that
pulls messages from the queue at its own pace and handles them as
before. Incoming "canceltiles" messages are handled specially, though: The
queue is emptied of "tile" messages.

The above sounds simple but there are several details that were a bit tricky
to get right.
2015-06-09 18:29:48 +03:00

65 lines
1.5 KiB
C++

/* -*- 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_TSQUEUE_H
#define INCLUDED_TSQUEUE_H
#include "config.h"
#include <condition_variable>
#include <mutex>
#include <deque>
// Thread-safe queue
template <class T>
class tsqueue
{
public:
void put(const T& value)
{
std::unique_lock<std::mutex> lock(_mutex);
_queue.push_back(value);
lock.unlock();
_cv.notify_one();
}
T get()
{
std::unique_lock<std::mutex> lock(_mutex);
_cv.wait(lock, [this] { return _queue.size() > 0; });
T result = _queue.front();
_queue.pop_front();
return result;
}
void clear()
{
std::unique_lock<std::mutex> lock(_mutex);
while (_queue.size())
_queue.pop_front();
}
template<class UnaryPredicate>
void remove_if(UnaryPredicate p)
{
std::unique_lock<std::mutex> lock(_mutex);
_queue.erase(std::remove_if(_queue.begin(), _queue.end(), p),
_queue.end());
}
private:
std::mutex _mutex;
std::condition_variable _cv;
std::deque<T> _queue;
};
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */