From 5aa233d4e7ec0f87b59572d44b4e90cda2bb6272 Mon Sep 17 00:00:00 2001 From: cracker64 Date: Sun, 10 Mar 2013 01:47:09 -0500 Subject: [PATCH] Use cancels instead of pthread_getattr_np for compatibility. Stopping the download would be nice, it still goes in the background. --- src/preview/PreviewModel.cpp | 103 +++++++++++++++++------------------ src/preview/PreviewModel.h | 3 + 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/src/preview/PreviewModel.cpp b/src/preview/PreviewModel.cpp index b25d08456..8d608cf2e 100644 --- a/src/preview/PreviewModel.cpp +++ b/src/preview/PreviewModel.cpp @@ -36,65 +36,63 @@ void * PreviewModel::updateSaveCommentsTHelper(void * obj) return ((PreviewModel*)obj)->updateSaveCommentsT(); } +void PreviewModel::updateSaveInfoTDelete(void * arg) +{ + delete arg; +} +void PreviewModel::updateSaveDataTDelete(void * arg) +{ + free(arg); +} +void PreviewModel::updateSaveCommentsTDelete(void * arg) +{ + for(int i = 0; i < ((std::vector *)arg)->size(); i++) + delete ((std::vector *)arg)->at(i); + ((std::vector *)arg)->clear(); + delete arg; +} + void * PreviewModel::updateSaveInfoT() { - int check; - pthread_attr_t attr; - SaveInfo * tempSave = Client::Ref().GetSave(tSaveID, tSaveDate); - pthread_getattr_np(pthread_self(), &attr); - pthread_attr_getdetachstate(&attr,&check); - pthread_attr_destroy(&attr); - if (check==PTHREAD_CREATE_JOINABLE) - { - updateSaveInfoFinished = true; - return tempSave; - } else - { - if (tempSave) delete tempSave; - } - return NULL; + SaveInfo * tempSave; + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); + tempSave = Client::Ref().GetSave(tSaveID, tSaveDate); + pthread_cleanup_push(&updateSaveInfoTDelete,tempSave); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); + pthread_testcancel(); + updateSaveInfoFinished = true; + pthread_cleanup_pop(0); + return tempSave; } void * PreviewModel::updateSaveDataT() { - int tempDataSize, check; - pthread_attr_t attr; - unsigned char * tempData = Client::Ref().GetSaveData(tSaveID, tSaveDate, tempDataSize); - pthread_getattr_np(pthread_self(), &attr); - pthread_attr_getdetachstate(&attr,&check); - pthread_attr_destroy(&attr); - if (check==PTHREAD_CREATE_JOINABLE) - { - saveDataBuffer.clear(); - if (tempData) - saveDataBuffer.insert(saveDataBuffer.begin(), tempData, tempData+tempDataSize); - updateSaveDataFinished = true; - } - if (tempData) free(tempData); - + int tempDataSize; + unsigned char * tempData; + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); + tempData = Client::Ref().GetSaveData(tSaveID, tSaveDate, tempDataSize); + pthread_cleanup_push(&updateSaveDataTDelete,tempData); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); + pthread_testcancel(); + saveDataBuffer.clear(); + if (tempData) + saveDataBuffer.insert(saveDataBuffer.begin(), tempData, tempData+tempDataSize); + updateSaveDataFinished = true; + pthread_cleanup_pop(1); return NULL; } void * PreviewModel::updateSaveCommentsT() { - int check; - pthread_attr_t attr; - std::vector * tempComments = Client::Ref().GetComments(tSaveID, (commentsPageNumber-1)*20, 20); - pthread_getattr_np(pthread_self(), &attr); - pthread_attr_getdetachstate(&attr,&check); - pthread_attr_destroy(&attr); - if (check==PTHREAD_CREATE_JOINABLE) - { - updateSaveCommentsFinished = true; - return tempComments; - } else - { - for(int i = 0; i < tempComments->size(); i++) - delete tempComments->at(i); - tempComments->clear(); - delete tempComments; - } - return NULL; + std::vector * tempComments; + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); + tempComments = Client::Ref().GetComments(tSaveID, (commentsPageNumber-1)*20, 20); + pthread_cleanup_push(&updateSaveCommentsTDelete,tempComments); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); + pthread_testcancel(); + updateSaveCommentsFinished = true; + pthread_cleanup_pop(0); + return tempComments; } void PreviewModel::SetFavourite(bool favourite) @@ -345,12 +343,9 @@ void PreviewModel::Update() } PreviewModel::~PreviewModel() { - //pthread_join(updateSaveDataThread, NULL); - //pthread_join(updateSaveInfoThread, NULL); - //pthread_join(updateSaveCommentsThread, NULL); - if (updateSaveDataWorking) pthread_detach(updateSaveDataThread); - if (updateSaveInfoWorking) pthread_detach(updateSaveInfoThread); - if (updateSaveCommentsWorking) pthread_detach(updateSaveCommentsThread); + pthread_cancel(updateSaveDataThread); + pthread_cancel(updateSaveInfoThread); + pthread_cancel(updateSaveCommentsThread); if(save) delete save; if(saveComments) diff --git a/src/preview/PreviewModel.h b/src/preview/PreviewModel.h index 2ee2c45d5..a8800e3ff 100644 --- a/src/preview/PreviewModel.h +++ b/src/preview/PreviewModel.h @@ -44,18 +44,21 @@ class PreviewModel { volatile bool updateSaveDataFinished; pthread_t updateSaveDataThread; static void * updateSaveDataTHelper(void * obj); + static void updateSaveDataTDelete(void * arg); void * updateSaveDataT(); bool updateSaveInfoWorking; volatile bool updateSaveInfoFinished; pthread_t updateSaveInfoThread; static void * updateSaveInfoTHelper(void * obj); + static void updateSaveInfoTDelete(void * arg); void * updateSaveInfoT(); bool updateSaveCommentsWorking; volatile bool updateSaveCommentsFinished; pthread_t updateSaveCommentsThread; static void * updateSaveCommentsTHelper(void * obj); + static void updateSaveCommentsTDelete(void * arg); void * updateSaveCommentsT(); public: PreviewModel();