Use cancels instead of pthread_getattr_np for compatibility.

Stopping the download would be nice, it still goes in the background.
This commit is contained in:
cracker64
2013-03-10 01:47:09 -05:00
parent 3abc33fd3b
commit 5aa233d4e7
2 changed files with 52 additions and 54 deletions

View File

@@ -36,65 +36,63 @@ void * PreviewModel::updateSaveCommentsTHelper(void * obj)
return ((PreviewModel*)obj)->updateSaveCommentsT(); 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<SaveComment*> *)arg)->size(); i++)
delete ((std::vector<SaveComment*> *)arg)->at(i);
((std::vector<SaveComment*> *)arg)->clear();
delete arg;
}
void * PreviewModel::updateSaveInfoT() void * PreviewModel::updateSaveInfoT()
{ {
int check; SaveInfo * tempSave;
pthread_attr_t attr; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
SaveInfo * tempSave = Client::Ref().GetSave(tSaveID, tSaveDate); tempSave = Client::Ref().GetSave(tSaveID, tSaveDate);
pthread_getattr_np(pthread_self(), &attr); pthread_cleanup_push(&updateSaveInfoTDelete,tempSave);
pthread_attr_getdetachstate(&attr,&check); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_attr_destroy(&attr); pthread_testcancel();
if (check==PTHREAD_CREATE_JOINABLE) updateSaveInfoFinished = true;
{ pthread_cleanup_pop(0);
updateSaveInfoFinished = true; return tempSave;
return tempSave;
} else
{
if (tempSave) delete tempSave;
}
return NULL;
} }
void * PreviewModel::updateSaveDataT() void * PreviewModel::updateSaveDataT()
{ {
int tempDataSize, check; int tempDataSize;
pthread_attr_t attr; unsigned char * tempData;
unsigned char * tempData = Client::Ref().GetSaveData(tSaveID, tSaveDate, tempDataSize); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
pthread_getattr_np(pthread_self(), &attr); tempData = Client::Ref().GetSaveData(tSaveID, tSaveDate, tempDataSize);
pthread_attr_getdetachstate(&attr,&check); pthread_cleanup_push(&updateSaveDataTDelete,tempData);
pthread_attr_destroy(&attr); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
if (check==PTHREAD_CREATE_JOINABLE) pthread_testcancel();
{ saveDataBuffer.clear();
saveDataBuffer.clear(); if (tempData)
if (tempData) saveDataBuffer.insert(saveDataBuffer.begin(), tempData, tempData+tempDataSize);
saveDataBuffer.insert(saveDataBuffer.begin(), tempData, tempData+tempDataSize); updateSaveDataFinished = true;
updateSaveDataFinished = true; pthread_cleanup_pop(1);
}
if (tempData) free(tempData);
return NULL; return NULL;
} }
void * PreviewModel::updateSaveCommentsT() void * PreviewModel::updateSaveCommentsT()
{ {
int check; std::vector<SaveComment*> * tempComments;
pthread_attr_t attr; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
std::vector<SaveComment*> * tempComments = Client::Ref().GetComments(tSaveID, (commentsPageNumber-1)*20, 20); tempComments = Client::Ref().GetComments(tSaveID, (commentsPageNumber-1)*20, 20);
pthread_getattr_np(pthread_self(), &attr); pthread_cleanup_push(&updateSaveCommentsTDelete,tempComments);
pthread_attr_getdetachstate(&attr,&check); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_attr_destroy(&attr); pthread_testcancel();
if (check==PTHREAD_CREATE_JOINABLE) updateSaveCommentsFinished = true;
{ pthread_cleanup_pop(0);
updateSaveCommentsFinished = true; return tempComments;
return tempComments;
} else
{
for(int i = 0; i < tempComments->size(); i++)
delete tempComments->at(i);
tempComments->clear();
delete tempComments;
}
return NULL;
} }
void PreviewModel::SetFavourite(bool favourite) void PreviewModel::SetFavourite(bool favourite)
@@ -345,12 +343,9 @@ void PreviewModel::Update()
} }
PreviewModel::~PreviewModel() { PreviewModel::~PreviewModel() {
//pthread_join(updateSaveDataThread, NULL); pthread_cancel(updateSaveDataThread);
//pthread_join(updateSaveInfoThread, NULL); pthread_cancel(updateSaveInfoThread);
//pthread_join(updateSaveCommentsThread, NULL); pthread_cancel(updateSaveCommentsThread);
if (updateSaveDataWorking) pthread_detach(updateSaveDataThread);
if (updateSaveInfoWorking) pthread_detach(updateSaveInfoThread);
if (updateSaveCommentsWorking) pthread_detach(updateSaveCommentsThread);
if(save) if(save)
delete save; delete save;
if(saveComments) if(saveComments)

View File

@@ -44,18 +44,21 @@ class PreviewModel {
volatile bool updateSaveDataFinished; volatile bool updateSaveDataFinished;
pthread_t updateSaveDataThread; pthread_t updateSaveDataThread;
static void * updateSaveDataTHelper(void * obj); static void * updateSaveDataTHelper(void * obj);
static void updateSaveDataTDelete(void * arg);
void * updateSaveDataT(); void * updateSaveDataT();
bool updateSaveInfoWorking; bool updateSaveInfoWorking;
volatile bool updateSaveInfoFinished; volatile bool updateSaveInfoFinished;
pthread_t updateSaveInfoThread; pthread_t updateSaveInfoThread;
static void * updateSaveInfoTHelper(void * obj); static void * updateSaveInfoTHelper(void * obj);
static void updateSaveInfoTDelete(void * arg);
void * updateSaveInfoT(); void * updateSaveInfoT();
bool updateSaveCommentsWorking; bool updateSaveCommentsWorking;
volatile bool updateSaveCommentsFinished; volatile bool updateSaveCommentsFinished;
pthread_t updateSaveCommentsThread; pthread_t updateSaveCommentsThread;
static void * updateSaveCommentsTHelper(void * obj); static void * updateSaveCommentsTHelper(void * obj);
static void updateSaveCommentsTDelete(void * arg);
void * updateSaveCommentsT(); void * updateSaveCommentsT();
public: public:
PreviewModel(); PreviewModel();