ClientCached* getClientCached(const std::string &name, Client *client) const { ClientCached *cc = NULL; m_clientcached.get(name, &cc); if (cc) return cc; if (std::this_thread::get_id() == m_main_thread) { return createClientCachedDirect(name, client); } // We're gonna ask the result to be put into here static ResultQueue<std::string, ClientCached*, u8, u8> result_queue; // Throw a request in m_get_clientcached_queue.add(name, 0, 0, &result_queue); try { while(true) { // Wait result for a second GetResult<std::string, ClientCached*, u8, u8> result = result_queue.pop_front(1000); if (result.key == name) { return result.item; } } } catch(ItemNotFoundException &e) { errorstream << "Waiting for clientcached " << name << " timed out." << std::endl; return &m_dummy_clientcached; } }
ClientCached* getClientCached(const std::string &name, IGameDef *gamedef) const { ClientCached *cc = NULL; m_clientcached.get(name, &cc); if(cc) return cc; if(get_current_thread_id() == m_main_thread) { return createClientCachedDirect(name, gamedef); } else { // We're gonna ask the result to be put into here ResultQueue<std::string, ClientCached*, u8, u8> result_queue; // Throw a request in m_get_clientcached_queue.add(name, 0, 0, &result_queue); try { // Wait result for a second GetResult<std::string, ClientCached*, u8, u8> result = result_queue.pop_front(1000); // Check that at least something worked OK assert(result.key == name); // Return it return result.item; } catch(ItemNotFoundException &e) { errorstream<<"Waiting for clientcached timed out."<<std::endl; return &m_dummy_clientcached; } } }
void processQueue(IGameDef *gamedef) { #ifndef SERVER //NOTE this is only thread safe for ONE consumer thread! while(!m_get_clientcached_queue.empty()) { GetRequest<std::string, ClientCached*, u8, u8> request = m_get_clientcached_queue.pop(); m_get_clientcached_queue.pushResult(request, createClientCachedDirect(request.key, (Client *)gamedef)); } #endif }
void processQueue(IGameDef *gamedef) { #ifndef SERVER while(!m_get_clientcached_queue.empty()) { 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); } #endif }