Changeset - r9477:56f022446026
[Not reviewed]
master
0 9 0
truebrain - 16 years ago 2008-06-08 12:06:27
truebrain@openttd.org
(svn r13412) -Add: OTTDThreadTerminateFunc, for all thread systems, which is called when a thread is terminated. Now GenWorld- and SaveLoad-thread cleanup theirselves correctly, while Fibers don't (as that causes access-violations)
9 files changed with 44 insertions and 27 deletions:
0 comments (0 inline, 0 general)
src/fiber_thread.cpp
Show inline comments
 
@@ -29,13 +29,13 @@ public:
 
		m_param(param),
 
		m_attached(false),
 
		m_kill(false)
 
	{
 
		this->m_sem = ThreadSemaphore::New();
 
		/* Create a thread and start stFiberProc */
 
		this->m_thread = ThreadObject::New(&stFiberProc, this);
 
		this->m_thread = ThreadObject::New(&stFiberProc, this, NULL);
 
	}
 

	
 
	/**
 
	 * Create a ThreadObject fiber and attach current thread to it.
 
	 */
 
	Fiber_Thread(void *param) :
src/genworld.cpp
Show inline comments
 
@@ -285,13 +285,13 @@ void GenerateWorld(GenerateWorldMode mod
 
	ResetWindowSystem();
 

	
 
	/* Create toolbars */
 
	SetupColorsAndInitialWindow();
 

	
 
	if (_network_dedicated ||
 
	    (_gw.thread = ThreadObject::New(&_GenerateWorld, NULL)) == NULL) {
 
	    (_gw.thread = ThreadObject::New(&_GenerateWorld, NULL, &ThreadObject::TerminateCleanup)) == NULL) {
 
		DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
 
		_gw.threaded = false;
 
		_GenerateWorld(NULL);
 
		return;
 
	}
 

	
src/saveload.cpp
Show inline comments
 
@@ -1692,13 +1692,13 @@ SaveOrLoadResult SaveOrLoad(const char *
 
			SaveViewportBeforeSaveGame();
 
			SlSaveChunks();
 
			SlWriteFill(); // flush the save buffer
 

	
 
			SaveFileStart();
 
			if (_network_server ||
 
						(_save_thread = ThreadObject::New(&SaveFileToDiskThread, NULL)) == NULL) {
 
						(_save_thread = ThreadObject::New(&SaveFileToDiskThread, NULL, &ThreadObject::TerminateCleanup)) == NULL) {
 
				if (!_network_server) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
 

	
 
				SaveOrLoadResult result = SaveFileToDisk(false);
 
				SaveFileDone();
 

	
 
				return result;
src/thread.h
Show inline comments
 
@@ -3,12 +3,13 @@
 
/** @file thread.h Base of all threads. */
 

	
 
#ifndef THREAD_H
 
#define THREAD_H
 

	
 
typedef void (*OTTDThreadFunc)(void *);
 
typedef void (*OTTDThreadTerminateFunc)(class ThreadObject *self);
 

	
 
/**
 
 * A Thread Object which works on all our supported OSes.
 
 */
 
class ThreadObject {
 
public:
 
@@ -53,27 +54,34 @@ public:
 

	
 
	/**
 
	 * Create a thread; proc will be called as first function inside the thread,
 
	 *  with optinal params.
 
	 * @param proc The procedure to call inside the thread.
 
	 * @param param The params to give with 'proc'.
 
	 * @param terminate_func The function (or NULL) to call when the thread terminates.
 
	 * @return True if the thread was started correctly.
 
	 */
 
	static ThreadObject *New(OTTDThreadFunc proc, void *param);
 
	static ThreadObject *New(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func);
 

	
 
	/**
 
	 * Convert the current thread to a new ThreadObject.
 
	 * @return A new ThreadObject with the current thread attached to it.
 
	 */
 
	static ThreadObject *AttachCurrent();
 

	
 
	/**
 
	 * Find the Id of the current running thread.
 
	 * @return The thread ID of the current active thread.
 
	 */
 
	static uint CurrentId();
 

	
 
	/**
 
	 * A OTTDThreadTerminateFunc, which cleans up the thread itself
 
	 *  at termination of the thread (so it becomes self-managed).
 
	 */
 
	static void TerminateCleanup(ThreadObject *self) { delete self; }
 
};
 

	
 
/**
 
 * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
 
 */
 
class ThreadSemaphore {
src/thread_morphos.cpp
Show inline comments
 
@@ -54,20 +54,23 @@ void KPutStr(CONST_STRPTR format)
 
/**
 
 * MorphOS version for ThreadObject.
 
 */
 
class ThreadObject_MorphOS : public ThreadObject {
 
private:
 
	APTR m_thr;                  ///< System thread identifier.
 
	OTTDThreadTerminateFunc m_terminate_func; ///< Function to call on thread termination.
 
	struct MsgPort *m_replyport;
 
	struct OTTDThreadStartupMessage m_msg;
 

	
 
public:
 
	/**
 
	 * Create a sub process and start it, calling proc(param).
 
	 */
 
	ThreadObject_MorphOS(OTTDThreadFunc proc, void *param) : m_thr(0)
 
	ThreadObject_MorphOS(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func) :
 
		m_thr(0),
 
		m_terminate_func(terminate_func)
 
	{
 
		struct Task *parent;
 

	
 
		KPutStr("[OpenTTD] Create thread...\n");
 

	
 
		parent = FindTask(NULL);
 
@@ -108,13 +111,15 @@ public:
 
		}
 
	}
 

	
 
	/**
 
	 * Create a thread and attach current thread to it.
 
	 */
 
	ThreadObject_MorphOS() : m_thr(0)
 
	ThreadObject_MorphOS() :
 
		m_thr(0),
 
		m_terminate_func(NULL)
 
	{
 
		m_thr = FindTask(NULL);
 
	}
 

	
 
	/* virtual */ ~ThreadObject_MorphOS()
 
	{
 
@@ -207,18 +212,20 @@ private:
 
				KPutStr("[Child] Returned to main()\n");
 
			}
 
		}
 

	
 
		/*  Quit the child, exec.library will reply the startup msg internally. */
 
		KPutStr("[Child] Done.\n");
 

	
 
		if (this->terminate_func != NULL) this->terminate_func(this);
 
	}
 
};
 

	
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func)
 
