-
Notifications
You must be signed in to change notification settings - Fork 217
/
os-impl-bsd-select.c
371 lines (335 loc) · 10.9 KB
/
os-impl-bsd-select.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* \file os-impl-bsd-select.c
* \author [email protected]
*
* Purpose: This file contains wrappers around the select() system call
*/
/****************************************************************************************
INCLUDE FILES
***************************************************************************************/
/*
* Inclusions Defined by OSAL layer.
*
* This must include whatever is required to get the prototypes of these functions:
*
* FD_SET/FD_CLR/FD_ISSET macros and fd_set typedef
* select()
* clock_gettime() - for computing select timeouts
*/
#include <string.h>
#include <errno.h>
#include <time.h>
#include "os-impl-select.h"
#include "os-shared-select.h"
#include "os-shared-idmap.h"
/****************************************************************************************
DEFINES
***************************************************************************************/
/***************************************************************************************
FUNCTION PROTOTYPES
**************************************************************************************/
/****************************************************************************************
GLOBAL DATA
***************************************************************************************/
/****************************************************************************************
LOCAL FUNCTIONS
***************************************************************************************/
/*----------------------------------------------------------------
* Function: OS_FdSet_ConvertIn_Impl
*
* Purpose: Local helper routine, not part of OSAL API.
*
* Convert an OS_FdSet (OSAL) structure into an fd_set (POSIX)
* which can then be passed to the POSIX select function.
*
* returns: Highest numbered file descriptor in the output fd_set
*-----------------------------------------------------------------*/
static int OS_FdSet_ConvertIn_Impl(fd_set *os_set, OS_FdSet *OSAL_set)
{
size_t offset;
size_t bit;
osal_index_t id;
uint8 objids;
int osfd;
int maxfd;
maxfd = -1;
for (offset = 0; offset < sizeof(OSAL_set->object_ids); ++offset)
{
objids = OSAL_set->object_ids[offset];
bit = 0;
while (objids != 0)
{
if (objids & 0x01)
{
id = OSAL_INDEX_C((offset * 8) + bit);
osfd = OS_impl_filehandle_table[id].fd;
if (osfd >= 0 && OS_impl_filehandle_table[id].selectable)
{
FD_SET(osfd, os_set);
if (osfd > maxfd)
{
maxfd = osfd;
}
}
}
++bit;
objids >>= 1;
}
}
return maxfd;
} /* end OS_FdSet_ConvertIn_Impl */
/*----------------------------------------------------------------
* Function: OS_FdSet_ConvertOut_Impl
*
* Purpose: Local helper routine, not part of OSAL API.
*
* Convert a POSIX fd_set structure into an OSAL OS_FdSet
* which can then be returned back to the application.
*
* This actually un-sets any bits in the "Input" parameter
* which are also set in the "output" parameter.
*-----------------------------------------------------------------*/
static void OS_FdSet_ConvertOut_Impl(fd_set *output, OS_FdSet *Input)
{
size_t offset;
size_t bit;
osal_index_t id;
uint8 objids;
int osfd;
for (offset = 0; offset < sizeof(Input->object_ids); ++offset)
{
objids = Input->object_ids[offset];
bit = 0;
while (objids != 0)
{
if (objids & 0x01)
{
id = OSAL_INDEX_C((offset * 8) + bit);
osfd = OS_impl_filehandle_table[id].fd;
if (osfd < 0 || !FD_ISSET(osfd, output))
{
Input->object_ids[offset] &= ~(1 << bit);
}
}
++bit;
objids >>= 1;
}
}
} /* end OS_FdSet_ConvertOut_Impl */
/*----------------------------------------------------------------
* Function: OS_DoSelect
*
* Purpose: Local helper routine, not part of OSAL API.
*
* Actual implementation of select() call
* Used by SelectSingle and SelectMultiple implementations (below)
*-----------------------------------------------------------------*/
static int32 OS_DoSelect(int maxfd, fd_set *rd_set, fd_set *wr_set, int32 msecs)
{
int os_status;
int32 return_code;
struct timeval tv;
struct timeval *tvptr;
struct timespec ts_now;
struct timespec ts_end;
if (msecs > 0)
{
clock_gettime(CLOCK_MONOTONIC, &ts_now);
ts_end.tv_sec = ts_now.tv_sec + (msecs / 1000);
ts_end.tv_nsec = ts_now.tv_nsec + (1000000 * (msecs % 1000));
if (ts_end.tv_nsec >= 1000000000)
{
++ts_end.tv_sec;
ts_end.tv_nsec -= 1000000000;
}
}
else
{
/* eliminates a false warning about possibly uninitialized use */
memset(&ts_end, 0, sizeof(ts_end));
}
do
{
if (msecs < 0)
{
tvptr = NULL;
}
else if (msecs == 0)
{
tvptr = &tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
}
else
{
tvptr = &tv;
clock_gettime(CLOCK_MONOTONIC, &ts_now);
/* note that the tv_sec and tv_usec/tv_nsec values are all signed longs, so OK to subtract */
tv.tv_sec = ts_end.tv_sec - ts_now.tv_sec;
tv.tv_usec = (ts_end.tv_nsec - ts_now.tv_nsec) / 1000;
if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
{
os_status = 0;
break;
}
if (tv.tv_usec < 0)
{
tv.tv_usec += 1000000;
++tv.tv_sec;
}
}
os_status = select(maxfd + 1, rd_set, wr_set, NULL, tvptr);
} while (os_status < 0 && errno == EINTR);
if (os_status < 0)
{
return_code = OS_ERROR;
}
else if (os_status == 0)
{
return_code = OS_ERROR_TIMEOUT;
}
else
{
return_code = OS_SUCCESS;
}
return return_code;
} /* end OS_DoSelect */
/****************************************************************************************
SELECT API
***************************************************************************************/
/*----------------------------------------------------------------
*
* Function: OS_SelectSingle_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SelectSingle_Impl(const OS_object_token_t *token, uint32 *SelectFlags, int32 msecs)
{
int32 return_code;
fd_set wr_set;
fd_set rd_set;
OS_impl_file_internal_record_t *impl;
impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);
/*
* If called on a stream_id which does not support this
* operation, return immediately and do not invoke the system call
*/
if (!impl->selectable)
{
return OS_ERR_OPERATION_NOT_SUPPORTED;
}
if (*SelectFlags != 0)
{
FD_ZERO(&wr_set);
FD_ZERO(&rd_set);
if (*SelectFlags & OS_STREAM_STATE_READABLE)
{
FD_SET(impl->fd, &rd_set);
}
if (*SelectFlags & OS_STREAM_STATE_WRITABLE)
{
FD_SET(impl->fd, &wr_set);
}
return_code = OS_DoSelect(impl->fd, &rd_set, &wr_set, msecs);
if (return_code == OS_SUCCESS)
{
if (!FD_ISSET(impl->fd, &rd_set))
{
*SelectFlags &= ~OS_STREAM_STATE_READABLE;
}
if (!FD_ISSET(impl->fd, &wr_set))
{
*SelectFlags &= ~OS_STREAM_STATE_WRITABLE;
}
}
else
{
*SelectFlags = 0;
}
}
else
{
/* Nothing to check for, return immediately. */
return_code = OS_SUCCESS;
}
return return_code;
} /* end OS_SelectSingle_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SelectMultiple_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SelectMultiple_Impl(OS_FdSet *ReadSet, OS_FdSet *WriteSet, int32 msecs)
{
fd_set wr_set;
fd_set rd_set;
int osfd;
int maxfd;
int32 return_code;
/*
* This return code will be used if the set(s) do not
* contain any file handles capable of select(). It
* will be overwritten with the real result of the
* select call, if selectable file handles were specified.
*/
return_code = OS_ERR_OPERATION_NOT_SUPPORTED;
FD_ZERO(&rd_set);
FD_ZERO(&wr_set);
maxfd = -1;
if (ReadSet != NULL)
{
osfd = OS_FdSet_ConvertIn_Impl(&rd_set, ReadSet);
if (osfd > maxfd)
{
maxfd = osfd;
}
}
if (WriteSet != NULL)
{
osfd = OS_FdSet_ConvertIn_Impl(&wr_set, WriteSet);
if (osfd > maxfd)
{
maxfd = osfd;
}
}
if (maxfd >= 0)
{
return_code = OS_DoSelect(maxfd, &rd_set, &wr_set, msecs);
}
if (return_code == OS_SUCCESS)
{
if (ReadSet != NULL)
{
OS_FdSet_ConvertOut_Impl(&rd_set, ReadSet);
}
if (WriteSet != NULL)
{
OS_FdSet_ConvertOut_Impl(&wr_set, WriteSet);
}
}
return return_code;
} /* end OS_SelectMultiple_Impl */