Fix multicaller support in RequestQueue
authorsapier <Sapier at GMX dot net>
Wed, 13 Nov 2013 20:46:14 +0000 (21:46 +0100)
committerkwolekr <kwolekr@minetest.net>
Sun, 17 Nov 2013 17:45:58 +0000 (12:45 -0500)
src/itemdef.cpp
src/shader.cpp
src/tile.cpp
src/util/thread.h

index 83a70f1d7c368fdc45cb0397732425a2687e1456..c520ea902736f4c355710d0755c7a2119f58e3be 100644 (file)
@@ -638,12 +638,9 @@ public:
                {
                        GetRequest<std::string, ClientCached*, u8, u8>
                                        request = m_get_clientcached_queue.pop();
-                       GetResult<std::string, ClientCached*, u8, u8>
-                                       result;
-                       result.key = request.key;
-                       result.callers = request.callers;
-                       result.item = createClientCachedDirect(request.key, gamedef);
-                       request.dest->push_back(result);
+
+                       m_get_clientcached_queue.pushResult(request,
+                                       createClientCachedDirect(request.key, gamedef));
                }
 #endif
        }
index ed8cc5b739f23d0726cce7b4fc400d02a176143e..122cbea19a207ad6f562becc7f956e7739af420f 100644 (file)
@@ -546,13 +546,7 @@ void ShaderSource::processQueue()
                                <<"name=\""<<request.key<<"\""
                                <<std::endl;*/
 
-               GetResult<std::string, u32, u8, u8>
-                               result;
-               result.key = request.key;
-               result.callers = request.callers;
-               result.item = getShaderIdDirect(request.key);
-
-               request.dest->push_back(result);
+               m_get_shader_queue.pushResult(request,getShaderIdDirect(request.key));
        }
 }
 
index 3c4989ea85a58bf43853de59056f0132644848ff..5dec79a10e4c1346251ab37b7c74e3d86e164768 100644 (file)
@@ -786,13 +786,7 @@ void TextureSource::processQueue()
                                <<"name=\""<<request.key<<"\""
                                <<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.pushResult(request,getTextureIdDirect(request.key));
        }
 }
 
index 678d9cf9b059db41314a70229f552d1754f247bb..70d41aeb982b4c1c21e6a536cf54b8c180ecda0a 100644 (file)
@@ -106,13 +106,7 @@ public:
        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
@@ -120,7 +114,7 @@ class GetResult
 public:
        Key key;
        T item;
-       std::list<CallerInfo<Caller, CallerData> > callers;
+       std::pair<Caller, CallerData> caller;
 };
 
 template<typename Key, typename T, typename Caller, typename CallerData>
@@ -128,22 +122,24 @@ class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
 {
 };
 
+template<typename Caller, typename Data, typename Key, typename T>
+class CallerInfo
+{
+public:
+       Caller caller;
+       Data data;
+       ResultQueue< Key, T, Caller, Data>* dest;
+};
+
 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)
+       GetRequest(Key a_key)
        {
-               dest = a_dest;
                key = a_key;
        }
        ~GetRequest()
@@ -151,8 +147,7 @@ public:
        }
        
        Key key;
-       ResultQueue<Key, T, Caller, CallerData> *dest;
-       std::list<CallerInfo<Caller, CallerData> > callers;
+       std::list<CallerInfo<Caller, CallerData, Key, T> > callers;
 };
 
 template<typename Key, typename T, typename Caller, typename CallerData>
@@ -180,20 +175,21 @@ public:
 
                        if(request.key == key)
                        {
-                               for(typename std::list< CallerInfo<Caller, CallerData> >::iterator
+                               for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
                                                i = request.callers.begin();
                                                i != request.callers.end(); ++i)
                                {
-                                       CallerInfo<Caller, CallerData> &ca = *i;
+                                       CallerInfo<Caller, CallerData, Key, T> &ca = *i;
                                        if(ca.caller == caller)
                                        {
                                                ca.data = callerdata;
                                                return;
                                        }
                                }
-                               CallerInfo<Caller, CallerData> ca;
+                               CallerInfo<Caller, CallerData, Key, T> ca;
                                ca.caller = caller;
                                ca.data = callerdata;
+                               ca.dest = dest;
                                request.callers.push_back(ca);
                                return;
                        }
@@ -205,11 +201,11 @@ public:
 
                GetRequest<Key, T, Caller, CallerData> request;
                request.key = key;
-               CallerInfo<Caller, CallerData> ca;
+               CallerInfo<Caller, CallerData, Key, T> ca;
                ca.caller = caller;
                ca.data = callerdata;
+               ca.dest = dest;
                request.callers.push_back(ca);
-               request.dest = dest;
                
                m_queue.getList().push_back(request);
        }
@@ -219,6 +215,26 @@ public:
                return m_queue.pop_front(wait_if_empty);
        }
 
+       void pushResult(GetRequest<Key, T, Caller, CallerData> req,
+                                       T res) {
+
+               for(typename std::list< CallerInfo<Caller, CallerData, Key, T> >::iterator
+                               i = req.callers.begin();
+                               i != req.callers.end(); ++i)
+               {
+                       CallerInfo<Caller, CallerData, Key, T> &ca = *i;
+
+                       GetResult<Key,T,Caller,CallerData> result;
+
+                       result.key = req.key;
+                       result.item = res;
+                       result.caller.first = ca.caller;
+                       result.caller.second = ca.data;
+
+                       ca.dest->push_back(result);
+               }
+       }
+
 private:
        MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;
 };