{
 
	return new ThreadObject_MorphOS(proc, param);
 
	return new ThreadObject_MorphOS(proc, param, terminate_func);
 
}
 

	
 
/* static */ ThreadObject *ThreadObject::AttachCurrent()
 
{
 
	return new ThreadObject_MorphOS();
 
}
src/thread_none.cpp
Show inline comments
 
@@ -3,13 +3,13 @@
 
/** @file thread_none.cpp No-Threads-Available implementation of Threads */
 

	
 
#include "stdafx.h"
 
#include "thread.h"
 
#include "fiber.hpp"
 

	
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func)
 
{
 
	return NULL;
 
}
 

	
 
/* static */ ThreadObject *ThreadObject::AttachCurrent()
 
{
src/thread_os2.cpp
Show inline comments
 
@@ -56,13 +56,13 @@ void OTTDExitThread()
 
{
 
	_endthread();
 
}
 

	
 
#endif
 

	
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func)
 
{
 
	return NULL;
 
}
 

	
 
/* static */ ThreadObject *ThreadObject::AttachCurrent()
 
{
src/thread_pthread.cpp
Show inline comments
 
@@ -19,22 +19,24 @@ private:
 
	pthread_t m_thr;             ///< System thread identifier.
 
	OTTDThreadFunc m_proc;       ///< External thread procedure.
 
	void     *m_param;           ///< Parameter for the external thread procedure.
 
	bool      m_attached;        ///< True if the ThreadObject was attached to an existing thread.
 
	sem_t     m_sem_start;       ///< Here the new thread waits before it starts.
 
	sem_t     m_sem_stop;        ///< Here the other thread can wait for this thread to end.
 
	OTTDThreadTerminateFunc m_terminate_func; ///< Function to call on thread termination.
 

	
 
public:
 
	/**
 
	 * Create a pthread and start it, calling proc(param).
 
	 */
 
	ThreadObject_pthread(OTTDThreadFunc proc, void *param) :
 
	ThreadObject_pthread(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func) :
 
		m_thr(0),
 
		m_proc(proc),
 
		m_param(param),
 
		m_attached(false)
 
		m_attached(false),
 
		m_terminate_func(terminate_func)
 
	{
 
		sem_init(&m_sem_start, 0, 0);
 
		sem_init(&m_sem_stop, 0, 0);
 

	
 
		pthread_create(&m_thr, NULL, &stThreadProc, this);
 
		sem_post(&m_sem_start);
 
@@ -44,13 +46,14 @@ public:
 
	 * Create a pthread and attach current thread to it.
 
	 */
 
	ThreadObject_pthread() :
 
		m_thr(0),
 
		m_proc(NULL),
 
		m_param(0),
 
		m_attached(true)
 
		m_attached(true),
 
		m_terminate_func(NULL)
 
	{
 
		sem_init(&m_sem_start, 0, 0);
 
		sem_init(&m_sem_stop, 0, 0);
 

	
 
		m_thr = pthread_self();
 
	}
 
@@ -99,14 +102,12 @@ public:
 
	{
 
		/* You cannot join yourself */
 
		assert(!IsCurrent());
 

	
 
		pthread_join(m_thr, NULL);
 
		m_thr = 0;
 

	
 
		delete this;
 
	}
 

	
 
	/* virtual */ bool IsCurrent()
 
	{
 
		return pthread_self() == m_thr;
 
	}
 
@@ -133,32 +134,28 @@ private:
 
	 */
 
	void ThreadProc()
 
	{
 
		/* The new thread stops here so the calling thread can complete pthread_create() call */
 
		sem_wait(&m_sem_start);
 

	
 
		/* Did this thread die naturally/via exit, or did it join? */
 
		bool exit = false;
 

	
 
		/* Call the proc of the creator to continue this thread */
 
		try {
 
			m_proc(m_param);
 
		} catch (...) {
 
			exit = true;
 
		}
 

	
 
		/* Notify threads waiting for our completion */
 
		sem_post(&m_sem_stop);
 

	
 
		if (exit) delete this;
 
		if (this->m_terminate_func != NULL) this->m_terminate_func(this);
 
	}
 
};
 

	
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func)
 
