Skip to content

Commit

Permalink
- fixed formatting and static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
sp-martin committed May 31, 2024
1 parent e6ac425 commit 7cc5a2a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 42 deletions.
21 changes: 12 additions & 9 deletions api/oc_network_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,21 @@ oc_process_network_event(void)
OC_LIST_LOCAL(network_events);
oc_list_copy(network_events, g_network_events);
oc_list_init(g_network_events);
#ifdef OC_NETWORK_MONITOR
#ifdef OC_NETWORK_MONITOR
bool interface_up = g_interface_up;
g_interface_up = false;
g_interface_up = false;
bool interface_down = g_interface_down;
g_interface_down = false;
#endif /* OC_NETWORK_MONITOR */
oc_network_event_handler_mutex_unlock();

#ifdef OC_DYNAMIC_ALLOCATION
if(oc_list_length(network_events) >= OC_MAX_NUM_CONCURRENT_REQUESTS) {
// send a wake-up signal in case the queue might reach the limit for a device
#ifdef OC_DYNAMIC_ALLOCATION
if (oc_list_length(network_events) >= OC_MAX_NUM_CONCURRENT_REQUESTS) {
// send a wake-up signal in case the queue might reach the limit for a
// device
oc_connectivity_wakeup();
}
#endif /* OC_DYNAMIC_ALLOCATION */
#endif /* OC_DYNAMIC_ALLOCATION */

#ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT
oc_tcp_on_connect_event_t *event =
Expand Down Expand Up @@ -187,8 +188,9 @@ oc_network_drop_receive_events(const oc_endpoint_t *endpoint)
}
oc_network_event_handler_mutex_unlock();

