sprint 1-alpha
|
00001 /****************************************************************************** 00002 * sprint::thread (function) 00003 * 00004 * Copyright (C) 2005-2011 Paolo Medici <www.pmx.it> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 * 00020 *******************************************************************************/ 00021 00022 #ifndef _SPRINT_THREAD_FUNCTION_H 00023 #define _SPRINT_THREAD_FUNCTION_H 00024 00029 #include <sprint/thread/detail/platform.h> 00030 00031 #ifdef SPRINT_THREAD_WIN32 00032 # include <windows.h> 00033 #else 00034 # include <pthread.h> 00035 #endif 00036 00037 namespace sprint { 00038 00039 #ifdef SPRINT_THREAD_WIN32 00040 00042 struct thread_function { 00043 // Some Traits 00044 00046 typedef DWORD ReturnType; 00048 typedef void * ParamType; 00049 00051 typedef LPTHREAD_START_ROUTINE CallBackType; 00052 00054 CallBackType proc; 00055 00057 ParamType param; 00058 00059 thread_function(CallBackType _proc, ParamType _param) : proc(_proc), param(_param) { } 00060 }; 00061 00062 namespace detail { 00063 00065 template<class R, R (*Func)()> 00066 DWORD __stdcall CALLBACK embedded_ptr_fun(void *__Unused) 00067 { 00068 return Func(); 00069 } 00070 00071 // static linking for a member function R (T::*F)() 00072 template<class R, class T, R (T::*F)()> 00073 DWORD __stdcall CALLBACK embedded_mem_fun(void *p) 00074 { 00075 return ((reinterpret_cast<T*>(p))->*F)(); 00076 } 00077 00081 template<class _Invoker> 00082 DWORD __stdcall CALLBACK thread_fun(void *_p) 00083 { 00084 typename _Invoker::ParamType *p = reinterpret_cast< typename _Invoker::ParamType *> (_p); 00085 00086 _Invoker::run(*p); 00087 00088 delete p; 00089 return 0; 00090 } 00091 00092 } 00093 00094 #elif defined SPRINT_THREAD_POSIX 00095 00097 struct thread_function { 00098 // Some Traits 00099 00100 typedef void * ReturnType; 00101 typedef void * ParamType; 00102 00104 typedef void *(*CallBackType) (void *); 00105 00106 CallBackType proc; 00107 ParamType param; 00108 00109 thread_function(CallBackType _proc, ParamType _param) : proc(_proc), param(_param) { } 00110 }; 00111 00112 namespace detail { 00113 00115 template<class R, R (*Func)()> 00116 void * embedded_ptr_fun(void *__Unused) 00117 { 00118 return Func(); 00119 } 00120 00121 // jumper: funzione trampolino per chiamare una funzione R (T::*F)() 00122 // NOTA: il valore di R e' importante... come fare per ottenerlo automaticamente? 00123 template<class R, class T, R (T::*F)()> 00124 void * embedded_mem_fun(void *p) 00125 { 00126 return ((reinterpret_cast<T*>(p))->*F)(); 00127 } 00128 00129 template<class _Invoker> 00130 void * thread_fun(void *_p) 00131 { 00132 typename _Invoker::ParamType *p = reinterpret_cast< typename _Invoker::ParamType *> (_p); 00133 00134 _Invoker::run(*p); 00135 00136 delete p; 00137 return 0; 00138 } 00139 00140 } 00141 00142 #endif 00143 00144 } // naemspace sprint 00145 00146 #endif