{
 
	return new ThreadObject_pthread(proc, param);
 
	return new ThreadObject_pthread(proc, param, terminate_func);
 
}
 

	
 
/* static */ ThreadObject *ThreadObject::AttachCurrent()
 
{
 
	return new ThreadObject_pthread();
 
}
src/thread_win32.cpp
Show inline comments
 
@@ -17,23 +17,25 @@ class ThreadObject_Win32 : public Thread
 
private:
 
	uint     m_id_thr;
 
	HANDLE   m_h_thr;
 
	OTTDThreadFunc m_proc;
 
	void     *m_param;
 
	bool     m_attached;
 
	OTTDThreadTerminateFunc m_terminate_func;
 

	
 
public:
 
	/**
 
	 * Create a win32 thread and start it, calling proc(param).
 
	 */
 
	ThreadObject_Win32(OTTDThreadFunc proc, void *param) :
 
	ThreadObject_Win32(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func) :
 
		m_id_thr(0),
 
		m_h_thr(NULL),
 
		m_proc(proc),
 
		m_param(param),
 
		m_attached(false)
 
		m_attached(false),
 
		m_terminate_func(terminate_func)
 
	{
 
		m_h_thr = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &m_id_thr);
 
		if (m_h_thr == NULL) return;
 
		ResumeThread(m_h_thr);
 
	}
 

	
 
@@ -42,13 +44,14 @@ public:
 
	 */
 
	ThreadObject_Win32() :
 
		m_id_thr(0),
 
		m_h_thr(NULL),
 
		m_proc(NULL),
 
		m_param(NULL),
 
		m_attached(false)
 
		m_attached(false),
 
		m_terminate_func(NULL)
 
	{
 
		BOOL ret = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &m_h_thr, 0, FALSE, DUPLICATE_SAME_ACCESS);
 
		if (!ret) return;
 
		m_id_thr = GetCurrentThreadId();
 
	}
 

	
 
@@ -127,18 +130,20 @@ private:
 
	void ThreadProc()
 
	{
 
		try {
 
			m_proc(m_param);
 
		} catch (...) {
 
		}
 

	
 
		if (this->m_terminate_func != NULL) this->m_terminate_func(this);
 
	}
 
};
 

	
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
 
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func)
 
{
 
	return new ThreadObject_Win32(proc, param);
 
	return new ThreadObject_Win32(proc, param, terminate_func);
 
}
 

	
 
/* static */ ThreadObject* ThreadObject::AttachCurrent()
 
{
 
	return new ThreadObject_Win32();
 
}
0 comments (0 inline, 0 general)