GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
child-list.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "child-list.h"
31 #include "oct-syscalls.h"
32 
34 
35 void child_list::remove (pid_t pid)
36 {
37  m_list.remove_if ([pid] (const child& oc) { return oc.m_pid == pid; });
38 }
39 
40 void child_list::child_list::insert (pid_t pid, child::child_event_handler f)
41 {
42  m_list.append (child (pid, f));
43 }
44 
45 void child_list::reap (void)
46 {
47  // Mark the record for PID invalid.
48 
49  for (auto& oc : m_list)
50  {
51  // The call to the child::child_event_handler might
52  // invalidate the iterator (for example, by calling
53  // child_list::remove), so we increment the iterator
54  // here.
55 
56  if (oc.m_have_status)
57  {
58  oc.m_have_status = 0;
59 
60  child::child_event_handler f = oc.m_handler;
61 
62  if (f && f (oc.m_pid, oc.m_status))
63  oc.m_pid = -1;
64  }
65  }
66 
67  // Remove PIDs that have completed above.
68  remove (-1);
69 }
70 
71 // Wait on our children and record any changes in their status.
72 
73 bool child_list::wait (void)
74 {
75  bool retval = false;
76 
77  for (auto& oc : m_list)
78  {
79  pid_t pid = oc.m_pid;
80 
81  if (pid > 0)
82  {
83  int status;
84 
85  if (sys::waitpid (pid, &status, sys::wnohang ()) > 0)
86  {
87  oc.m_have_status = 1;
88 
89  oc.m_status = status;
90 
91  retval = true;
92 
93  break;
94  }
95  }
96  }
97 
98  return retval;
99 }
100 
OCTAVE_END_NAMESPACE(octave)
bool wait(void)
Definition: child-list.cc:73
base_list< child > m_list
Definition: child-list.h:91
void remove(pid_t pid)
Definition: child-list.cc:35
void reap(void)
Definition: child-list.cc:45
bool(* child_event_handler)(pid_t, int)
Definition: child-list.h:49
pid_t m_pid
Definition: child-list.h:62
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE * f
int wnohang(void)
pid_t waitpid(pid_t pid, int *status, int options)