Thread.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages 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 freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/System/Unix/Thread.hpp>
00029 #include <iostream>
00030 
00031 
00032 namespace sf
00033 {
00037 Thread::Thread() :
00038 myIsActive(false),
00039 myFunction(NULL),
00040 myUserData(NULL)
00041 {
00042 
00043 }
00044 
00045 
00049 Thread::Thread(Thread::FuncType Function, void* UserData) :
00050 myIsActive(false),
00051 myFunction(Function),
00052 myUserData(UserData)
00053 {
00054 
00055 }
00056 
00057 
00061 Thread::~Thread()
00062 {
00063     // Wait for the thread to finish before destroying the instance
00064     if (myIsActive)
00065         Wait();
00066 }
00067 
00068 
00072 void Thread::Launch()
00073 {
00074     // Create the thread
00075     myIsActive = true;
00076     int Error = pthread_create(&myThread, NULL, &Thread::ThreadFunc, this);
00077 
00078     // Error ?
00079     if (Error != 0)
00080     {
00081         std::cerr << "Failed to create thread" << std::endl;
00082         myIsActive = false;
00083     }
00084 }
00085 
00086 
00090 void Thread::Wait()
00091 {
00092     if (myIsActive)
00093     {
00094         // Wait for the thread to finish, no timeout
00095         pthread_join(myThread, NULL);
00096 
00097         // Reset the thread state
00098         myIsActive = false;
00099     }
00100 }
00101 
00102 
00109 void Thread::Terminate()
00110 {
00111     if (myIsActive)
00112     {
00113         pthread_cancel(myThread);
00114         myIsActive = false;
00115     }
00116 }
00117 
00118 
00122 void Thread::Run()
00123 {
00124     if (myFunction)
00125         myFunction(myUserData);
00126 }
00127 
00128 
00132 void* Thread::ThreadFunc(void* UserData)
00133 {
00134     // The sfThread instance is stored in the user data
00135     Thread* ThreadToRun = reinterpret_cast<Thread*>(UserData);
00136 
00137     // Tell the thread to handle cancel requests immediatly
00138     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
00139 
00140     // Forward to the instance
00141     ThreadToRun->Run();
00142 
00143     return NULL;
00144 }
00145 
00146 } // namespace sf