Removed some depricated utility classes

- Removed CallerInfo, GetResult, ResultQueue, GetRequestRequestQueue
classes
- Refactor TextureSource to use C++11 promise/future
This commit is contained in:
Dániel Varga 2015-07-26 10:34:04 +02:00 committed by darkrose
parent db092abd94
commit 3479e5405c
3 changed files with 21 additions and 152 deletions

View File

@ -73,23 +73,17 @@ void TextureSource::processQueue()
/*
Fetch textures
*/
if(m_get_texture_queue.size() > 0)
std::lock_guard<std::mutex> guard(m_get_texture_queue_mutex);
while (!m_get_texture_queue.empty())
{
GetRequest<std::string, u32, u8, u8>
request = m_get_texture_queue.pop();
std::string name = m_get_texture_queue.front().first;
infostream<<"TextureSource::processQueue(): "
<<"got texture request with "
<<"name=\""<<request.key<<"\""
<<"name=\""<< name <<"\""
<<std::endl;
GetResult<std::string, u32, u8, u8>
result;
result.key = request.key;
result.callers = request.callers;
result.item = getTextureIdDirect(request.key);
request.dest->push_back(result);
m_get_texture_queue.front().second.set_value(getTextureIdDirect(name));
m_get_texture_queue.pop();
}
}
@ -116,29 +110,21 @@ u32 TextureSource::getTextureId(const std::string &name)
}else{
infostream<<"getTextureId(): Queued: name=\""<<name<<"\""<<std::endl;
// We're gonna ask the result to be put into here
ResultQueue<std::string, u32, u8, u8> result_queue;
std::future<u32> future;
{
std::lock_guard<std::mutex> guard(m_get_texture_queue_mutex);
// We're gonna ask the result to be put into here
std::promise<u32> promise;
future = promise.get_future();
// Throw a request in
m_get_texture_queue.add(name, 0, 0, &result_queue);
// Throw a request in
m_get_texture_queue.push(std::make_pair(name, std::move(promise)));
}
infostream<<"Waiting for texture from main thread, name=\""
<<name<<"\""<<std::endl;
try{
// Wait result for a second
GetResult<std::string, u32, u8, u8>
result = result_queue.pop_front(1000);
// Check that at least something worked OK
if (result.key != name)
return 0;
return result.item;
}catch(ItemNotFoundException &e) {
infostream<<"Waiting for texture timed out."<<std::endl;
return 0;
}
return future.get();
}
infostream<<"getTextureId(): Failed"<<std::endl;

View File

@ -30,6 +30,9 @@
#include "threads.h"
#include "utility.h"
#include <string>
#include <future>
#include <mutex>
#include <queue>
using namespace jthread;
@ -258,7 +261,8 @@ private:
JMutex m_atlaspointer_cache_mutex;
// Queued texture fetches (to be processed by the main thread)
RequestQueue<std::string, u32, u8, u8> m_get_texture_queue;
std::queue<std::pair<std::string, std::promise<u32>>> m_get_texture_queue;
std::mutex m_get_texture_queue_mutex;
};
enum MaterialType{

View File

@ -1127,127 +1127,6 @@ protected:
core::list<T> m_list;
};
/*
A single worker thread - multiple client threads queue framework.
*/
template<typename Caller, typename Data>
class CallerInfo
{
public:
Caller caller;
Data data;
};
template<typename Key, typename T, typename Caller, typename CallerData>
class GetResult
{
public:
Key key;
T item;
core::list<CallerInfo<Caller, CallerData> > callers;
};
template<typename Key, typename T, typename Caller, typename CallerData>
class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
{
};
template<typename Key, typename T, typename Caller, typename CallerData>
class GetRequest
{
public:
GetRequest()
{
dest = NULL;
}
GetRequest(ResultQueue<Key,T, Caller, CallerData> *a_dest)
{
dest = a_dest;
}
GetRequest(ResultQueue<Key,T, Caller, CallerData> *a_dest,
Key a_key)
{
dest = a_dest;
key = a_key;
}
~GetRequest()
{
}
Key key;
ResultQueue<Key, T, Caller, CallerData> *dest;
core::list<CallerInfo<Caller, CallerData> > callers;
};
template<typename Key, typename T, typename Caller, typename CallerData>
class RequestQueue
{
public:
u32 size()
{
return m_queue.size();
}
void add(Key key, Caller caller, CallerData callerdata,
ResultQueue<Key, T, Caller, CallerData> *dest)
{
JMutexAutoLock lock(m_queue.getMutex());
/*
If the caller is already on the list, only update CallerData
*/
for(typename core::list< GetRequest<Key, T, Caller, CallerData> >::Iterator
i = m_queue.getList().begin();
i != m_queue.getList().end(); i++)
{
GetRequest<Key, T, Caller, CallerData> &request = *i;
if(request.key == key)
{
for(typename core::list< CallerInfo<Caller, CallerData> >::Iterator
i = request.callers.begin();
i != request.callers.end(); i++)
{
CallerInfo<Caller, CallerData> &ca = *i;
if(ca.caller == caller)
{
ca.data = callerdata;
return;
}
}
CallerInfo<Caller, CallerData> ca;
ca.caller = caller;
ca.data = callerdata;
request.callers.push_back(ca);
return;
}
}
/*
Else add a new request to the queue
*/
GetRequest<Key, T, Caller, CallerData> request;
request.key = key;
CallerInfo<Caller, CallerData> ca;
ca.caller = caller;
ca.data = callerdata;
request.callers.push_back(ca);
request.dest = dest;
m_queue.getList().push_back(request);
}
GetRequest<Key, T, Caller, CallerData> pop(bool wait_if_empty=false)
{
return m_queue.pop_front(wait_if_empty);
}
private:
MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;
};
/*
Pseudo-random (VC++ rand() sucks)
*/