[9451c2]: src / select.cc Maximize Restore History

Download this file

select.cc    217 lines (205 with data), 8.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (C) 2007-2013 Olaf Till <i7tiol@t-online.de>
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <octave/oct.h>
#include <octave/oct-stream.h>
#ifdef POSIX
#include <sys/select.h>
#else
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#include <errno.h>
#include <map>
DEFUN_DLD (select, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {[@var{n}, @var{ridx}, @var{widx}, @var{eidx}] =} select (@var{read_fids}, @var{write_fids}, @var{except_fids}, @var{timeout}[, @var{nfds}])\n\
Calls Unix @code{select}, see the respective manual.\n\
The following interface was chosen:\n\
@var{read_fids}, @var{write_fids}, @var{except_fids}: vectors of stream-ids.\n\
@var{timeout}: seconds, negative for infinite.\n\
@var{nfds}: optional, default is Unix' FD_SETSIZE (platform specific).\n\
An error is returned if nfds or a filedescriptor belonging to a stream-id\n\
plus one exceeds FD_SETSIZE.\n\
Return values are:\n\
@var{n}: number of ready streams.\n\
@var{ridx}, @var{widx}, @var{eidx}: index vectors of ready streams in\n\
@var{read_fids}, @var{write_fids}, and @var{except_fids}, respectively.\n\
@end deftypefn")
{
/* This routine assumes that Octaves stream-id and the
corresponding filedescriptor of the system are the same
number. This should be the case in Octave >= 3.0.0. */
octave_value_list retval;
int nargin = args.length ();
int i, fd, fid, nfds, n, act, argc;
double argtout, *fvec;
timeval tout;
timeval *timeout = &tout;
ColumnVector read_fids, write_fids, except_fids;
if (nargin == 4)
nfds = FD_SETSIZE;
else if (nargin == 5) {
if (! args(4).is_real_scalar ()) {
error ("select: 'nfds' must be a real scalar.\n");
return retval;
}
nfds = args(4).int_value ();
if (nfds <= 0) {
error ("select: 'nfds' should be greater than zero.\n");
return retval;
}
if (nfds > FD_SETSIZE) {
error ("select: 'nfds' exceeds systems maximum given by FD_SETSIZE.\n");
return retval;
}
}
else {
error ("select: four or five arguments required.\n");
return retval;
}
if (! args(3).is_real_scalar ()) {
error ("select: 'timeout' must be a real scalar.\n");
return retval;
}
if ((argtout = args(3).double_value ()) < 0)
timeout = NULL;
else {
double ipart, fpart;
fpart = modf (argtout, &ipart);
tout.tv_sec = lrint (ipart);
tout.tv_usec = lrint (fpart * 1000);
}
for (argc = 0; argc < 3; argc++) {
if (! args(argc).is_empty ()) {
if (! args(argc).is_real_type ()) {
error ("select: first three arguments must be real vectors.\n");
return retval;
}
if (args(argc).rows () != 1 &&
args(argc).columns () != 1) {
error ("select: first three arguments must be real vectors.\n");
return retval;
}
switch (argc) {
case 0: read_fids = ColumnVector
(args(argc).vector_value ()); break;
case 1: write_fids = ColumnVector
(args(argc).vector_value ()); break;
case 2: except_fids = ColumnVector
(args(argc).vector_value ()); break;
}
}
}
fd_set rfds, wfds, efds;
FD_ZERO (&rfds); FD_ZERO (&wfds); FD_ZERO (&efds);
for (i = 0; i < read_fids.length (); i++) {
fid = lrint (read_fids(i));
fd = octave_stream_list::lookup (fid, "select") .
file_number ();
if (error_state || fd < 0) {
error ("select: invalid file-id");
return retval;
}
if (fid >= FD_SETSIZE) {
error ("select: filedescriptor >= FD_SETSIZE");
return retval;
}
FD_SET (fid, &rfds);
}
for (i = 0; i < write_fids.length (); i++) {
fid = lrint (write_fids(i));
fd = octave_stream_list::lookup (fid, "select") .
file_number ();
if (error_state || fd < 0) {
error ("select: invalid file-id");
return retval;
}
if (fid >= FD_SETSIZE) {
error ("select: filedescriptor >= FD_SETSIZE");
return retval;
}
FD_SET (fid, &wfds);
}
for (i = 0; i < except_fids.length (); i++) {
fid = lrint (except_fids(i));
fd = octave_stream_list::lookup (fid, "select") .
file_number ();
if (error_state || fd < 0) {
error ("select: invalid file-id");
return retval;
}
if (fid >= FD_SETSIZE) {
error ("select: filedescriptor >= FD_SETSIZE");
return retval;
}
FD_SET (fid, &efds);
}
if ((n = select (nfds, &rfds, &wfds, &efds, timeout)) == -1) {
std::string err;
switch (errno) {
case EBADF:
err = "EBADF";
break;
case EINTR:
err = "EINTR";
break;
case EINVAL:
err = "EINVAL";
break;
default:
err = "unknown error";
}
error ("select: unix select returned error: %s\n",
err.c_str ());
return retval;
}
if (nargout > 3) {
for (i = 0, act = 0; i < except_fids.length (); i++)
if (FD_ISSET (lrint (except_fids(i)), &efds)) act++;
RowVector eidx = RowVector (act);
for (i = 0, fvec = eidx.fortran_vec ();
i < except_fids.length (); i++)
if (FD_ISSET (lrint (except_fids(i)), &efds)) {
*fvec = double (i + 1);
fvec++;
}
retval(3) = eidx;
}
if (nargout > 2) {
for (i = 0, act = 0; i < write_fids.length (); i++)
if (FD_ISSET (lrint (write_fids(i)), &wfds)) act++;
RowVector widx = RowVector (act);
for (i = 0, fvec = widx.fortran_vec ();
i < write_fids.length (); i++)
if (FD_ISSET (lrint (write_fids(i)), &wfds)) {
*fvec = double (i + 1);
fvec++;
}
retval(2) = widx;
}
if (nargout > 1) {
for (i = 0, act = 0; i < read_fids.length (); i++)
if (FD_ISSET (lrint (read_fids(i)), &rfds)) act++;
RowVector ridx = RowVector (act);
for (i = 0, fvec = ridx.fortran_vec ();
i < read_fids.length (); i++)
if (FD_ISSET (lrint (read_fids(i)), &rfds)) {
*fvec = double (i + 1);
fvec++;
}
retval(1) = ridx;
}
retval(0) = n;
return retval;
}