GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
child-list.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://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 "lo-error.h"
29 #include "oct-syscalls.h"
30 #include "signal-wrappers.h"
31 #include "singleton-cleanup.h"
32 
33 namespace octave
34 {
35  child_list::child_list_rep *child_list::instance = 0;
36 
37  bool
39  {
40  bool retval = true;
41 
42  if (! instance)
43  {
44  instance = new child_list_rep ();
45 
46  if (instance)
48  }
49 
50  if (! instance)
51  (*current_liboctave_error_handler)
52  ("unable to create child list object!");
53 
54  return retval;
55  }
56 
57  void
59  {
60  if (instance_ok ())
61  instance->insert (pid, f);
62  }
63 
64  void
66  {
67  if (instance_ok ())
68  instance->reap ();
69  }
70 
71  bool
73  {
74  return (instance_ok ()) ? instance->wait () : false;
75  }
76 
77  class pid_equal
78  {
79  public:
80 
81  pid_equal (pid_t v) : val (v) { }
82 
83  bool operator () (const child& oc) const { return oc.pid == val; }
84 
85  private:
86 
87  pid_t val;
88  };
89 
90  void
91  child_list::remove (pid_t pid)
92  {
93  if (instance_ok ())
94  instance->remove_if (pid_equal (pid));
95  }
96 
97  void
99  {
100  append (child (pid, f));
101  }
102 
103  void
105  {
106  // Mark the record for PID invalid.
107 
108  for (iterator p = begin (); p != end (); p++)
109  {
110  // The call to the child::child_event_handler might
111  // invalidate the iterator (for example, by calling
112  // child_list::remove), so we increment the iterator
113  // here.
114 
115  child& oc = *p;
116 
117  if (oc.have_status)
118  {
119  oc.have_status = 0;
120 
122 
123  if (f && f (oc.pid, oc.status))
124  oc.pid = -1;
125  }
126  }
127 
128  remove_if (pid_equal (-1));
129  }
130 
131  // Wait on our children and record any changes in their status.
132 
133  bool
135  {
136  bool retval = false;
137 
138  for (iterator p = begin (); p != end (); p++)
139  {
140  child& oc = *p;
141 
142  pid_t pid = oc.pid;
143 
144  if (pid > 0)
145  {
146  int status;
147 
148  if (octave::sys::waitpid (pid, &status, octave::sys::wnohang ()) > 0)
149  {
150  oc.have_status = 1;
151 
152  oc.status = status;
153 
154  retval = true;
155 
156  break;
157  }
158  }
159  }
160 
161  return retval;
162  }
163 }
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
child_event_handler handler
Definition: child-list.h:75
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE * f
static void remove(pid_t pid)
Definition: child-list.cc:91
static child_list_rep * instance
Definition: child-list.h:120
sig_atomic_t have_status
Definition: child-list.h:78
std::list< child >::iterator iterator
Definition: base-list.h:40
pid_t waitpid(pid_t pid, int *status, int options)
void insert(pid_t pid, child::child_event_handler f)
Definition: child-list.cc:98
static void add(fptr f)
static bool wait(void)
Definition: child-list.cc:72
bool(* child_event_handler)(pid_t, int)
Definition: child-list.h:48
octave_value retval
Definition: data.cc:6294
static bool instance_ok(void)
Definition: child-list.cc:38
bool operator()(const child &oc) const
Definition: child-list.cc:83
static void insert(pid_t pid, child::child_event_handler f)
Definition: child-list.cc:58
void remove_if(P pred)
Definition: base-list.h:55
int wnohang(void)
p
Definition: lu.cc:138
static void cleanup_instance(void)
Definition: child-list.h:122
void append(const child &s)
Definition: base-list.h:110
pid_equal(pid_t v)
Definition: child-list.cc:81
static void reap(void)
Definition: child-list.cc:65