GNU Octave  4.4.1
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-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "child-list.h"
28 #include "oct-syscalls.h"
29 
30 namespace octave
31 {
32  class pid_equal
33  {
34  public:
35 
36  pid_equal (pid_t v) : val (v) { }
37 
38  bool operator () (const child& oc) const { return oc.pid == val; }
39 
40  private:
41 
42  pid_t val;
43  };
44 
45  void child_list::remove (pid_t pid)
46  {
47  m_list.remove_if (pid_equal (pid));
48  }
49 
50  void child_list::child_list::insert (pid_t pid, child::child_event_handler f)
51  {
52  m_list.append (child (pid, f));
53  }
54 
55  void child_list::reap (void)
56  {
57  // Mark the record for PID invalid.
58 
59  for (auto& oc : m_list)
60  {
61  // The call to the child::child_event_handler might
62  // invalidate the iterator (for example, by calling
63  // child_list::remove), so we increment the iterator
64  // here.
65 
66  if (oc.have_status)
67  {
68  oc.have_status = 0;
69 
70  child::child_event_handler f = oc.handler;
71 
72  if (f && f (oc.pid, oc.status))
73  oc.pid = -1;
74  }
75  }
76 
77  // ??
78  remove (-1);
79  }
80 
81  // Wait on our children and record any changes in their status.
82 
83  bool child_list::wait (void)
84  {
85  bool retval = false;
86 
87  for (auto& oc : m_list)
88  {
89  pid_t pid = oc.pid;
90 
91  if (pid > 0)
92  {
93  int status;
94 
95  if (sys::waitpid (pid, &status, sys::wnohang ()) > 0)
96  {
97  oc.have_status = 1;
98 
99  oc.status = status;
100 
101  retval = true;
102 
103  break;
104  }
105  }
106  }
107 
108  return retval;
109  }
110 }
base_list< child > m_list
Definition: child-list.h:88
bool operator()(const child &oc) const
Definition: child-list.cc:38
bool(* child_event_handler)(pid_t, int)
Definition: child-list.h:46
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
void remove(pid_t pid)
Definition: child-list.cc:45
pid_t waitpid(pid_t pid, int *status, int options)
bool wait(void)
Definition: child-list.cc:83
octave_value retval
Definition: data.cc:6246
int wnohang(void)
pid_equal(pid_t v)
Definition: child-list.cc:36
void reap(void)
Definition: child-list.cc:55