GNU Octave  6.2.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-2021 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 
33 namespace octave
34 {
35  class pid_equal
36  {
37  public:
38 
39  pid_equal (pid_t v) : val (v) { }
40 
41  bool operator () (const child& oc) const { return oc.pid == val; }
42 
43  private:
44 
45  pid_t val;
46  };
47 
48  void child_list::remove (pid_t pid)
49  {
50  m_list.remove_if (pid_equal (pid));
51  }
52 
53  void child_list::child_list::insert (pid_t pid, child::child_event_handler f)
54  {
55  m_list.append (child (pid, f));
56  }
57 
58  void child_list::reap (void)
59  {
60  // Mark the record for PID invalid.
61 
62  for (auto& oc : m_list)
63  {
64  // The call to the child::child_event_handler might
65  // invalidate the iterator (for example, by calling
66  // child_list::remove), so we increment the iterator
67  // here.
68 
69  if (oc.have_status)
70  {
71  oc.have_status = 0;
72 
73  child::child_event_handler f = oc.handler;
74 
75  if (f && f (oc.pid, oc.status))
76  oc.pid = -1;
77  }
78  }
79 
80  // ??
81  remove (-1);
82  }
83 
84  // Wait on our children and record any changes in their status.
85 
86  bool child_list::wait (void)
87  {
88  bool retval = false;
89 
90  for (auto& oc : m_list)
91  {
92  pid_t pid = oc.pid;
93 
94  if (pid > 0)
95  {
96  int status;
97 
98  if (sys::waitpid (pid, &status, sys::wnohang ()) > 0)
99  {
100  oc.have_status = 1;
101 
102  oc.status = status;
103 
104  retval = true;
105 
106  break;
107  }
108  }
109  }
110 
111  return retval;
112  }
113 }
void reap(void)
Definition: child-list.cc:58
base_list< child > m_list
Definition: child-list.h:91
bool wait(void)
Definition: child-list.cc:86
void remove(pid_t pid)
Definition: child-list.cc:48
bool(* child_event_handler)(pid_t, int)
Definition: child-list.h:49
pid_equal(pid_t v)
Definition: child-list.cc:39
bool operator()(const child &oc) const
Definition: child-list.cc:41
pid_t waitpid(pid_t pid, int *status, int options)
int wnohang(void)
static double f(double k, double l_nu, double c_pm)
Definition: randpoisson.cc:118
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811