|
libpsynth 0.2.1
|
00001 /*************************************************************************** 00002 * * 00003 * PSYCHOSYNTH * 00004 * =========== * 00005 * * 00006 * Copyright (C) 2007 Juan Pedro Bolivar Puente * 00007 * * 00008 * This program is free software: you can redistribute it and/or modify * 00009 * it under the terms of the GNU General Public License as published by * 00010 * the Free Software Foundation, either version 3 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This program is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 00020 * * 00021 ***************************************************************************/ 00022 00023 #ifndef PSYNTH_THREAD_H 00024 #define PSYNTH_THREAD_H 00025 00026 #include <pthread.h> 00027 00028 namespace psynth 00029 { 00030 00031 #include <libpsynth/common/FastDelegate.h> 00032 using namespace fastdelegate; 00033 00041 class runnable 00042 { 00043 public: 00044 virtual ~runnable() {}; 00045 00049 virtual void run() = 0; 00050 }; 00051 00055 typedef FastDelegate0<void> thread_delegate; 00056 00070 class thread 00071 { 00072 private: 00073 pthread_t m_thread; 00074 thread_delegate m_delegate; 00075 00076 static void* process(void* obj) { 00077 thread* the_this = reinterpret_cast<thread*>(obj); 00078 the_this->m_delegate(); 00079 00080 return NULL; 00081 }; 00082 00083 public: 00088 thread (runnable& obj) { 00089 m_delegate = MakeDelegate(&obj, &runnable::run); 00090 } 00091 00096 thread (thread_delegate func) { 00097 m_delegate = func; 00098 } 00099 00104 void set (runnable& obj) { 00105 m_delegate = MakeDelegate(&obj, &runnable::run); 00106 } 00107 00112 void set (thread_delegate func) { 00113 m_delegate = func; 00114 } 00115 00119 void start () { 00120 pthread_create(&m_thread, NULL, &thread::process, this); 00121 } 00122 00127 void start (runnable& obj) { 00128 m_delegate = MakeDelegate(&obj, &runnable::run); 00129 pthread_create(&m_thread, NULL, &thread::process, this); 00130 } 00131 00136 void start (thread_delegate func) { 00137 m_delegate = func; 00138 pthread_create(&m_thread, NULL, &thread::process, this); 00139 } 00140 00147 void join () { 00148 pthread_join(m_thread, NULL); 00149 } 00150 00151 /* 00152 pthread_t getThreadId() { 00153 return m_thread; 00154 }; 00155 */ 00156 }; 00157 00163 class self_thread : public runnable, 00164 public thread 00165 { 00166 public: 00168 virtual ~self_thread() {}; 00169 00171 self_thread () : 00172 thread (static_cast<runnable&>(*this)) 00173 {}; 00174 }; 00175 00176 } /* namespace psynth */ 00177 00178 #endif /* PSYNTH_THREAD_H */
1.7.4