Revert "Backported some thread classes cleanup from dev-threads (1ad8ff6e47fb9e249e6bcbc2f4a3a94933a1168a)"

This reverts commit 7cf73c94c4.

I wanted to make this better but failed so badly ;) The memory holding the thread function pointer may be freed before the thread is started, so the whole resource passing thing is needed anyway.

This probably fixes #584
master
krzys-h 2015-08-19 20:25:17 +02:00
parent 27bbc27948
commit d723295aad
4 changed files with 40 additions and 137 deletions

View File

@ -1,27 +1,7 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#pragma once
#include "common/thread/sdl_cond_wrapper.h"
#include "common/thread/sdl_mutex_wrapper.h"
#include "common/thread/sdl_thread_wrapper.h"
#include <SDL_thread.h>
@ -36,11 +16,11 @@
* in new thread and a unique_ptr to resource which is to be passed to the new thread.
*
* This is how it works:
* - in main thread: create a new thread passing the resource to it,
* - in main thread: create a new thread passing to it a special temporary context,
* - in main thread: wait for synchronization signal that the ownership was passed,
* - in new thread: acquire the resource
* - in new thread: acquire the resource from the context
* - in new thread: signal back to main thread that the resource was acquired,
* - in main thread: clean up and exit
* - in main thread: clean up temporary context and exit
* - in new thread: run the specified function with the acquired resource.
*
* It's a bit complicated, but that's the safe (thread-safe and exception-safe)
@ -64,20 +44,16 @@ public:
CSDLCondWrapper cond;
bool condition = false;
ThreadData data;
data.resource = std::move(m_resource);
data.threadFunction = m_threadFunction;
data.mutex = &mutex;
data.cond = &cond;
data.condition = &condition;
SDL_LockMutex(*mutex);
CSDLThreadWrapper thread([&]() {
SDL_LockMutex(*mutex);
ResourceUPtr resource = std::move(m_resource);
condition = true;
SDL_CondSignal(*cond);
SDL_UnlockMutex(*mutex);
m_threadFunction(std::move(resource));
});
thread.Start();
SDL_CreateThread(Run, reinterpret_cast<void*>(&data));
while (!condition)
{
@ -88,6 +64,35 @@ public:
}
private:
static int Run(void* data)
{
ThreadFunctionPtr threadFunction = nullptr;
ResourceUPtr resource;
ThreadData* threadData = reinterpret_cast<ThreadData*>(data);
SDL_LockMutex(**threadData->mutex);
threadFunction = threadData->threadFunction;
resource = std::move(threadData->resource);
*threadData->condition = true;
SDL_CondSignal(**threadData->cond);
SDL_UnlockMutex(**threadData->mutex);
threadFunction(std::move(resource));
return 0;
}
private:
struct ThreadData
{
ResourceUPtr resource;
CSDLMutexWrapper* mutex = nullptr;
CSDLCondWrapper* cond = nullptr;
bool* condition = nullptr;
ThreadFunctionPtr threadFunction = nullptr;
};
ThreadFunctionPtr m_threadFunction;
ResourceUPtr m_resource;
};

View File

@ -1,22 +1,3 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#pragma once
#include <SDL_thread.h>

View File

@ -1,22 +1,3 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#pragma once
#include <SDL_thread.h>

View File

@ -1,64 +0,0 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#pragma once
#include <SDL_thread.h>
#include <functional>
/**
* \class CSDLThreadWrapper
* \brief Wrapper around SDL_thread allowing passing of capture lambdas as thread function
*/
class CSDLThreadWrapper
{
public:
using ThreadFunctionPtr = std::function<void()>;
CSDLThreadWrapper(ThreadFunctionPtr threadFunction)
: m_threadFunction(threadFunction),
m_thread(nullptr)
{}
~CSDLThreadWrapper()
{}
void Start()
{
m_thread = SDL_CreateThread(&StartThreadWithThreadFunction, &m_threadFunction);
}
SDL_Thread* operator*()
{
return m_thread;
}
private:
static int StartThreadWithThreadFunction(void* data)
{
ThreadFunctionPtr func = *(static_cast<ThreadFunctionPtr*>(data));
func();
return 0;
}
private:
ThreadFunctionPtr m_threadFunction;
SDL_Thread* m_thread;
};