00001 // 00002 // $Id: thread.cpp 2 2008-03-01 20:58:50Z kulibali $ 00003 // 00004 // Copyright (c) 2008, The Periapsis Project. All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are 00008 // met: 00009 // 00010 // * Redistributions of source code must retain the above copyright notice, 00011 // this list of conditions and the following disclaimer. 00012 // 00013 // * Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // * Neither the name of the The Periapsis Project nor the names of its 00018 // contributors may be used to endorse or promote products derived from 00019 // this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00022 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00023 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00024 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00025 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00027 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 00034 #include "platform/thread.hpp" 00035 #include "data/exception.hpp" 00036 #include "platform/lowlevel.hpp" 00037 00038 namespace gsgl 00039 { 00040 00041 namespace platform 00042 { 00043 00044 mutex::mutex() 00045 : m(0) 00046 { 00047 m = SDL_CreateMutex(); 00048 } // mutex::mutex() 00049 00050 00051 mutex::~mutex() 00052 { 00053 if (m) 00054 SDL_DestroyMutex(m); 00055 } // mutex::~mutex() 00056 00057 00058 void mutex::lock() 00059 { 00060 if (m) 00061 SDL_LockMutex(m); 00062 } // mutex::lock() 00063 00064 00065 void mutex::unlock() 00066 { 00067 if (m) 00068 SDL_UnlockMutex(m); 00069 } // mutex::unlock() 00070 00071 00072 ////////////////////////////////////////////////////////////// 00073 00074 static int run_thread(void *data) 00075 { 00076 thread *th = (thread *) data; 00077 00078 if (th) 00079 return th->run(); 00080 else 00081 return -1; 00082 } // run_thread() 00083 00084 00085 thread::thread() 00086 : t(0) 00087 { 00088 } // thread::thread() 00089 00090 00091 thread::~thread() 00092 { 00093 if (t) 00094 SDL_WaitThread(t, 0); 00095 } // thread::~thread() 00096 00097 00098 bool thread::is_running() 00099 { 00100 return t != 0; 00101 } // thread::is_running() 00102 00103 00104 void thread::start() 00105 { 00106 if (!t) 00107 t = SDL_CreateThread(run_thread, this); 00108 else 00109 throw runtime_exception(L"You cannot run a thread that is already running!"); 00110 } // thread::start() 00111 00112 00113 void thread::kill() 00114 { 00115 if (t) 00116 SDL_KillThread(t); 00117 } // thread:kill() 00118 00119 00120 void thread::wait() 00121 { 00122 if (t) 00123 { 00124 SDL_WaitThread(t, 0); 00125 t = 0; 00126 } 00127 } // thread::wait() 00128 00129 } // namespace platform 00130 00131 } // namespace gsgl