2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include <rte_cycles.h>
18 #include <rte_common.h>
23 static struct input *inputs[32];
25 static int max_input_fd;
27 int reg_input(struct input *in)
29 if (n_inputs == sizeof(inputs)/sizeof(inputs[0]))
32 for (int i = 0; i < n_inputs; ++i) {
36 inputs[n_inputs++] = in;
37 max_input_fd = RTE_MAX(in->fd, max_input_fd);
42 void unreg_input(struct input *in)
46 for (rm = 0; rm < n_inputs; ++rm) {
47 if (inputs[rm] == in) {
55 for (i = rm + 1; i < n_inputs; ++i) {
56 inputs[i - 1] = inputs[i];
61 for (i = 0; i < n_inputs; ++i) {
62 max_input_fd = RTE_MAX(inputs[i]->fd, max_input_fd);
66 static int tsc_diff_to_tv(uint64_t beg, uint64_t end, struct timeval *tv)
72 uint64_t diff = end - beg;
88 for (int i = 0; i < n_inputs; ++i) {
89 FD_SET(inputs[i]->fd, &in_fd);
92 ret = select(max_input_fd + 1, &in_fd, NULL, NULL, &tv);
95 for (int i = 0; i < n_inputs; ++i) {
96 if (FD_ISSET(inputs[i]->fd, &in_fd)) {
97 inputs[i]->proc_input(inputs[i]);
104 void input_proc_until(uint64_t deadline)
110 /* Keep checking for input until select() returned 0 (timeout
111 occurred before input was read) or current time has passed
112 the deadline (which occurs when time progresses past the
113 deadline between return of select() and the next
115 while (ret != 0 && tsc_diff_to_tv(rte_rdtsc(), deadline, &tv) == 0) {
118 for (int i = 0; i < n_inputs; ++i) {
119 FD_SET(inputs[i]->fd, &in_fd);
122 ret = select(max_input_fd + 1, &in_fd, NULL, NULL, &tv);
125 for (int i = 0; i < n_inputs; ++i) {
126 if (FD_ISSET(inputs[i]->fd, &in_fd)) {
127 inputs[i]->proc_input(inputs[i]);