Fix issue where thumbnails are sometimes the incorrect ones

This commit is contained in:
Simon Robertshaw 2012-10-01 00:15:09 +01:00
parent 89e9623725
commit 163fbde8af
3 changed files with 24 additions and 21 deletions

View File

@ -37,7 +37,7 @@ void ThumbnailBroker::RenderThumbnail(GameSave * gameSave, bool decorations, int
pthread_mutex_lock(&thumbnailQueueMutex); pthread_mutex_lock(&thumbnailQueueMutex);
bool running = thumbnailQueueRunning; bool running = thumbnailQueueRunning;
thumbnailQueueRunning = true; thumbnailQueueRunning = true;
renderRequests.push_back(ThumbRenderRequest(new GameSave(*gameSave), decorations, width, height, tListener)); renderRequests.push_back(ThumbRenderRequest(new GameSave(*gameSave), decorations, width, height, ListenerHandle(tListener->ListenerRand, tListener)));
pthread_mutex_unlock(&thumbnailQueueMutex); pthread_mutex_unlock(&thumbnailQueueMutex);
if(!running) if(!running)
@ -55,7 +55,7 @@ void ThumbnailBroker::RetrieveThumbnail(int saveID, int saveDate, int width, int
pthread_mutex_lock(&thumbnailQueueMutex); pthread_mutex_lock(&thumbnailQueueMutex);
bool running = thumbnailQueueRunning; bool running = thumbnailQueueRunning;
thumbnailQueueRunning = true; thumbnailQueueRunning = true;
thumbnailRequests.push_back(ThumbnailRequest(saveID, saveDate, width, height, tListener)); thumbnailRequests.push_back(ThumbnailRequest(saveID, saveDate, width, height, ListenerHandle(tListener->ListenerRand, tListener)));
pthread_mutex_unlock(&thumbnailQueueMutex); pthread_mutex_unlock(&thumbnailQueueMutex);
if(!running) if(!running)
@ -80,7 +80,7 @@ void ThumbnailBroker::FlushThumbQueue()
{ {
if(CheckThumbnailListener(thumbnailComplete.front().first)) if(CheckThumbnailListener(thumbnailComplete.front().first))
{ {
thumbnailComplete.front().first->OnThumbnailReady(thumbnailComplete.front().second); thumbnailComplete.front().first.second->OnThumbnailReady(thumbnailComplete.front().second);
} }
else else
{ {
@ -130,7 +130,7 @@ void ThumbnailBroker::thumbnailQueueProcessTH()
thumbnail->Resize(req.Width, req.Height); thumbnail->Resize(req.Width, req.Height);
pthread_mutex_lock(&thumbnailQueueMutex); pthread_mutex_lock(&thumbnailQueueMutex);
thumbnailComplete.push_back(std::pair<ThumbnailListener*, Thumbnail*>(req.CompletedListener, thumbnail)); thumbnailComplete.push_back(std::pair<ListenerHandle, Thumbnail*>(req.CompletedListener, thumbnail));
pthread_mutex_unlock(&thumbnailQueueMutex); pthread_mutex_unlock(&thumbnailQueueMutex);
} }
} }
@ -173,7 +173,7 @@ void ThumbnailBroker::thumbnailQueueProcessTH()
tempThumbnail->Resize((*specIter).Width, (*specIter).Height); tempThumbnail->Resize((*specIter).Width, (*specIter).Height);
pthread_mutex_lock(&thumbnailQueueMutex); pthread_mutex_lock(&thumbnailQueueMutex);
thumbnailComplete.push_back(std::pair<ThumbnailListener*, Thumbnail*>((*specIter).CompletedListener, tempThumbnail)); thumbnailComplete.push_back(std::pair<ListenerHandle, Thumbnail*>((*specIter).CompletedListener, tempThumbnail));
pthread_mutex_unlock(&thumbnailQueueMutex); pthread_mutex_unlock(&thumbnailQueueMutex);
} }
} }
@ -286,7 +286,7 @@ void ThumbnailBroker::thumbnailQueueProcessTH()
tempThumbnail->Resize((*specIter).Width, (*specIter).Height); tempThumbnail->Resize((*specIter).Width, (*specIter).Height);
pthread_mutex_lock(&thumbnailQueueMutex); pthread_mutex_lock(&thumbnailQueueMutex);
thumbnailComplete.push_back(std::pair<ThumbnailListener*, Thumbnail*>((*specIter).CompletedListener, tempThumbnail)); thumbnailComplete.push_back(std::pair<ListenerHandle, Thumbnail*>((*specIter).CompletedListener, tempThumbnail));
pthread_mutex_unlock(&thumbnailQueueMutex); pthread_mutex_unlock(&thumbnailQueueMutex);
} }
} }
@ -314,10 +314,10 @@ void ThumbnailBroker::RetrieveThumbnail(int saveID, int width, int height, Thumb
RetrieveThumbnail(saveID, 0, width, height, tListener); RetrieveThumbnail(saveID, 0, width, height, tListener);
} }
bool ThumbnailBroker::CheckThumbnailListener(ThumbnailListener * tListener) bool ThumbnailBroker::CheckThumbnailListener(ListenerHandle handle)
{ {
pthread_mutex_lock(&listenersMutex); pthread_mutex_lock(&listenersMutex);
int count = std::count(validListeners.begin(), validListeners.end(), tListener); int count = std::count(validListeners.begin(), validListeners.end(), handle);
pthread_mutex_unlock(&listenersMutex); pthread_mutex_unlock(&listenersMutex);
return count; return count;
@ -326,7 +326,7 @@ bool ThumbnailBroker::CheckThumbnailListener(ThumbnailListener * tListener)
void ThumbnailBroker::AttachThumbnailListener(ThumbnailListener * tListener) void ThumbnailBroker::AttachThumbnailListener(ThumbnailListener * tListener)
{ {
pthread_mutex_lock(&listenersMutex); pthread_mutex_lock(&listenersMutex);
validListeners.push_back(tListener); validListeners.push_back(ListenerHandle(tListener->ListenerRand, tListener));
pthread_mutex_unlock(&listenersMutex); pthread_mutex_unlock(&listenersMutex);
} }
@ -334,10 +334,10 @@ void ThumbnailBroker::DetachThumbnailListener(ThumbnailListener * tListener)
{ {
pthread_mutex_lock(&listenersMutex); pthread_mutex_lock(&listenersMutex);
std::vector<ThumbnailListener*>::iterator iter = validListeners.begin(); std::vector<ListenerHandle>::iterator iter = validListeners.begin();
while (iter != validListeners.end()) while (iter != validListeners.end())
{ {
if(*iter == tListener) if(*iter == ListenerHandle(tListener->ListenerRand, tListener))
iter = validListeners.erase(iter); iter = validListeners.erase(iter);
else else
++iter; ++iter;

View File

@ -11,6 +11,7 @@
class GameSave; class GameSave;
class Thumbnail; class Thumbnail;
class ThumbnailListener; class ThumbnailListener;
typedef std::pair<int, ThumbnailListener*> ListenerHandle;
class ThumbnailBroker: public Singleton<ThumbnailBroker> class ThumbnailBroker: public Singleton<ThumbnailBroker>
{ {
private: private:
@ -18,8 +19,8 @@ private:
{ {
public: public:
int Width, Height; int Width, Height;
ThumbnailListener * CompletedListener; ListenerHandle CompletedListener;
ThumbnailSpec(int width, int height, ThumbnailListener * completedListener) : ThumbnailSpec(int width, int height, ListenerHandle completedListener) :
Width(width), Height(height), CompletedListener(completedListener) {} Width(width), Height(height), CompletedListener(completedListener) {}
}; };
@ -45,7 +46,7 @@ private:
ThumbnailID ID; ThumbnailID ID;
std::vector<ThumbnailSpec> SubRequests; std::vector<ThumbnailSpec> SubRequests;
ThumbnailRequest(int saveID, int saveDate, int width, int height, ThumbnailListener * completedListener) : ThumbnailRequest(int saveID, int saveDate, int width, int height, ListenerHandle completedListener) :
ID(saveID, saveDate), Complete(false), HTTPContext(NULL), RequestTime(0) ID(saveID, saveDate), Complete(false), HTTPContext(NULL), RequestTime(0)
{ {
SubRequests.push_back(ThumbnailSpec(width, height, completedListener)); SubRequests.push_back(ThumbnailSpec(width, height, completedListener));
@ -59,10 +60,10 @@ private:
int Width, Height; int Width, Height;
bool Decorations; bool Decorations;
GameSave * Save; GameSave * Save;
ThumbnailListener * CompletedListener; ListenerHandle CompletedListener;
ThumbRenderRequest(GameSave * save, bool decorations, int width, int height, ThumbnailListener * completedListener) : ThumbRenderRequest(GameSave * save, bool decorations, int width, int height, ListenerHandle completedListener) :
Save(save), Width(width), Height(height), CompletedListener(completedListener), Decorations(decorations) {} Save(save), Width(width), Height(height), CompletedListener(completedListener), Decorations(decorations) {}
ThumbRenderRequest() : Save(0), Decorations(true), Width(0), Height(0), CompletedListener(NULL) {} ThumbRenderRequest() : Save(0), Decorations(true), Width(0), Height(0), CompletedListener(ListenerHandle(0, NULL)) {}
}; };
//Thumbnail retreival //Thumbnail retreival
@ -80,11 +81,12 @@ private:
std::deque<ThumbnailRequest> thumbnailRequests; std::deque<ThumbnailRequest> thumbnailRequests;
std::deque<ThumbRenderRequest> renderRequests; std::deque<ThumbRenderRequest> renderRequests;
std::deque<std::pair<ThumbnailListener*, Thumbnail*> > thumbnailComplete; std::deque<std::pair<ListenerHandle, Thumbnail*> > thumbnailComplete;
std::list<ThumbnailRequest> currentRequests; std::list<ThumbnailRequest> currentRequests;
std::deque<std::pair<ThumbnailID, Thumbnail*> > thumbnailCache; std::deque<std::pair<ThumbnailID, Thumbnail*> > thumbnailCache;
std::vector<ThumbnailListener*> validListeners;
std::vector<ListenerHandle> validListeners;
static void * thumbnailQueueProcessHelper(void * ref); static void * thumbnailQueueProcessHelper(void * ref);
void thumbnailQueueProcessTH(); void thumbnailQueueProcessTH();
@ -99,7 +101,7 @@ public:
void RetrieveThumbnail(int saveID, int saveDate, int width, int height, ThumbnailListener * tListener); void RetrieveThumbnail(int saveID, int saveDate, int width, int height, ThumbnailListener * tListener);
void RetrieveThumbnail(int saveID, int width, int height, ThumbnailListener * tListener); void RetrieveThumbnail(int saveID, int width, int height, ThumbnailListener * tListener);
bool CheckThumbnailListener(ThumbnailListener * tListener); bool CheckThumbnailListener(ListenerHandle handle);
void AttachThumbnailListener(ThumbnailListener * tListener); void AttachThumbnailListener(ThumbnailListener * tListener);
void DetachThumbnailListener(ThumbnailListener * tListener); void DetachThumbnailListener(ThumbnailListener * tListener);
}; };

View File

@ -4,7 +4,8 @@ class Thumbnail;
class ThumbnailListener class ThumbnailListener
{ {
public: public:
ThumbnailListener() {} int ListenerRand;
ThumbnailListener() { ListenerRand = rand(); }
virtual ~ThumbnailListener() {} virtual ~ThumbnailListener() {}
virtual void OnThumbnailReady(Thumbnail * thumb) {} virtual void OnThumbnailReady(Thumbnail * thumb) {}