#ifdef OC_DYNAMIC_ALLOCATION
if(oc_get_network_events_queue_length(endpoint->device) + dropped >= OC_MAX_NUM_CONCURRENT_REQUESTS) {
#ifdef OC_DYNAMIC_ALLOCATION
if (oc_get_network_events_queue_length(endpoint->device) + dropped >=
OC_MAX_NUM_CONCURRENT_REQUESTS) {
// send a wake-up signal in case the queue for the device was full
oc_connectivity_wakeup();
}
Expand Down Expand Up @@ -224,7 +226,8 @@ oc_get_network_events_queue_length(size_t device)
{
oc_network_event_handler_mutex_lock();
int message_count = 0;
for (oc_message_t *message = (oc_message_t *)oc_list_head(g_network_events); message != NULL; message = message->next) {
for (oc_message_t *message = (oc_message_t *)oc_list_head(g_network_events);
message != NULL; message = message->next) {
if (message->endpoint.device == device) {
++message_count;
}
Expand Down
2 changes: 1 addition & 1 deletion api/oc_network_events_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void oc_network_interface_event(oc_interface_event_t event);

/**
* @brief Returns the network event queue length for the device
*
*
* @param device valid device index
* @return number of events in the queue
*/
Expand Down
65 changes: 37 additions & 28 deletions port/linux/ipadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,9 @@ process_event(ip_context_t *dev, fd_set *rdfds, fd_set *wfds)
}

#ifdef OC_DYNAMIC_ALLOCATION
static int
fds_count(const fd_set *sourcefds) {
static int
fds_count(const fd_set *sourcefds)
{
int rfd_count = 0;
for (int i = 0; i < FD_SETSIZE; i++) { //// TODO: optimize
if (FD_ISSET(i, sourcefds)) {
Expand All @@ -904,9 +905,10 @@ fds_count(const fd_set *sourcefds) {
return rfd_count;
}

static int
pick_random_fd(const fd_set *sourcefds, int fd_count) {
int random_rfd = oc_random_value() % fd_count;
static int
pick_random_fd(const fd_set *sourcefds, int fd_count)
{
int random_rfd = (int)oc_random_value() % fd_count;
for (int i = 0; i < FD_SETSIZE; i++) { // TODO: optimize FD_SETSIZE
if (FD_ISSET(i, sourcefds)) {
if (--fd_count == random_rfd) {
Expand All @@ -917,17 +919,18 @@ pick_random_fd(const fd_set *sourcefds, int fd_count) {
return -1;
}

static int
remove_random_fds(fd_set *rdfds, int rfds_count, int remove_count) {

static int
remove_random_fds(fd_set *rdfds, int rfds_count, int remove_count)
{

int removed = 0;
while (removed < remove_count) {
int fd = pick_random_fd(rdfds, rfds_count);
if (fd < 0) {
break;
}
// remove file descriptor from the set
FD_CLR(fd, rdfds);
FD_CLR(fd, rdfds);
--rfds_count;
++removed;
}
Expand Down Expand Up @@ -959,19 +962,21 @@ process_events(ip_context_t *dev, fd_set *rdfds, fd_set *wfds, int fd_count)
}
fd_count--;
}

if (process_socket_signal_event(dev, rdfds)) {
fd_count--;
}

#ifdef OC_DYNAMIC_ALLOCATION
// check if network queue can consume all 'ready' events
int available_count = OC_MAX_NUM_CONCURRENT_REQUESTS - oc_get_network_events_queue_length(dev->device);
if (available_count < fd_count) {
int available_count = OC_MAX_NUM_CONCURRENT_REQUESTS -
oc_get_network_events_queue_length(dev->device);
if (available_count < fd_count) {
// get the number of read file descriptors
int rfds_count = fds_count(rdfds);
int removed = remove_random_fds(rdfds, rfds_count, rfds_count - available_count);
fd_count-= removed;
int rfds_count = fds_count(rdfds);
int removed =
remove_random_fds(rdfds, rfds_count, rfds_count - available_count);
fd_count -= removed;
}
#endif /* OC_DYNAMIC_ALLOCATION */

Expand Down Expand Up @@ -1000,7 +1005,9 @@ to_timeval(oc_clock_time_t ticks)
}
#endif /* OC_HAS_FEATURE_TCP_ASYNC_CONNECT */

static void add_control_flow_rfds(fd_set *output_set, const ip_context_t *dev){
static void
add_control_flow_rfds(fd_set *output_set, const ip_context_t *dev)
{
/* Monitor network interface changes on the platform from only the 0th
* logical device
*/
Expand All @@ -1015,7 +1022,7 @@ network_event_thread(void *data)
{
ip_context_t *dev = (ip_context_t *)data;
FD_ZERO(&dev->rfds);

udp_add_socks_to_rfd_set(dev);
add_control_flow_rfds(&dev->rfds, dev);
#ifdef OC_TCP
Expand All @@ -1042,14 +1049,15 @@ network_event_thread(void *data)
}
#endif /* OC_HAS_FEATURE_TCP_ASYNC_CONNECT */

#ifdef OC_DYNAMIC_ALLOCATION
if (oc_get_network_events_queue_length(dev->device) >= OC_MAX_NUM_CONCURRENT_REQUESTS) {
#ifdef OC_DYNAMIC_ALLOCATION
if (oc_get_network_events_queue_length(dev->device) >=
OC_MAX_NUM_CONCURRENT_REQUESTS) {
// the queue is full -> add only control flow rfds
FD_ZERO(&rdfds);
add_control_flow_rfds(&rdfds, dev);
#ifdef OC_TCP
#ifdef OC_TCP
tcp_add_controlflow_socks_to_rfd_set(&rdfds, dev);
#endif /* OC_TCP */
#endif /* OC_TCP */
}
#endif /* OC_DYNAMIC_ALLOCATION */
int n = select(FD_SETSIZE, &rdfds, wfds, NULL, timeout);
Expand Down Expand Up @@ -1572,16 +1580,17 @@ initialize_ip_context(ip_context_t *dev, size_t device,
return true;
}

static void signal_event_thread(ip_context_t *dev)
static void
signal_event_thread(ip_context_t *dev)
{
do {
if (write(dev->shutdown_pipe[1], "\n", 1) < 0) {
if (errno == EINTR) {
continue;
}
OC_WRN("cannot wakeup network thread (error: %d)", (int)errno);
if (write(dev->shutdown_pipe[1], "\n", 1) < 0) {
if (errno == EINTR) {
continue;
}
break;
OC_WRN("cannot wakeup network thread (error: %d)", (int)errno);
}
break;
} while (true);
}

Expand Down
4 changes: 2 additions & 2 deletions port/linux/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ tcp_add_socks_to_rfd_set(ip_context_t *dev)
#endif /* OC_IPV4 */
}

void
void
tcp_add_controlflow_socks_to_rfd_set(fd_set *rfd_set, const ip_context_t *dev)
{
FD_SET(dev->tcp.connect_pipe[0], rfd_set);
FD_SET(dev->tcp.connect_pipe[0], rfd_set);
}

static adapter_receive_state_t
Expand Down
3 changes: 2 additions & 1 deletion port/linux/tcpadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ void tcp_add_socks_to_rfd_set(ip_context_t *dev);
*
* @param dev the device network context (cannot be NULL)
*/
void tcp_add_controlflow_socks_to_rfd_set(fd_set *rfd_set, const ip_context_t *dev);
void tcp_add_controlflow_socks_to_rfd_set(fd_set *rfd_set,
const ip_context_t *dev);

/**
* @brief Handle data available on the signal pipe (dev->connect_pipe).
Expand Down
2 changes: 1 addition & 1 deletion port/oc_connectivity_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void oc_connectivity_shutdown(size_t device);
*/
int oc_send_buffer2(oc_message_t *message, bool queue);

#ifdef OC_DYNAMIC_ALLOCATION
#ifdef OC_DYNAMIC_ALLOCATION
/**
* @brief send a signal to start consuming network events
*/
Expand Down

0 comments on commit 7cc5a2a

Please sign in to comment.