-#ifdef WIN32
-#include <windows.h>
-#else
-#include <pthread.h>
-#include <signal.h>
-#endif
#include <stdexcept>
#include "thread.h"
+#include "thread_private.h"
using namespace std;
namespace Msp {
-struct Thread::Private
-{
-#ifdef WIN32
- HANDLE handle;
-#else
- pthread_t handle;
-#endif
-
- Private(): handle(0) { }
-
-#ifdef WIN32
- static DWORD WINAPI
-#else
- static void *
-#endif
- main_wrapper(void *a)
- {
- Thread *t = reinterpret_cast<Thread *>(a);
- t->main();
- t->state_ = FINISHED;
- return 0;
- }
-};
-
-
Thread::Thread():
priv_(new Private),
state_(PENDING)
if(state_>=JOINED)
return;
-#ifdef WIN32
- WaitForSingleObject(priv_->handle, INFINITE);
-#else
- pthread_join(priv_->handle, 0);
-#endif
+ platform_join();
state_ = JOINED;
}
if(state_!=RUNNING)
return;
-#ifdef WIN32
- TerminateThread(priv_->handle, 0);
-#else
- pthread_kill(priv_->handle, SIGKILL);
-#endif
+ platform_kill();
state_ = KILLED;
}
if(state_>=RUNNING)
throw logic_error("already launched");
-#ifdef WIN32
- DWORD dummy; // Win9x needs the lpTthreadId parameter
- priv_->handle = CreateThread(0, 0, &Private::main_wrapper, this, 0, &dummy);
-#else
- pthread_create(&priv_->handle, 0, &Private::main_wrapper, this);
-#endif
+ platform_launch();
state_ = RUNNING;
}
+ThreadReturn THREAD_CALL Thread::Private::main_wrapper(void *arg)
+{
+ Thread *thread = reinterpret_cast<Thread *>(arg);
+ thread->main();
+ thread->state_ = FINISHED;
+ return 0;
+}
+
} // namespace Msp