Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

thread.cpp

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2005 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00029 #include "precomp.h"
00030 #include "thread.h"
00031 #include "runnable.h"
00032 
00034 // CL_Thread Construction:
00035 
00036 CL_Thread::CL_Thread() : handle(0)
00037 {
00038 }
00039 
00040 CL_Thread::~CL_Thread()
00041 {
00042 #ifdef WIN32
00043         if (handle)
00044                 CloseHandle(handle);
00045 #else
00046         // todo: Is this needed? We'd rather not kill the thread :/
00047         //       (yes, I'm too lazy to try find the linsux docs for it)
00048         if (handle)
00049                 pthread_cancel(handle);
00050 #endif
00051 }
00052 
00054 // CL_Thread Attributes:
00055 
00057 // CL_Thread Operations:
00058 
00059 void CL_Thread::start(CL_Runnable *runnable)
00060 {
00061         if (runnable == 0)
00062                 throw CL_Exception(TEXT("Invalid runnable pointer"));
00063 
00064 #ifdef WIN32
00065         if (handle)
00066                 CloseHandle(handle);
00067 
00068         DWORD threadId = 0;
00069         handle = CreateThread(0, 0, &CL_Thread::thread_main, runnable, 0, &threadId);
00070         if (handle == 0)
00071         {
00072                 throw CL_Exception(TEXT("Unable to create new thread"));
00073         }
00074 #else
00075         int result = pthread_create(&handle, 0, thread_main, runnable);
00076         if (result != 0)
00077         {
00078                 handle = 0;
00079                 throw CL_Exception(TEXT("Unable to create new thread"));
00080         }
00081 #endif
00082 }
00083 
00084 void CL_Thread::join()
00085 {
00086 #ifdef WIN32
00087         WaitForSingleObject(handle, INFINITE);
00088         if (handle)
00089                 CloseHandle(handle);
00090 #else
00091         pthread_join(handle, 0);
00092 #endif
00093         handle = 0;
00094 }
00095 
00097 // CL_Thread Implementation:
00098 
00099 #ifdef WIN32
00100 DWORD CL_Thread::thread_main(void *data)
00101 {
00102         CL_Runnable *runnable = (CL_Runnable *) data;
00103         runnable->run();
00104         return 0;
00105 }
00106 #else
00107 void *CL_Thread::thread_main(void *data)
00108 {
00109         // kill thread immidiately - no cancelation point
00110         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
00111 
00112         CL_Runnable *runnable = (CL_Runnable *) data;
00113         runnable->run();
00114         return 0;
00115 }
00116 #endif

Generated on Sat Feb 19 22:51:16 2005 for npcore by  doxygen 1.4.1