Mercurial > octave
annotate liboctave/system/child-list.cc @ 30173:f8ee588f1c7d
maint: Replace pid_equal helper class with lambda function.
* child-list.cc: Delete pid_equal helper class.
* child-list.cc (remove): Use lambda function in call to remove_if().
author | Rik <rik@octave.org> |
---|---|
date | Tue, 14 Sep 2021 10:34:28 -0700 |
parents | a8c61e096c68 |
children | 796f54d4ddbf |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
3 // Copyright (C) 1993-2021 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
25 |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
27 # include "config.h" |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
28 #endif |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
29 |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
30 #include "child-list.h" |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
31 #include "oct-syscalls.h" |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
32 |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
33 namespace octave |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
34 { |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
35 void child_list::remove (pid_t pid) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
36 { |
30173
f8ee588f1c7d
maint: Replace pid_equal helper class with lambda function.
Rik <rik@octave.org>
parents:
30063
diff
changeset
|
37 m_list.remove_if ([pid] (const child& oc) { return oc.m_pid == pid; }); |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
38 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
39 |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
40 void child_list::child_list::insert (pid_t pid, child::child_event_handler f) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
41 { |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
42 m_list.append (child (pid, f)); |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
43 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
44 |
24734
5d8c4cbc56d7
don't use singleton pattern for child_list
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
45 void child_list::reap (void) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
46 { |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
47 // Mark the record for PID invalid. |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
48 |
24736
cd25b9c5f808
eliminate useless public iterator typedefs in child_list class
John W. Eaton <jwe@octave.org>
parents:
24734
diff
changeset
|
49 for (auto& oc : m_list) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
50 { |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
51 // The call to the child::child_event_handler might |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
52 // invalidate the iterator (for example, by calling |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
53 // child_list::remove), so we increment the iterator |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
54 // here. |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
55 |
30049
bffdbda91d28
maint: use "m_" prefix for member variables in child, child_list classes
Rik <rik@octave.org>
parents:
29358
diff
changeset
|
56 if (oc.m_have_status) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
57 { |
30049
bffdbda91d28
maint: use "m_" prefix for member variables in child, child_list classes
Rik <rik@octave.org>
parents:
29358
diff
changeset
|
58 oc.m_have_status = 0; |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
59 |
30049
bffdbda91d28
maint: use "m_" prefix for member variables in child, child_list classes
Rik <rik@octave.org>
parents:
29358
diff
changeset
|
60 child::child_event_handler f = oc.m_handler; |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
61 |
30049
bffdbda91d28
maint: use "m_" prefix for member variables in child, child_list classes
Rik <rik@octave.org>
parents:
29358
diff
changeset
|
62 if (f && f (oc.m_pid, oc.m_status)) |
bffdbda91d28
maint: use "m_" prefix for member variables in child, child_list classes
Rik <rik@octave.org>
parents:
29358
diff
changeset
|
63 oc.m_pid = -1; |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
64 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
65 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
66 |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
67 // Remove PIDs that have completed above. |
24734
5d8c4cbc56d7
don't use singleton pattern for child_list
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
68 remove (-1); |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
69 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
70 |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
71 // Wait on our children and record any changes in their status. |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
72 |
24734
5d8c4cbc56d7
don't use singleton pattern for child_list
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
73 bool child_list::wait (void) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
74 { |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
75 bool retval = false; |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
76 |
24736
cd25b9c5f808
eliminate useless public iterator typedefs in child_list class
John W. Eaton <jwe@octave.org>
parents:
24734
diff
changeset
|
77 for (auto& oc : m_list) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
78 { |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
79 pid_t pid = oc.m_pid; |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
80 |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
81 if (pid > 0) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
82 { |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
83 int status; |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
84 |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
85 if (sys::waitpid (pid, &status, sys::wnohang ()) > 0) |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
86 { |
30049
bffdbda91d28
maint: use "m_" prefix for member variables in child, child_list classes
Rik <rik@octave.org>
parents:
29358
diff
changeset
|
87 oc.m_have_status = 1; |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
88 |
30063
a8c61e096c68
maint: clean up some variable renamings in cset bffdbda91d28
Rik <rik@octave.org>
parents:
30049
diff
changeset
|
89 oc.m_status = status; |
22049
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
90 |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
91 retval = true; |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
92 |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
93 break; |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
94 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
95 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
96 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
97 |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
98 return retval; |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
99 } |
ccf50f029999
move octave_child_list to separate file and define inside namespace
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
100 } |