adding more thread-safety

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13267 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-07-18 14:20:09 +00:00
parent 8edf1b6f9e
commit ea1c6947ab
2 changed files with 25 additions and 1 deletions

View File

@ -21,6 +21,7 @@ KartUpdateProtocol::KartUpdateProtocol()
m_self_kart_index = i; m_self_kart_index = i;
} }
} }
pthread_mutex_init(&m_positions_updates_mutex, NULL);
} }
KartUpdateProtocol::~KartUpdateProtocol() KartUpdateProtocol::~KartUpdateProtocol()
@ -47,7 +48,10 @@ void KartUpdateProtocol::notifyEvent(Event* event)
a = ns.getFloat(4); a = ns.getFloat(4);
b = ns.getFloat(8); b = ns.getFloat(8);
c = ns.getFloat(12); c = ns.getFloat(12);
m_karts[kart_id]->setXYZ(Vec3(a,b,c)); pthread_mutex_trylock(&m_positions_updates_mutex);
m_next_positions.push_back(Vec3(a,b,c));
m_karts_ids.push_back(kart_id);
pthread_mutex_unlock(&m_positions_updates_mutex);
Log::info("KartUpdateProtocol", "Updating kart %i pos to %f %f %f", kart_id, a,b,c); Log::info("KartUpdateProtocol", "Updating kart %i pos to %f %f %f", kart_id, a,b,c);
ns.removeFront(16); ns.removeFront(16);
@ -90,6 +94,20 @@ void KartUpdateProtocol::update()
m_listener->sendMessage(this, ns, false); m_listener->sendMessage(this, ns, false);
} }
} }
switch(pthread_mutex_trylock(&m_positions_updates_mutex))
{
case 0: /* if we got the lock, unlock and return 1 (true) */
while (!m_next_positions.empty())
{
m_karts[m_karts_ids.back()]->setXYZ(m_next_positions.back());
m_next_positions.pop_back();
m_karts_ids.pop_back();
}
pthread_mutex_unlock(&m_positions_updates_mutex);
break;
default:
break;
}
} }

View File

@ -2,6 +2,8 @@
#define KART_UPDATE_PROTOCOL_HPP #define KART_UPDATE_PROTOCOL_HPP
#include "network/protocol.hpp" #include "network/protocol.hpp"
#include "utils/vec3.hpp"
#include <list>
class AbstractKart; class AbstractKart;
@ -20,6 +22,10 @@ class KartUpdateProtocol : public Protocol
std::vector<AbstractKart*> m_karts; std::vector<AbstractKart*> m_karts;
uint32_t m_self_kart_index; uint32_t m_self_kart_index;
std::list<Vec3> m_next_positions;
std::list<uint32_t> m_karts_ids;
pthread_mutex_t m_positions_updates_mutex;
}; };
#endif // KART_UPDATE_PROTOCOL_HPP #endif // KART_UPDATE_PROTOCOL_HPP