2012-06-17 00:29:13 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2017-11-08 23:56:20 +01:00
|
|
|
#include "irrlichttypes.h"
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include "threading/mutex_auto_lock.h"
|
|
|
|
#include "threading/semaphore.h"
|
2012-12-20 18:19:49 +01:00
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
2013-08-11 04:09:45 +02:00
|
|
|
#include <map>
|
2015-01-13 13:28:21 +01:00
|
|
|
#include <set>
|
|
|
|
#include <queue>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
/*
|
2015-01-13 13:28:21 +01:00
|
|
|
Queue with unique values with fast checking of value existence
|
2012-06-17 00:29:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
template<typename Value>
|
|
|
|
class UniqueQueue
|
|
|
|
{
|
|
|
|
public:
|
2015-01-13 13:28:21 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
/*
|
2015-01-13 13:28:21 +01:00
|
|
|
Does nothing if value is already queued.
|
|
|
|
Return value:
|
|
|
|
true: value added
|
|
|
|
false: value already exists
|
2012-06-17 00:29:13 +02:00
|
|
|
*/
|
2015-01-13 13:28:21 +01:00
|
|
|
bool push_back(const Value& value)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-01-13 13:28:21 +01:00
|
|
|
if (m_set.insert(value).second)
|
|
|
|
{
|
|
|
|
m_queue.push(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2015-01-13 13:28:21 +01:00
|
|
|
void pop_front()
|
|
|
|
{
|
|
|
|
m_set.erase(m_queue.front());
|
|
|
|
m_queue.pop();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 13:28:21 +01:00
|
|
|
const Value& front() const
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-01-13 13:28:21 +01:00
|
|
|
return m_queue.front();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 13:28:21 +01:00
|
|
|
u32 size() const
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-01-13 13:28:21 +01:00
|
|
|
return m_queue.size();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-13 13:28:21 +01:00
|
|
|
std::set<Value> m_set;
|
|
|
|
std::queue<Value> m_queue;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
class MutexedMap
|
|
|
|
{
|
|
|
|
public:
|
2017-08-19 22:23:47 +02:00
|
|
|
MutexedMap() = default;
|
2015-01-13 13:28:21 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
void set(const Key &name, const Value &value)
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2012-06-17 00:29:13 +02:00
|
|
|
m_values[name] = value;
|
|
|
|
}
|
2015-01-13 13:28:21 +01:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
bool get(const Key &name, Value *result) const
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2021-04-05 13:38:31 +02:00
|
|
|
auto n = m_values.find(name);
|
2015-04-07 12:13:12 +02:00
|
|
|
if (n == m_values.end())
|
2012-06-17 00:29:13 +02:00
|
|
|
return false;
|
2015-04-07 12:13:12 +02:00
|
|
|
if (result)
|
2012-12-20 18:19:49 +01:00
|
|
|
*result = n->second;
|
2012-06-17 00:29:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
std::vector<Value> getValues() const
|
2012-11-30 17:12:32 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2015-03-04 17:18:57 +01:00
|
|
|
std::vector<Value> result;
|
2021-04-05 13:38:31 +02:00
|
|
|
result.reserve(m_values.size());
|
|
|
|
for (auto it = m_values.begin(); it != m_values.end(); ++it)
|
2015-04-07 12:13:12 +02:00
|
|
|
result.push_back(it->second);
|
2012-11-30 17:12:32 +01:00
|
|
|
return result;
|
|
|
|
}
|
2015-01-13 13:28:21 +01:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
void clear() { m_values.clear(); }
|
2012-11-30 17:12:32 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
private:
|
2012-12-20 18:19:49 +01:00
|
|
|
std::map<Key, Value> m_values;
|
2017-06-06 16:29:28 +02:00
|
|
|
mutable std::mutex m_mutex;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
// Thread-safe Double-ended queue
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class MutexedQueue
|
|
|
|
{
|
|
|
|
public:
|
2014-01-06 12:45:42 +01:00
|
|
|
template<typename Key, typename U, typename Caller, typename CallerData>
|
|
|
|
friend class RequestQueue;
|
|
|
|
|
2017-08-19 22:23:47 +02:00
|
|
|
MutexedQueue() = default;
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
bool empty() const
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
|
|
|
return m_queue.empty();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2015-04-07 12:13:12 +02:00
|
|
|
|
2021-04-05 13:38:31 +02:00
|
|
|
void push_back(const T &t)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2015-03-05 20:54:36 +01:00
|
|
|
m_queue.push_back(t);
|
2015-04-07 12:13:12 +02:00
|
|
|
m_signal.post();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2014-01-06 12:45:42 +01:00
|
|
|
|
|
|
|
/* this version of pop_front returns a empty element of T on timeout.
|
2015-01-13 13:28:21 +01:00
|
|
|
* Make sure default constructor of T creates a recognizable "empty" element
|
|
|
|
*/
|
2014-01-06 12:45:42 +01:00
|
|
|
T pop_frontNoEx(u32 wait_time_max_ms)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
if (m_signal.wait(wait_time_max_ms)) {
|
|
|
|
MutexAutoLock lock(m_mutex);
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2021-04-05 13:38:31 +02:00
|
|
|
T t = std::move(m_queue.front());
|
2015-03-05 20:54:36 +01:00
|
|
|
m_queue.pop_front();
|
2014-01-06 12:45:42 +01:00
|
|
|
return t;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2017-08-19 22:23:47 +02:00
|
|
|
|
|
|
|
return T();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2014-01-06 12:45:42 +01:00
|
|
|
|
|
|
|
T pop_front(u32 wait_time_max_ms)
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
if (m_signal.wait(wait_time_max_ms)) {
|
|
|
|
MutexAutoLock lock(m_mutex);
|
2014-01-06 12:45:42 +01:00
|
|
|
|
2021-04-05 13:38:31 +02:00
|
|
|
T t = std::move(m_queue.front());
|
2015-03-05 20:54:36 +01:00
|
|
|
m_queue.pop_front();
|
2014-01-06 12:45:42 +01:00
|
|
|
return t;
|
|
|
|
}
|
2017-08-19 22:23:47 +02:00
|
|
|
|
|
|
|
throw ItemNotFoundException("MutexedQueue: queue is empty");
|
2014-01-06 12:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
T pop_frontNoEx()
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
m_signal.wait();
|
2014-01-06 12:45:42 +01:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2014-01-06 12:45:42 +01:00
|
|
|
|
2021-04-05 13:38:31 +02:00
|
|
|
T t = std::move(m_queue.front());
|
2015-03-05 20:54:36 +01:00
|
|
|
m_queue.pop_front();
|
2014-01-06 12:45:42 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
T pop_back(u32 wait_time_max_ms=0)
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
if (m_signal.wait(wait_time_max_ms)) {
|
|
|
|
MutexAutoLock lock(m_mutex);
|
2014-01-06 12:45:42 +01:00
|
|
|
|
2021-04-05 13:38:31 +02:00
|
|
|
T t = std::move(m_queue.back());
|
2015-03-05 20:54:36 +01:00
|
|
|
m_queue.pop_back();
|
2014-01-06 12:45:42 +01:00
|
|
|
return t;
|
|
|
|
}
|
2017-08-19 22:23:47 +02:00
|
|
|
|
|
|
|
throw ItemNotFoundException("MutexedQueue: queue is empty");
|
2014-01-06 12:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this version of pop_back returns a empty element of T on timeout.
|
2015-01-13 13:28:21 +01:00
|
|
|
* Make sure default constructor of T creates a recognizable "empty" element
|
|
|
|
*/
|
2015-04-07 12:13:12 +02:00
|
|
|
T pop_backNoEx(u32 wait_time_max_ms)
|
2014-01-06 12:45:42 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
if (m_signal.wait(wait_time_max_ms)) {
|
|
|
|
MutexAutoLock lock(m_mutex);
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2021-04-05 13:38:31 +02:00
|
|
|
T t = std::move(m_queue.back());
|
2015-03-05 20:54:36 +01:00
|
|
|
m_queue.pop_back();
|
2014-01-06 12:45:42 +01:00
|
|
|
return t;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2017-08-19 22:23:47 +02:00
|
|
|
|
|
|
|
return T();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2014-01-06 12:45:42 +01:00
|
|
|
T pop_backNoEx()
|
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
m_signal.wait();
|
2014-01-06 12:45:42 +01:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2014-01-06 12:45:42 +01:00
|
|
|
|
2021-04-05 13:38:31 +02:00
|
|
|
T t = std::move(m_queue.back());
|
2015-03-05 20:54:36 +01:00
|
|
|
m_queue.pop_back();
|
2014-01-06 12:45:42 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-06-06 16:29:28 +02:00
|
|
|
std::mutex &getMutex() { return m_mutex; }
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
std::deque<T> &getQueue() { return m_queue; }
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2015-03-05 20:54:36 +01:00
|
|
|
std::deque<T> m_queue;
|
2017-06-06 16:29:28 +02:00
|
|
|
mutable std::mutex m_mutex;
|
2015-04-07 12:13:12 +02:00
|
|
|
Semaphore m_signal;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
2015-07-11 02:24:00 +02:00
|
|
|
template<typename K, typename V>
|
|
|
|
class LRUCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LRUCache(size_t limit, void (*cache_miss)(void *data, const K &key, V *dest),
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
m_limit = limit;
|
|
|
|
m_cache_miss = cache_miss;
|
|
|
|
m_cache_miss_data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLimit(size_t limit)
|
|
|
|
{
|
|
|
|
m_limit = limit;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void invalidate()
|
|
|
|
{
|
|
|
|
m_map.clear();
|
|
|
|
m_queue.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
const V *lookupCache(K key)
|
|
|
|
{
|
|
|
|
typename cache_type::iterator it = m_map.find(key);
|
|
|
|
V *ret;
|
|
|
|
if (it != m_map.end()) {
|
|
|
|
// found!
|
|
|
|
|
|
|
|
cache_entry_t &entry = it->second;
|
|
|
|
|
|
|
|
ret = &entry.second;
|
|
|
|
|
|
|
|
// update the usage information
|
|
|
|
m_queue.erase(entry.first);
|
|
|
|
m_queue.push_front(key);
|
|
|
|
entry.first = m_queue.begin();
|
|
|
|
} else {
|
|
|
|
// cache miss -- enter into cache
|
|
|
|
cache_entry_t &entry =
|
|
|
|
m_map[key];
|
|
|
|
ret = &entry.second;
|
|
|
|
m_cache_miss(m_cache_miss_data, key, &entry.second);
|
|
|
|
|
|
|
|
// delete old entries
|
|
|
|
if (m_queue.size() == m_limit) {
|
|
|
|
const K &id = m_queue.back();
|
|
|
|
m_map.erase(id);
|
|
|
|
m_queue.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_queue.push_front(key);
|
|
|
|
entry.first = m_queue.begin();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void (*m_cache_miss)(void *data, const K &key, V *dest);
|
|
|
|
void *m_cache_miss_data;
|
|
|
|
size_t m_limit;
|
|
|
|
typedef typename std::template pair<typename std::template list<K>::iterator, V> cache_entry_t;
|
|
|
|
typedef std::template map<K, cache_entry_t> cache_type;
|
|
|
|
cache_type m_map;
|
|
|
|
// we can't use std::deque here, because its iterators get invalidated
|
|
|
|
std::list<K> m_queue;
|
|
|
|
};
|