GNU Octave  9.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-2024 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
41 child_list::child_list::insert (pid_t pid, child::child_event_handler f)
42 {
43  m_list.append (child (pid, f));
44 }
45 
46 void
48 {
49  // Mark the record for PID invalid.
50 
51  for (auto& oc : m_list)
52  {
53  // The call to the child::child_event_handler might
54  // invalidate the iterator (for example, by calling
55  // child_list::remove), so we increment the iterator
56  // here.
57 
58  if (oc.m_have_status)
59  {
60  oc.m_have_status = 0;
61 
62  child::child_event_handler f = oc.m_handler;
63 
64  if (f && f (oc.m_pid, oc.m_status))
65  oc.m_pid = -1;
66  }
67  }
68 
69  // Remove PIDs that have completed above.
70  remove (-1);
71 }
72 
73 // Wait on our children and record any changes in their status.
74 
75 bool
77 {
78  bool retval = false;
79 
80  for (auto& oc : m_list)
81  {
82  pid_t pid = oc.m_pid;
83 
84  if (pid > 0)
85  {
86  int status;
87 
88  if (sys::waitpid (pid, &status, sys::wnohang ()) > 0)
89  {
90  oc.m_have_status = 1;
91 
92  oc.m_status = status;
93 
94  retval = true;
95 
96  break;
97  }
98  }
99  }
100 
101  return retval;
102 }
103 
104 OCTAVE_END_NAMESPACE(octave)
void remove(pid_t pid)
Definition: child-list.cc:35
bool wait()
Definition: child-list.cc:76
void reap()
Definition: child-list.cc:47
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
pid_t waitpid(pid_t pid, int *status, int options)
int wnohang()