X-Boost  2.3.8
function.h
1 /******************************************************************************
2 * sprint::thread (function)
3 *
4 * Copyright (C) 2005-2011 Paolo Medici <www.pmx.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 *******************************************************************************/
21 
22 #ifndef _SPRINT_THREAD_FUNCTION_H
23 #define _SPRINT_THREAD_FUNCTION_H
24 
29 #ifdef WIN32
30 # include <windows.h>
31 #else
32 # include <pthread.h>
33 #endif
34 
35 namespace sprint {
36 
37 #ifdef WIN32
38 
40  struct thread_function {
41  // Some Traits
42 
44  typedef DWORD ReturnType;
46  typedef void * ParamType;
47 
49  typedef LPTHREAD_START_ROUTINE CallBackType;
50 
52  CallBackType proc;
53 
55  ParamType param;
56 
57  thread_function(CallBackType _proc, ParamType _param) : proc(_proc), param(_param) { }
58  };
59 
60  namespace detail {
61 
63  template<class R, R (*Func)()>
64  DWORD __stdcall CALLBACK embedded_ptr_fun(void *__Unused)
65  {
66  return Func();
67  }
68 
69  // static linking for a member function R (T::*F)()
70  template<class R, class T, R (T::*F)()>
71  DWORD __stdcall CALLBACK embedded_mem_fun(void *p)
72  {
73  return ((reinterpret_cast<T*>(p))->*F)();
74  }
75 
79  template<class _Invoker>
80  DWORD __stdcall CALLBACK thread_fun(void *_p)
81  {
82  typename _Invoker::ParamType *p = reinterpret_cast< typename _Invoker::ParamType *> (_p);
83 
84  _Invoker::run(*p);
85 
86  delete p;
87  return 0;
88  }
89 
90  }
91 
92 #else
93 
95  struct thread_function {
96  // Some Traits
97 
98  typedef void * ReturnType;
99  typedef void * ParamType;
100 
102  typedef void *(*CallBackType) (void *);
103 
104  CallBackType proc;
105  ParamType param;
106 
107  thread_function(CallBackType _proc, ParamType _param) : proc(_proc), param(_param) { }
108  };
109 
110  namespace detail {
111 
113  template<class R, R (*Func)()>
114  void * embedded_ptr_fun(void *__Unused)
115  {
116  return Func();
117  }
118 
119  // jumper: funzione trampolino per chiamare una funzione R (T::*F)()
120  // NOTA: il valore di R e' importante... come fare per ottenerlo automaticamente?
121  template<class R, class T, R (T::*F)()>
122  void * embedded_mem_fun(void *p)
123  {
124  return ((reinterpret_cast<T*>(p))->*F)();
125  }
126 
127  template<class _Invoker>
128  void * thread_fun(void *_p)
129  {
130  typename _Invoker::ParamType *p = reinterpret_cast< typename _Invoker::ParamType *> (_p);
131 
132  _Invoker::run(*p);
133 
134  delete p;
135  return 0;
136  }
137 
138  }
139 
140 #endif
141 
142 } // naemspace sprint
143 
144 #endif
pthread thread creation params
Definition: function.h:95
void *(* CallBackType)(void *)
The callback type supporter by local thread architecture.
Definition: function.h:102