Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

thread_pthread.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: thread_pthread.cpp,v 1.1.1.1 2000/04/09 12:18:02 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #include "Core/precomp.h"
00016 
00017 #include <pthread.h>
00018 #include "API/Core/System/cl_assert.h"
00019 #include "thread_pthread.h"
00020 
00021 CL_Thread *CL_Thread::create(CL_Runnable *runnable)
00022 {
00023         return new CL_Thread_Posix(runnable);
00024 }
00025 
00026 CL_Thread_Posix::CL_Thread_Posix(CL_Runnable *runnable)
00027 {
00028         this->runnable = runnable;
00029         running = false;
00030 }
00031 
00032 CL_Thread_Posix::~CL_Thread_Posix()
00033 {
00034         terminate();
00035 }
00036 
00037 void CL_Thread_Posix::start()
00038 {
00039         if (running) return;
00040 
00041         cl_assert(
00042                 pthread_create(
00043                         &thread,
00044                         NULL,
00045                         run_init,
00046                         this)==0);
00047 
00048         running = true;
00049 }
00050 
00051 void *CL_Thread_Posix::run_init(void *_self)
00052 {
00053         CL_Thread_Posix *self = (CL_Thread_Posix *) _self;
00054 
00055         // kill thread immidiately - no cancelation point...
00056         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
00057 
00058         self->runnable->run();
00059         return NULL;
00060 }
00061 
00062 void CL_Thread_Posix::terminate()
00063 {
00064         if (running) pthread_cancel(thread);
00065         running = false;
00066 }
00067 
00068 void CL_Thread_Posix::wait()
00069 {
00070         if (running) pthread_join(thread, NULL);
00071         running = false;
00072 }

Generated at Wed Apr 4 19:54:04 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001