libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
thread_wrapper_impl.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2004 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace ppd {
20 
21 // Implementation details..... Don't look below here!!! ;)
22 
23 // "wrapper" implementation...
24 /* Can't delete a sequential-traits thread.
25  template<generic_traits::api_type API, class TWC> inline void
26  wrapper<API, sequential_mode, TWC>::wait_thread_exit() {
27  }
28 */
29  template<generic_traits::api_type API, class Mdl, class TWC> inline
30  wrapper<API, Mdl, TWC>::wrapper(const typename thread_traits::api_params_type::suspend_period_ms ew) noexcept(false)
31  : base_t(ew), exit_requested(false) {
32  }
33 
34  template<generic_traits::api_type API, class Mdl, class TWC> inline
35  wrapper<API, Mdl, TWC>::wrapper(thread_context_t &&thread_context, const typename thread_traits::api_params_type::suspend_period_ms ew) noexcept(false)
36  : base_t(ew), exit_requested(false), thread_context_(std::move(thread_context)) {
37  }
38 
39  template<generic_traits::api_type API, class Mdl, class TWC> inline
40  wrapper<API, Mdl, TWC>::wrapper(const wrapper &tb) noexcept(true)
41  : base_t(tb), exit_requested(false), thread_context_(tb.thread_context_) {
42  }
43 
44  template<generic_traits::api_type API, class Mdl, class TWC> inline bool
45  wrapper<API, Mdl, TWC>::pre_exit() noexcept(false) {
46  return exit_requested;
47  }
48 
49  template<generic_traits::api_type API, class Mdl, class TWC> inline typename wrapper<API, Mdl, TWC>::thread_traits::api_params_type::states
50  wrapper<API, Mdl, TWC>::process() noexcept(false) {
51  thread_context_t obj(std::move(thread_context_));
52  while (!this->pre_exit() && !this->worker_fn(obj));
53  return thread_traits::api_params_type::no_kernel_thread;
54  }
55 
56  template<generic_traits::api_type API, class Mdl, class TWC> inline
57  wrapper<API, Mdl, TWC>::~wrapper() noexcept(false) {
58  this->exit_requested=true;
59  base_t::wait_thread_exit();
60  // See how I've tried to clean up everything before we might throw here?
61  // Yeah - well - apart from all of the base class stuff, etc, etc. So it's really rather bad...
62  this->exception_thrown_in_thread.throw_if_set();
63  }
64 
65  template<generic_traits::api_type API, class Mdl> inline
66  thread<API, Mdl>::thread(const typename thread_traits::api_params_type::suspend_period_ms ew) noexcept(false)
67  : base_t(ew) {
68  }
69 
70  template<generic_traits::api_type API, class Mdl>
71  template<class ProcFn> inline
72  thread<API, Mdl>::thread(ProcFn &&proc_fn, const typename thread_traits::api_params_type::suspend_period_ms ew) noexcept(false)
73  : base_t(std::forward<ProcFn>(proc_fn), ew) {
74  this->create_running();
75  }
76 
77  template<generic_traits::api_type API, class Mdl> inline
78  thread<API, Mdl>::~thread() noexcept(false) {
79  this->exit_requested=true;
80  base_t::wait_thread_exit();
81  }
82 
83  template<generic_traits::api_type API, class Mdl> inline bool
84  thread<API, Mdl>::worker_fn(thread_context_t &context) noexcept(false) {
85  context();
86  return true;
87  }
88 
89 } } }