libjmmcg
release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
thread_base.cpp
Go to the documentation of this file.
1
/******************************************************************************
2
** Copyright © 2005 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
#
include
"../core/private_/thread_base.hpp"
20
21
#
include
<
boost
/
exception
/
diagnostic_information
.
hpp
>
22
23
// Implementation details..... Don't look below here!!! ;)
24
25
JMMCG_REVISION_CONSTANT
(
_T
(
LIBJMMCG_VERSION_NUMBER
))
26
27
// "thread_base" implementation...
28
29
namespace
jmmcg
{
namespace
LIBJMMCG_VER_NAMESPACE
{
namespace
ppd
{
namespace
private_
{
30
31
typedef
thread_base
<
generic_traits
::
api_type
::
posix_pthreads
, sequential_mode>
thread_t_s
;
32
33
template
<>
thread_t_s
::
thread_traits
::
api_params_type
::
core_work_fn_ret_t
34
thread_t_s
::
core_work_fn
(
thread_traits
::
api_params_type
::
core_work_fn_arg_t
ptr
)
noexcept
(
false
) {
35
assert(ptr);
36
thread_t_s
*
const
pthis
=
reinterpret_cast
<
thread_t_s
*>(
ptr
);
37
assert(pthis);
38
if
(
pthis
) {
39
try
{
40
pthis
->
thread_params
.
state
=
thread_traits
::
api_params_type
::
active
;
41
thread_traits
::
set_cancelstate
(
thread_traits
::
api_params_type
::
cancel_disable
);
42
int
old_state
;
43
[[
maybe_unused
]]
const
int
pth_err
=
pthread_setcanceltype
(PTHREAD_CANCEL_DEFERRED, &
old_state
);
44
assert(pth_err==0);
45
const
thread_traits
::
api_params_type
::
states
ret
=
pthis
->
process_chk
();
46
pthis
->
thread_params
.
state
=
thread_traits
::
api_params_type
::
exiting
;
47
return
reinterpret_cast
<
thread_traits
::
api_params_type
::
core_work_fn_ret_t
>(
ret
);
48
#
if
defined
(
__GNUC__
)
&&
!
defined
(
__clang__
)
49
}
catch
(
abi
::
__forced_unwind
const
&) {
50
pthis
->
thread_params
.
state
=
thread_traits
::
api_params_type
::
exiting
;
51
throw
;
52
#
endif
53
}
catch
(
std
::
exception
const
&
ex
) {
54
JMMCG_TRACE
(_T(
"STL-derived exception. Details: "
)<<boost::diagnostic_information(ex));
55
pthis
->
exception_thrown_in_thread
.
set
(
std
::
current_exception
());
56
}
catch
(...) {
57
JMMCG_TRACE
(_T(
"Unknown exception caught. If it is a SIGSEGV, then the ss value in the thread_params ctor may need increasing."
));
58
auto
ex
=
std
::
current_exception
();
59
if
(
ex
) {
60
pthis
->
exception_thrown_in_thread
.
set
(
ex
);
61
}
62
throw
;
63
}
64
pthis
->
thread_params
.
state
=
thread_traits
::
api_params_type
::
exiting
;
65
return
reinterpret_cast
<
thread_traits
::
api_params_type
::
core_work_fn_ret_t
>(
thread_traits
::
api_params_type
::
unknown_exception
);
66
}
67
// In reality, no errors are actively reported to the user for this case, as the pthis was null.
68
// So we almost silently fail, and hope that they aren't sooooo dumb as to not notice that their work isn't being processed...
69
// This is because the only "opportunity" I have to report it is as an exception in the destructor. Hooo-boy would that be evil...
70
// Just think of the memory leaks.... So I don't do that. That's the breaks....
71
return
reinterpret_cast
<
thread_traits
::
api_params_type
::
core_work_fn_ret_t
>(
thread_traits
::
api_params_type
::
null_this_pointer
);
72
}
73
74
typedef
thread_base
<
generic_traits
::
api_type
::
posix_pthreads
,
heavyweight_threading
>
thread_t_m
;
75
76
template
<>
thread_t_m
::
thread_traits
::
api_params_type
::
core_work_fn_ret_t
77
thread_t_m
::
core_work_fn
(
thread_traits
::
api_params_type
::
core_work_fn_arg_t
ptr
)
noexcept
(
false
) {
78
assert(ptr);
79
thread_t_m
*
const
pthis
=
reinterpret_cast
<
thread_t_m
*>(
ptr
);
80
assert(
dynamic_cast
<thread_t_m *>(pthis));
81
if
(
pthis
) {
82
try
{
83
pthis
->
thread_params
.
state
=
thread_traits
::
api_params_type
::
active
;
84
thread_traits
::
set_cancelstate
(
thread_traits
::
api_params_type
::
cancel_disable
);
85
int
old_state
;
86
[[
maybe_unused
]]
const
int
pth_err
=
pthread_setcanceltype
(PTHREAD_CANCEL_DEFERRED, &
old_state
);
87
assert(pth_err==0);
88
const
thread_traits
::
api_params_type
::
states
ret
=
pthis
->
process_chk
();
89
pthis
->
thread_params
.
state
=
thread_traits
::
api_params_type
::
exiting
;
90
return
reinterpret_cast
<
thread_traits
::
api_params_type
::
core_work_fn_ret_t
>(
ret
);
91
#
if
defined
(
__GNUC__
)
&&
!
defined
(
__clang__
)
92
}
catch
(
abi
::
__forced_unwind
const
&) {
93
pthis
->
thread_params
.
state
=
thread_traits
::
api_params_type
::
exiting
;
94
throw
;
95
#
endif
96
}
catch
(
std
::
exception
const
&
ex
) {
97
JMMCG_TRACE
(_T(
"STL-derived exception. Details: "
)<<boost::diagnostic_information(ex));
98
pthis
->
exception_thrown_in_thread
.
set
(
std
::
current_exception
());
99
}
catch
(...) {
100
JMMCG_TRACE
(_T(
"Unknown exception caught. If it is a SIGSEGV, then the ss value in the thread_params ctor may need increasing."
));
101
auto
ex
=
std
::
current_exception
();
102
if
(
ex
) {
103
pthis
->
exception_thrown_in_thread
.
set
(
ex
);
104
}
105
throw
;
106
}
107
pthis
->
thread_params
.
state
=
thread_traits
::
api_params_type
::
exiting
;
108
return
reinterpret_cast
<
thread_traits
::
api_params_type
::
core_work_fn_ret_t
>(
thread_traits
::
api_params_type
::
unknown_exception
);
109
}
110
// In reality, no errors are actively reported to the user for this case, as the pthis was null.
111
// So we almost silently fail, and hope that they aren't sooooo dumb as to not notice that their work isn't being processed...
112
// This is because the only "opportunity" I have to report it is as an exception in the destructor. Hooo-boy would that be evil...
113
// Just think of the memory leaks.... So I don't do that. That's the breaks....
114
return
reinterpret_cast
<
thread_traits
::
api_params_type
::
core_work_fn_ret_t
>(
thread_traits
::
api_params_type
::
null_this_pointer
);
115
}
116
117
} } } }
unix
thread_base.cpp
Generated on Tue May 11 2021 17:22:07 for libjmmcg by
1.9.2