libjmmcg
release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
pool_thread_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
ppd
{
namespace
pool
{
namespace
thread_types
{
20
21
template
<
class
WQ
>
inline
22
steal
<
generic_traits
::
return_data
::
nonjoinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
23
steal
(
exit_requested_type
&
exit_requested
,
signalled_work_queue_type
&
work_q
)
noexcept
(
true
)
24
:
base_t
(
exit_requested
),
signalled_work_queue
(
work_q
) {
25
}
26
27
template
<
class
WQ
>
inline
28
steal
<
generic_traits
::
return_data
::
nonjoinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
29
~
steal
() {
30
base_t
::
wait_thread_exit
();
31
}
32
33
template
<
class
WQ
>
inline
34
typename
steal
<
generic_traits
::
return_data
::
nonjoinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
thread_traits
::
api_params_type
::
states
35
steal
<
generic_traits
::
return_data
::
nonjoinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
36
process
()
noexcept
(
false
) {
37
// Serialize access to the queue by all of the other threads in the pool, this means that only one job can be removed at a time from the work queue.
38
for
(;;) {
39
const
typename
exit_requested_type
::
lock_result_type
lkd
=
this
->
exit_requested
.
lock
();
40
assert
(
lkd
.
second
==
lock_traits
::
atom_set
);
41
if
(
lkd
.
first
==
exit_requested_type
::
states
::
exit_requested
) {
42
// Ensure the rest of the threads in the pool exit.
43
this
->
exit_requested
.
set
(
exit_requested_type
::
states
::
exit_requested
);
44
break
;
45
}
else
if
(
lkd
.
first
==
exit_requested_type
::
states
::
new_work_arrived
) {
46
batch
.
process_a_batch
(
this
->
exception_thrown_in_thread
,
signalled_work_queue
);
47
}
48
}
49
return
thread_traits
::
api_params_type
::
no_kernel_thread
;
50
}
51
52
template
<
class
WQ
>
inline
53
slave
<
generic_traits
::
return_data
::
nonjoinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
54
slave
(
exit_requested_type
&
exit_requested
,
typename
signalled_work_queue_type
::
value_type
&
wk
)
noexcept
(
true
)
55
:
base_t
(
exit_requested
),
some_work
(
wk
) {
56
}
57
58
template
<
class
WQ
>
inline
59
slave
<
generic_traits
::
return_data
::
nonjoinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
60
~
slave
() {
61
base_t
::
wait_thread_exit
();
62
}
63
64
template
<
class
WQ
>
inline
65
typename
slave
<
generic_traits
::
return_data
::
nonjoinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
thread_traits
::
api_params_type
::
states
66
slave
<
generic_traits
::
return_data
::
nonjoinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
67
process
()
noexcept
(
false
) {
68
some_work
->
process
();
69
return
thread_traits
::
api_params_type
::
no_kernel_thread
;
70
}
71
72
template
<
class
WQ
>
inline
73
steal
<
generic_traits
::
return_data
::
joinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
74
steal
(
exit_requested_type
&
exit_r
,
signalled_work_queue_type
&
work_q
)
noexcept
(
true
)
75
:
base_t
(
exit_r
),
signalled_work_queue
(
work_q
) {
76
}
77
78
template
<
class
WQ
>
inline
79
steal
<
generic_traits
::
return_data
::
joinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
80
~
steal
() {
81
base_t
::
wait_thread_exit
();
82
}
83
84
template
<
class
WQ
>
inline
85
typename
steal
<
generic_traits
::
return_data
::
joinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
thread_traits
::
api_params_type
::
states
86
steal
<
generic_traits
::
return_data
::
joinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
87
process
()
noexcept
(
false
) {
88
// Serialize access to the queue by all of the other threads in the pool, this means that only one job can be removed at a time from the work queue.
89
for
(;;) {
90
const
typename
exit_requested_type
::
lock_result_type
lkd
=
this
->
exit_requested
.
lock
();
91
assert
(
lkd
.
second
==
lock_traits
::
atom_set
);
92
if
(
lkd
.
first
==
exit_requested_type
::
states
::
exit_requested
) {
93
// Ensure the rest of the threads in the pool exit.
94
this
->
exit_requested
.
set
(
exit_requested_type
::
states
::
exit_requested
);
95
break
;
96
}
else
if
(
lkd
.
first
==
exit_requested_type
::
states
::
new_work_arrived
) {
97
batch
.
process_a_batch
(
this
->
exception_thrown_in_thread
,
signalled_work_queue
);
98
}
99
}
100
return
thread_traits
::
api_params_type
::
no_kernel_thread
;
101
}
102
103
template
<
class
WQ
>
inline
104
slave
<
generic_traits
::
return_data
::
joinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
105
slave
(
exit_requested_type
&
exit_requested
,
typename
signalled_work_queue_type
::
value_type
&
wk
)
noexcept
(
true
)
106
:
base_t
(
exit_requested
),
some_work
(
wk
) {
107
}
108
109
template
<
class
WQ
>
inline
110
slave
<
generic_traits
::
return_data
::
joinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
111
~
slave
() {
112
base_t
::
wait_thread_exit
();
113
}
114
115
template
<
class
WQ
>
inline
116
typename
slave
<
generic_traits
::
return_data
::
joinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
thread_traits
::
api_params_type
::
states
117
slave
<
generic_traits
::
return_data
::
joinable
,
thread_os_traits
<
generic_traits
::
MS_Win32
,
heavyweight_threading
>,
WQ
>::
118
process
()
noexcept
(
false
) {
119
if
(
some_work
->
result_traits
()==
generic_traits
::
return_data
::
joinable
) {
120
some_work
->
process
(
this
->
exception_thrown_in_thread
);
121
}
else
{
122
some_work
->
process
();
123
}
124
return
thread_traits
::
api_params_type
::
no_kernel_thread
;
125
}
126
127
} } } }
experimental
NT-based
NTSpecific
pool_thread_impl.hpp
Generated on Tue May 11 2021 17:31:30 for libjmmcg by
1.9.2