diff --git a/src/coap_io.c b/src/coap_io.c index d51febc6..5d55bd8e 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -1302,8 +1302,8 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, int enfds, fd_set *ereadfds, fd_set *ewritefds, fd_set *eexceptfds) { #if COAP_CONSTRAINED_STACK - static coap_mutex_t static_mutex = COAP_MUTEX_INITIALIZER; # ifndef COAP_EPOLL_SUPPORT + static coap_mutex_t static_mutex = COAP_MUTEX_INITIALIZER; static fd_set readfds, writefds, exceptfds; static coap_socket_t *sockets[64]; unsigned int num_sockets = 0; @@ -1324,13 +1324,14 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, unsigned int i; #endif /* ! COAP_EPOLL_SUPPORT */ + coap_ticks(&before); + +#ifndef COAP_EPOLL_SUPPORT + #if COAP_CONSTRAINED_STACK coap_mutex_lock(&static_mutex); #endif /* COAP_CONSTRAINED_STACK */ - coap_ticks(&before); - -#ifndef COAP_EPOLL_SUPPORT timeout = coap_io_prepare_io(ctx, sockets, (sizeof(sockets) / sizeof(sockets[0])), &num_sockets, before); @@ -1400,6 +1401,15 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, return -1; } } + if (ereadfds) { + *ereadfds = readfds; + } + if (ewritefds) { + *ewritefds = writefds; + } + if (eexceptfds) { + *eexceptfds = exceptfds; + } if (result > 0) { for (i = 0; i < num_sockets; i++) { @@ -1416,17 +1426,12 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, } } +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ + coap_ticks(&now); coap_io_do_io(ctx, now); - if (ereadfds) { - *ereadfds = readfds; - } - if (ewritefds) { - *ewritefds = writefds; - } - if (eexceptfds) { - *eexceptfds = exceptfds; - } #else /* COAP_EPOLL_SUPPORT */ (void)ereadfds; @@ -1489,10 +1494,6 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, coap_ticks(&now); #endif /* WITHOUT_ASYNC */ -#if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&static_mutex); -#endif /* COAP_CONSTRAINED_STACK */ - return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND); } #endif /* RIOT_VERSION */ diff --git a/src/net.c b/src/net.c index fdeee390..2917c1db 100644 --- a/src/net.c +++ b/src/net.c @@ -1611,7 +1611,13 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) if (n == len) { if (coap_pdu_parse_header(session->partial_pdu, session->proto) && coap_pdu_parse_opt(session->partial_pdu)) { +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&s_static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ coap_dispatch(ctx, session, session->partial_pdu); +#if COAP_CONSTRAINED_STACK + coap_mutex_lock(&s_static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ } coap_delete_pdu(session->partial_pdu); session->partial_pdu = NULL; @@ -1655,7 +1661,13 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) session->partial_read = hdr_size; if (size == 0) { if (coap_pdu_parse_header(session->partial_pdu, session->proto)) { +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&s_static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ coap_dispatch(ctx, session, session->partial_pdu); +#if COAP_CONSTRAINED_STACK + coap_mutex_lock(&s_static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ } coap_delete_pdu(session->partial_pdu); session->partial_pdu = NULL;