diff --git a/include/coap3/coap_net.h b/include/coap3/coap_net.h index b8fcaca3..8dbffc12 100644 --- a/include/coap3/coap_net.h +++ b/include/coap3/coap_net.h @@ -696,15 +696,6 @@ int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, */ int coap_io_pending(coap_context_t *context); -/**@}*/ - -/** - * @ingroup internal_api - * @defgroup app_io_internal Application I/O Handling - * Internal API for Application Input / Output checking - * @{ - */ - /** * Iterates through all the coap_socket_t structures embedded in endpoints or * sessions associated with the @p ctx to determine which are wanting any @@ -894,8 +885,7 @@ coap_write(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, - coap_tick_t now - ) { + coap_tick_t now) { return coap_io_prepare_io(ctx, sockets, max_sockets, num_sockets, now); } @@ -910,8 +900,7 @@ coap_write(coap_context_t *ctx, * @param now Current time */ COAP_STATIC_INLINE COAP_DEPRECATED void -coap_read(coap_context_t *ctx, coap_tick_t now - ) { +coap_read(coap_context_t *ctx, coap_tick_t now) { coap_io_do_io(ctx, now); } diff --git a/include/coap3/coap_net_internal.h b/include/coap3/coap_net_internal.h index 00448de7..e49aebb0 100644 --- a/include/coap3/coap_net_internal.h +++ b/include/coap3/coap_net_internal.h @@ -221,6 +221,17 @@ int coap_insert_node(coap_queue_t **queue, coap_queue_t *node); */ int coap_delete_node(coap_queue_t *node); +/** + * Destroys specified @p node. + * + * Note: Needs to be called in a locked state if node->session is set. + * + * @param node Node entry to remove. + * + * @return @c 1 node deleted from queue, @c 0 failure. + */ +int coap_delete_node_locked(coap_queue_t *node); + /** * Removes all items from given @p queue and frees the allocated storage. * @@ -435,8 +446,204 @@ coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu); */ int coap_client_delay_first(coap_session_t *session); +/** + * CoAP stack context must be released with coap_free_context_locked(). This + * function clears all entries from the receive queue and send queue and deletes the + * resources that have been registered with @p context, and frees the attached + * endpoints. + * + * Note: This function must be called in the locked state. + * + * @param context The current coap_context_t object to free off. + */ +void coap_free_context_locked(coap_context_t *context); + /** @} */ +/** + * @ingroup internal_api + * @defgroup app_io_internal Application I/O Handling + * Internal API for Application Input / Output checking + * @{ + */ + +/** + * Processes any outstanding read, write, accept or connect I/O as indicated + * in the coap_socket_t structures (COAP_SOCKET_CAN_xxx set) embedded in + * endpoints or sessions associated with @p ctx. + * + * Note: If epoll support is compiled into libcoap, coap_io_do_epoll_locked() must + * be used instead of coap_io_do_io_locked(). + * + * Note: This function must be called in the locked state. + * + * @param ctx The CoAP context + * @param now Current time + */ +void coap_io_do_io_locked(coap_context_t *ctx, coap_tick_t now); + +/** + * Process all the epoll events + * + * Note: If epoll support is compiled into libcoap, coap_io_do_epoll_locked() must + * be used instead of coap_io_do_io_locked(). + * + * Note: This function must be called in the locked state. + * + * @param ctx The current CoAP context. + * @param events The list of events returned from an epoll_wait() call. + * @param nevents The number of events. + * + */ +void coap_io_do_epoll_locked(coap_context_t *ctx, struct epoll_event *events, + size_t nevents); + +/** + * Check to see if there is any i/o pending for the @p context. + * + * This includes Observe active (client) and partial large block transfers. + * + * Note: This function must be called in the locked state. + * + * coap_io_process() is called internally to try to send outstanding + * data as well as process any packets just received. + * + * @param context The CoAP context. + * + * @return @c 1 I/O still pending, @c 0 no I/O pending. + */ +int coap_io_pending_locked(coap_context_t *context); + +/** + * Any now timed out delayed packet is transmitted, along with any packets + * associated with requested observable response. + * + * In addition, it returns when the next expected I/O is expected to take place + * (e.g. a packet retransmit). + * + * Note: If epoll support is compiled into libcoap, coap_io_prepare_epoll_locked() + * must be used instead of coap_io_prepare_io_locked(). + * + * Note: This function must be called in the locked state. + * + * @param ctx The CoAP context + * @param now Current time. + * + * @return timeout Maxmimum number of milliseconds that can be used by a + * epoll_wait() to wait for network events or 0 if wait should be + * forever. + */ +unsigned int coap_io_prepare_epoll_locked(coap_context_t *ctx, coap_tick_t now); + +/** + * Iterates through all the coap_socket_t structures embedded in endpoints or + * sessions associated with the @p ctx to determine which are wanting any + * read, write, accept or connect I/O (COAP_SOCKET_WANT_xxx is set). If set, + * the coap_socket_t is added to the @p sockets. + * + * Any now timed out delayed packet is transmitted, along with any packets + * associated with requested observable response. + * + * In addition, it returns when the next expected I/O is expected to take place + * (e.g. a packet retransmit). + * + * Prior to calling coap_io_do_io_locked(), the @p sockets must be tested to see + * if any of the COAP_SOCKET_WANT_xxx have the appropriate information and if + * so, COAP_SOCKET_CAN_xxx is set. This typically will be done after using a + * select() call. + * + * Note: If epoll support is compiled into libcoap, coap_io_prepare_epoll_locked() + * must be used instead of coap_io_prepare_io_locked(). + * + * Note: This function must be called in the locked state. + * + * @param ctx The CoAP context + * @param sockets Array of socket descriptors, filled on output + * @param max_sockets Size of socket array. + * @param num_sockets Pointer to the number of valid entries in the socket + * arrays on output. + * @param now Current time. + * + * @return timeout Maxmimum number of milliseconds that can be used by a + * select() to wait for network events or 0 if wait should be + * forever. + */ +unsigned int coap_io_prepare_io_locked(coap_context_t *ctx, + coap_socket_t *sockets[], + unsigned int max_sockets, + unsigned int *num_sockets, + coap_tick_t now + ); + +/** + * The main I/O processing function. All pending network I/O is completed, + * and then optionally waits for the next input packet. + * + * This internally calls coap_io_prepare_io(), then select() for the appropriate + * sockets, updates COAP_SOCKET_CAN_xxx where appropriate and then calls + * coap_io_do_io() before returning with the time spent in the function. + * + * Alternatively, if libcoap is compiled with epoll support, this internally + * calls coap_io_prepare_epoll(), then epoll_wait() for waiting for any file + * descriptors that have (internally) been set up with epoll_ctl() and + * finally coap_io_do_epoll() before returning with the time spent in the + * function. + * + * Note: This function must be called in the locked state. + * + * @param ctx The CoAP context + * @param timeout_ms Minimum number of milliseconds to wait for new packets + * before returning after doing any processing. + * If COAP_IO_WAIT, the call will block until the next + * internal action (e.g. packet retransmit) if any, or block + * until the next packet is received whichever is the sooner + * and do the necessary processing. + * If COAP_IO_NO_WAIT, the function will return immediately + * after processing without waiting for any new input + * packets to arrive. + * + * @return Number of milliseconds spent in function or @c -1 if there was + * an error + */ +int coap_io_process_locked(coap_context_t *ctx, uint32_t timeout_ms); + +#if !defined(RIOT_VERSION) && !defined(WITH_CONTIKI) +/** + * The main message processing loop with additional fds for internal select. + * + * Note: This function must be called in the locked state. + * + * @param ctx The CoAP context + * @param timeout_ms Minimum number of milliseconds to wait for new packets + * before returning after doing any processing. + * If COAP_IO_WAIT, the call will block until the next + * internal action (e.g. packet retransmit) if any, or block + * until the next packet is received whichever is the sooner + * and do the necessary processing. + * If COAP_IO_NO_WAIT, the function will return immediately + * after processing without waiting for any new input + * packets to arrive. + * @param nfds The maximum FD set in readfds, writefds or exceptfds + * plus one, + * @param readfds Read FDs to additionally check for in internal select() + * or NULL if not required. + * @param writefds Write FDs to additionally check for in internal select() + * or NULL if not required. + * @param exceptfds Except FDs to additionally check for in internal select() + * or NULL if not required. + * + * + * @return Number of milliseconds spent in coap_io_process_with_fds, or @c -1 + * if there was an error. If defined, readfds, writefds, exceptfds + * are updated as returned by the internal select() call. + */ +int coap_io_process_with_fds_locked(coap_context_t *ctx, uint32_t timeout_ms, + int nfds, fd_set *readfds, fd_set *writefds, + fd_set *exceptfds); +#endif /* ! RIOT_VERSION && ! WITH_CONTIKI */ + +/**@}*/ + extern int coap_started; #endif /* COAP_NET_INTERNAL_H_ */ diff --git a/include/coap3/coap_threadsafe_internal.h b/include/coap3/coap_threadsafe_internal.h index 8f176267..92b880a0 100644 --- a/include/coap3/coap_threadsafe_internal.h +++ b/include/coap3/coap_threadsafe_internal.h @@ -43,18 +43,8 @@ #define coap_find_async(s,t) coap_find_async_locked(s,t) #define coap_delete_oscore_recipient(s,r) coap_delete_oscore_recipient_locked(s,r) #define coap_delete_resource(c,r) coap_delete_resource_locked(c,r) -#define coap_free_context(c) coap_free_context_locked(c) #define coap_free_endpoint(e) coap_free_endpoint_locked(e) #define coap_get_resource_from_uri_path(c,u) coap_get_resource_from_uri_path_locked(c,u) -#define coap_io_do_epoll(c,e,n) coap_io_do_epoll_locked(c,e,n) -#define coap_io_do_io(c,n) coap_io_do_io_locked(c,n) -#define coap_io_pending(c) coap_io_pending_locked(c) -#define coap_io_prepare_epoll(c,n) coap_io_prepare_epoll_locked(c,n) -#define coap_io_prepare_io(c,s,m,n,t) coap_io_prepare_io_locked(c,s,m,n,t) -#define coap_io_process(s,t) coap_io_process_locked(s,t) -#ifdef HAVE_SYS_SELECT_H -#define coap_io_process_with_fds(s,t,n,r,w,e) coap_io_process_with_fds_locked(s,t,n,r,w,e) -#endif /* HAVE_SYS_SELECT_H */ #define coap_join_mcast_group_intf(c,g,i) coap_join_mcast_group_intf_locked(c,g,i) #define coap_new_cache_entry(s,p,r,b,i) coap_new_cache_entry_locked(s,p,r,b,i) #define coap_new_client_session(c,l,s,p) coap_new_client_session_locked(c,l,s,p) @@ -142,20 +132,9 @@ int coap_delete_oscore_recipient_locked(coap_context_t *context coap_bin_const_t *recipient_id); int coap_delete_resource_locked(coap_context_t *context, coap_resource_t *resource); coap_async_t *coap_find_async_locked(coap_session_t *session, coap_bin_const_t token); -void coap_free_context_locked(coap_context_t *context); void coap_free_endpoint_locked(coap_endpoint_t *ep); coap_resource_t *coap_get_resource_from_uri_path_locked(coap_context_t *context, coap_str_const_t *uri_path); -void coap_io_do_epoll_locked(coap_context_t *ctx, struct epoll_event *events, - size_t nevents); -void coap_io_do_io_locked(coap_context_t *ctx, coap_tick_t now); -int coap_io_pending_locked(coap_context_t *context); -unsigned int coap_io_prepare_epoll_locked(coap_context_t *ctx, coap_tick_t now); -unsigned int coap_io_prepare_io_locked(coap_context_t *ctx, - coap_socket_t *sockets[], - unsigned int max_sockets, - unsigned int *num_sockets, - coap_tick_t now); int coap_join_mcast_group_intf_locked(coap_context_t *ctx, const char *group_name, const char *ifname); coap_subscription_t *coap_persist_observe_add_locked(coap_context_t *context, @@ -170,12 +149,6 @@ int coap_persist_startup_locked(coap_context_t *context, const char *obs_cnt_save_file, uint32_t save_freq); void coap_persist_stop_locked(coap_context_t *context); -int coap_io_process_locked(coap_context_t *ctx, uint32_t timeout_ms); -#ifdef HAVE_SYS_SELECT_H -int coap_io_process_with_fds_locked(coap_context_t *ctx, uint32_t timeout_ms, - int nfds, fd_set *readfds, fd_set *writefds, - fd_set *exceptfds); -#endif /* HAVE_SYS_SELECT_H */ coap_async_t *coap_register_async_locked(coap_session_t *session, const coap_pdu_t *request, coap_tick_t delay); size_t coap_session_max_pdu_size_locked(const coap_session_t *session); diff --git a/src/coap_io.c b/src/coap_io.c index 3edd1851..91b79643 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -1181,10 +1181,20 @@ error: unsigned int coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now) { + unsigned int ret; + + coap_lock_lock(ctx, return 0); + ret = coap_io_prepare_epoll_locked(ctx, now); + coap_lock_unlock(ctx); + return ret; +} + +unsigned int +coap_io_prepare_epoll_locked(coap_context_t *ctx, coap_tick_t now) { #ifndef COAP_EPOLL_SUPPORT (void)ctx; (void)now; - coap_log_emerg("coap_io_prepare_epoll() requires libcoap compiled for using epoll\n"); + coap_log_emerg("coap_io_prepare_epoll_locked() requires libcoap compiled for using epoll\n"); return 0; #else /* COAP_EPOLL_SUPPORT */ coap_socket_t *sockets[1]; @@ -1194,7 +1204,7 @@ coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now) { coap_lock_check_locked(ctx); /* Use the common logic */ - timeout = coap_io_prepare_io(ctx, sockets, max_sockets, &num_sockets, now); + timeout = coap_io_prepare_io_locked(ctx, sockets, max_sockets, &num_sockets, now); /* Save when the next expected I/O is to take place */ ctx->next_timeout = timeout ? now + timeout : 0; if (ctx->eptimerfd != -1) { @@ -1218,7 +1228,7 @@ coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now) { ret = timerfd_settime(ctx->eptimerfd, 0, &new_value, NULL); if (ret == -1) { coap_log_err("%s: timerfd_settime failed: %s (%d)\n", - "coap_io_prepare_epoll", + "coap_io_prepare_epoll_locked", coap_socket_strerror(), errno); } } @@ -1236,6 +1246,24 @@ coap_io_prepare_io(coap_context_t *ctx, unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now) { + unsigned int ret; + + coap_lock_lock(ctx, return 0); + ret = coap_io_prepare_io_locked(ctx, sockets, max_sockets, num_sockets, now); + coap_lock_unlock(ctx); + return ret; +} + +/* + * return 0 No i/o pending + * +ve millisecs to next i/o activity + */ +unsigned int +coap_io_prepare_io_locked(coap_context_t *ctx, + coap_socket_t *sockets[], + unsigned int max_sockets, + unsigned int *num_sockets, + coap_tick_t now) { coap_queue_t *nextpdu; coap_session_t *s, *rtmp; coap_tick_t timeout = 0; @@ -1486,13 +1514,36 @@ release_2: #if !defined(WITH_LWIP) && !defined(CONTIKI) && !defined(RIOT_VERSION) int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) { - return coap_io_process_with_fds(ctx, timeout_ms, 0, NULL, NULL, NULL); + int ret; + + coap_lock_lock(ctx, return 0); + ret = coap_io_process_locked(ctx, timeout_ms); + coap_lock_unlock(ctx); + return ret; +} + +int +coap_io_process_locked(coap_context_t *ctx, uint32_t timeout_ms) { + return coap_io_process_with_fds_locked(ctx, timeout_ms, 0, NULL, NULL, NULL); } int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, int enfds, fd_set *ereadfds, fd_set *ewritefds, fd_set *eexceptfds) { + int ret; + + coap_lock_lock(ctx, return 0); + ret = coap_io_process_with_fds_locked(ctx, timeout_ms, enfds, ereadfds, ewritefds, + eexceptfds); + coap_lock_unlock(ctx); + return ret; +} + +int +coap_io_process_with_fds_locked(coap_context_t *ctx, uint32_t timeout_ms, + int enfds, fd_set *ereadfds, fd_set *ewritefds, + fd_set *eexceptfds) { coap_fd_t nfds = 0; coap_tick_t before, now; unsigned int timeout; @@ -1507,9 +1558,9 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, #ifndef COAP_EPOLL_SUPPORT - timeout = coap_io_prepare_io(ctx, ctx->sockets, - (sizeof(ctx->sockets) / sizeof(ctx->sockets[0])), - &ctx->num_sockets, before); + timeout = coap_io_prepare_io_locked(ctx, ctx->sockets, + (sizeof(ctx->sockets) / sizeof(ctx->sockets[0])), + &ctx->num_sockets, before); if (ereadfds) { ctx->readfds = *ereadfds; @@ -1613,7 +1664,7 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, } coap_ticks(&now); - coap_io_do_io(ctx, now); + coap_io_do_io_locked(ctx, now); #else /* COAP_EPOLL_SUPPORT */ (void)ereadfds; @@ -1621,7 +1672,7 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, (void)eexceptfds; (void)enfds; - timeout = coap_io_prepare_epoll(ctx, before); + timeout = coap_io_prepare_epoll_locked(ctx, before); do { struct epoll_event events[COAP_MAX_EPOLL_EVENTS]; @@ -1633,7 +1684,7 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, etimeout = 0; } else if (timeout == 0 && timeout_ms == COAP_IO_WAIT) { /* - * Nothing found in coap_io_prepare_epoll() and COAP_IO_WAIT set, + * Nothing found in coap_io_prepare_epoll_locked() and COAP_IO_WAIT set, * so wait forever in epoll_wait(). */ etimeout = -1; @@ -1677,7 +1728,7 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, #endif /* COAP_THREAD_SAFE */ coap_lock_lock(ctx, return -1); - coap_io_do_epoll(ctx, events, nfds); + coap_io_do_epoll_locked(ctx, events, nfds); /* * reset to COAP_IO_NO_WAIT (which causes etimeout to become 0) @@ -1705,12 +1756,22 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, } #endif /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION*/ +int +coap_io_pending(coap_context_t *context) { + int ret; + + coap_lock_lock(context, return 0); + ret = coap_io_pending_locked(context); + coap_lock_unlock(context); + return ret; +} + /* * return 1 I/O pending * 0 No I/O pending */ int -coap_io_pending(coap_context_t *context) { +coap_io_pending_locked(coap_context_t *context) { coap_session_t *s, *rtmp; #if COAP_SERVER_SUPPORT coap_endpoint_t *ep; @@ -1719,7 +1780,7 @@ coap_io_pending(coap_context_t *context) { if (!context) return 0; coap_lock_check_locked(context); - if (coap_io_process(context, COAP_IO_NO_WAIT) < 0) + if (coap_io_process_locked(context, COAP_IO_NO_WAIT) < 0) return 0; if (context->sendqueue) diff --git a/src/coap_io_contiki.c b/src/coap_io_contiki.c index 894f8cff..c2137374 100644 --- a/src/coap_io_contiki.c +++ b/src/coap_io_contiki.c @@ -61,7 +61,7 @@ prepare_io(coap_context_t *ctx) { unsigned next_io; coap_ticks(&now); - next_io = coap_io_prepare_io(ctx, sockets, max_sockets, &num_sockets, now); + next_io = coap_io_prepare_io_locked(ctx, sockets, max_sockets, &num_sockets, now); if (next_io) { coap_update_io_timer(ctx, next_io); } @@ -84,7 +84,7 @@ PROCESS_THREAD(libcoap_io_process, ev, data) { coap_socket->flags |= COAP_SOCKET_CAN_READ; coap_ticks(&now); - coap_io_do_io(coap_socket->context, now); + coap_io_do_io_locked(coap_socket->context, now); } } if (ev == PROCESS_EVENT_POLL) { @@ -236,19 +236,19 @@ coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet) { } int -coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) { +coap_io_process_locked(coap_context_t *ctx, uint32_t timeout_ms) { coap_tick_t before, now; coap_lock_check_locked(ctx); if (timeout_ms != COAP_IO_NO_WAIT) { - coap_log_err("coap_io_process must be called with COAP_IO_NO_WAIT\n"); + coap_log_err("coap_io_process_locked() must be called with COAP_IO_NO_WAIT\n"); return -1; } coap_ticks(&before); PROCESS_CONTEXT_BEGIN(&libcoap_io_process); prepare_io(ctx); - coap_io_do_io(ctx, before); + coap_io_do_io_locked(ctx, before); PROCESS_CONTEXT_END(&libcoap_io_process); coap_ticks(&now); return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND); diff --git a/src/coap_io_lwip.c b/src/coap_io_lwip.c index 4a0fbebf..d232d7c6 100644 --- a/src/coap_io_lwip.c +++ b/src/coap_io_lwip.c @@ -57,7 +57,17 @@ coap_io_process_timeout(void *arg) { } int -coap_io_process(coap_context_t *context, uint32_t timeout_ms) { +coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) { + int ret; + + coap_lock_lock(ctx, return 0); + ret = coap_io_process_locked(ctx, timeout_ms); + coap_lock_unlock(ctx); + return ret; +} + +int +coap_io_process_locked(coap_context_t *context, uint32_t timeout_ms) { coap_tick_t before; coap_tick_t now; unsigned int num_sockets; @@ -65,7 +75,7 @@ coap_io_process(coap_context_t *context, uint32_t timeout_ms) { coap_lock_check_locked(context); coap_ticks(&before); - timeout = coap_io_prepare_io(context, NULL, 0, &num_sockets, before); + timeout = coap_io_prepare_io_locked(context, NULL, 0, &num_sockets, before); if (timeout == 0 || (timeout_ms != COAP_IO_WAIT && timeout_ms < timeout)) timeout = timeout_ms; diff --git a/src/coap_io_riot.c b/src/coap_io_riot.c index 8902f8c6..5a299255 100644 --- a/src/coap_io_riot.c +++ b/src/coap_io_riot.c @@ -30,6 +30,16 @@ int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) { + int ret; + + coap_lock_lock(ctx, return 0); + ret = coap_io_process_locked(ctx, timeout_ms); + coap_lock_unlock(ctx); + return ret; +} + +int +coap_io_process_locked(coap_context_t *ctx, uint32_t timeout_ms) { coap_tick_t before, now; uint32_t timeout; coap_socket_t *sockets[1]; @@ -42,7 +52,7 @@ coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) { coap_ticks(&before); /* Use the common logic */ - timeout = coap_io_prepare_io(ctx, sockets, max_sockets, &num_sockets, before); + timeout = coap_io_prepare_io_locked(ctx, sockets, max_sockets, &num_sockets, before); if (timeout_ms == COAP_IO_NO_WAIT) { timeout = 0; @@ -73,7 +83,7 @@ coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) { } coap_ticks(&now); - coap_io_do_io(ctx, now); + coap_io_do_io_locked(ctx, now); #if COAP_SERVER_SUPPORT coap_expire_cache_entries(ctx); diff --git a/src/coap_net.c b/src/coap_net.c index e8523118..01aff8af 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -202,6 +202,28 @@ coap_insert_node(coap_queue_t **queue, coap_queue_t *node) { int coap_delete_node(coap_queue_t *node) { + int ret; +#if COAP_THREAD_SAFE + coap_context_t *context; +#endif /* COAP_THREAD_SAFE */ + + if (!node) + return 0; + if (!node->session) + return coap_delete_node_locked(node); + +#if COAP_THREAD_SAFE + /* Keep copy as node will be going away */ + context = node->session->context; +#endif /* COAP_THREAD_SAFE */ + coap_lock_lock(context, return 0); + ret = coap_delete_node_locked(node); + coap_lock_unlock(context); + return ret; +} + +int +coap_delete_node_locked(coap_queue_t *node) { if (!node) return 0; @@ -226,7 +248,7 @@ coap_delete_all(coap_queue_t *queue) { return; coap_delete_all(queue->next); - coap_delete_node(queue); + coap_delete_node_locked(queue); } coap_queue_t * @@ -558,7 +580,7 @@ coap_new_context(const coap_address_t *listen_addr) { c->dtls_context = coap_dtls_new_context(c); if (!c->dtls_context) { coap_log_emerg("coap_init: no DTLS context available\n"); - coap_free_context(c); + coap_free_context_locked(c); return NULL; } } @@ -602,6 +624,19 @@ coap_get_app_data(const coap_context_t *ctx) { void coap_free_context(coap_context_t *context) { + if (!context) + return; + /* + * Note there is an immediate unlock to release any other 'context' waiters + * So that their coap_lock_lock() will fail as 'context' is realy no more. + */ + coap_lock_being_freed(context, return); + coap_free_context_locked(context); + /* No need to unlock as context is no longer there */ +} + +void +coap_free_context_locked(coap_context_t *context) { if (!context) return; @@ -1043,7 +1078,7 @@ coap_client_delay_first(coap_session_t *session) { */ coap_session_reference(session); while (session->doing_first != 0) { - int result = coap_io_process(session->context, 1000); + int result = coap_io_process_locked(session->context, 1000); if (result < 0) { session->doing_first = 0; @@ -1052,7 +1087,7 @@ coap_client_delay_first(coap_session_t *session) { return 0; } - /* coap_io_process() may have updated session state */ + /* coap_io_process_locked() may have updated session state */ if (session->state == COAP_SESSION_STATE_CSM && current_state != COAP_SESSION_STATE_CSM) { /* Update timeout and restart the clock for CSM timeout */ @@ -1742,7 +1777,7 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { if (node->is_mcast) { coap_session_connected(node->session); - coap_delete_node(node); + coap_delete_node_locked(node); return COAP_INVALID_MID; } if (bytes_written == COAP_PDU_DELAYED) { @@ -1777,7 +1812,8 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { * coap_session_connected() is called. * However, there is the possibility coap_wait_ack() may be called for * this node (queue) and re-added to context->sendqueue. - * coap_delete_node(node) called shortly will handle this and remove it. + * coap_delete_node_locked(node) called shortly will handle this and + * remove it. */ coap_session_connected(node->session); } @@ -1790,7 +1826,7 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { context->nack_handler(node->session, node->pdu, COAP_NACK_TOO_MANY_RETRIES, node->id)); } - coap_delete_node(node); + coap_delete_node_locked(node); return COAP_INVALID_MID; } @@ -1859,7 +1895,7 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now } session->delayqueue = q->next; session->partial_write = 0; - coap_delete_node(q); + coap_delete_node_locked(q); } } @@ -2126,10 +2162,17 @@ coap_accept_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, void coap_io_do_io(coap_context_t *ctx, coap_tick_t now) { + coap_lock_lock(ctx, return); + coap_io_do_io_locked(ctx, now); + coap_lock_unlock(ctx); +} + +void +coap_io_do_io_locked(coap_context_t *ctx, coap_tick_t now) { #ifdef COAP_EPOLL_SUPPORT (void)ctx; (void)now; - coap_log_emerg("coap_io_do_io() requires libcoap not compiled for using epoll\n"); + coap_log_emerg("coap_io_do_io_locked() requires libcoap not compiled for using epoll\n"); #else /* ! COAP_EPOLL_SUPPORT */ coap_session_t *s, *rtmp; @@ -2178,17 +2221,24 @@ coap_io_do_io(coap_context_t *ctx, coap_tick_t now) { #endif /* ! COAP_EPOLL_SUPPORT */ } +void +coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents) { + coap_lock_lock(ctx, return); + coap_io_do_epoll_locked(ctx, events, nevents); + coap_lock_unlock(ctx); +} + /* - * While this code in part replicates coap_io_do_io(), doing the functions + * While this code in part replicates coap_io_do_io_locked(), doing the functions * directly saves having to iterate through the endpoints / sessions. */ void -coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents) { +coap_io_do_epoll_locked(coap_context_t *ctx, struct epoll_event *events, size_t nevents) { #ifndef COAP_EPOLL_SUPPORT (void)ctx; (void)events; (void)nevents; - coap_log_emerg("coap_io_do_epoll() requires libcoap compiled for using epoll\n"); + coap_log_emerg("coap_io_do_epoll_locked() requires libcoap compiled for using epoll\n"); #else /* COAP_EPOLL_SUPPORT */ coap_tick_t now; size_t j; @@ -2283,7 +2333,7 @@ coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents } /* And update eptimerfd as to when to next trigger */ coap_ticks(&now); - coap_io_prepare_epoll(ctx, now); + coap_io_prepare_epoll_locked(ctx, now); #endif /* COAP_EPOLL_SUPPORT */ } @@ -2392,7 +2442,7 @@ coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_lock_callback(context, context->nack_handler(session, q->pdu, reason, q->id)); } - coap_delete_node(q); + coap_delete_node_locked(q); } if (!context->sendqueue) @@ -2411,7 +2461,7 @@ coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_lock_callback(context, context->nack_handler(session, q->pdu, reason, q->id)); } - coap_delete_node(q); + coap_delete_node_locked(q); q = p->next; } else { p = q; @@ -2445,7 +2495,7 @@ coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, /* Flush out any entries on session->delayqueue */ coap_session_connected(session); } - coap_delete_node(q); + coap_delete_node_locked(q); } else { p = &(q->next); } @@ -3374,7 +3424,7 @@ skip_handler: goto drop_it_no_debug; } if (!coap_pdu_encode_header(response, session->proto)) { - coap_delete_node(node); + coap_delete_node_locked(node); goto drop_it_no_debug; } @@ -3756,7 +3806,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, session->recipient_ctx->initial_state == 0) { coap_log_warn("OSCORE: PDU could not be decrypted\n"); } - coap_delete_node(sent); + coap_delete_node_locked(sent); return; } else { session->oscore_encryption = 1; @@ -4019,7 +4069,7 @@ cleanup: coap_handle_event(context, COAP_EVENT_BAD_PACKET, session); } } - coap_delete_node(sent); + coap_delete_node_locked(sent); #if COAP_OSCORE_SUPPORT coap_delete_pdu(dec_pdu); #endif /* COAP_OSCORE_SUPPORT */ diff --git a/src/coap_session.c b/src/coap_session.c index df7e8cf6..e057aa48 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -481,7 +481,7 @@ coap_session_mfree(coap_session_t *session) { while (queue) { if (queue->session == session) { - coap_delete_node(queue); + coap_delete_node_locked(queue); break; } queue = queue->next; @@ -522,7 +522,7 @@ coap_session_mfree(coap_session_t *session) { COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, q->id)); } - coap_delete_node(q); + coap_delete_node_locked(q); } LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) { LL_DELETE(session->lg_xmit, lq); @@ -806,7 +806,7 @@ coap_session_connected(coap_session_t *session) { } if (COAP_PROTO_NOT_RELIABLE(session->proto)) { if (q) - coap_delete_node(q); + coap_delete_node_locked(q); if (bytes_written < 0) break; } else if (q) { @@ -817,7 +817,7 @@ coap_session_connected(coap_session_t *session) { session->partial_write = (size_t)bytes_written; break; } else { - coap_delete_node(q); + coap_delete_node_locked(q); } } } @@ -897,7 +897,7 @@ coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) { session->context->nack_handler(session, q->pdu, reason, q->id)); sent_nack = 1; } - coap_delete_node(q); + coap_delete_node_locked(q); } } #if COAP_CLIENT_SUPPORT @@ -946,7 +946,7 @@ coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) { q->next = NULL; coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n", coap_session_str(session), q->id); - coap_delete_node(q); + coap_delete_node_locked(q); } #if COAP_CLIENT_SUPPORT diff --git a/src/coap_threadsafe.c b/src/coap_threadsafe.c index 7d65af8a..4c235d69 100644 --- a/src/coap_threadsafe.c +++ b/src/coap_threadsafe.c @@ -13,6 +13,16 @@ * @brief CoAP multithreading safe functions */ +/* + * Public API functions are being migrated across to just ahead of where + * the _locked() functions are for ease of maintenance, debugging and + * documentation. + * + * The internal equivalent of the public API function will get _locked appended + * to the function name, and all calls to this function within the libcoap + * library have the function updated with the _locked suffix. Appropriate + * entries for this function are removed from coap_threadsafe_internal.h. + */ #include "coap_config.h" #if COAP_THREAD_SAFE @@ -503,97 +513,6 @@ coap_delete_oscore_recipient(coap_context_t *context, return ret; } -void -coap_free_context(coap_context_t *context) { - if (!context) - return; - /* - * Note there is an immediate unlock to release any other 'context' waiters - * So that their coap_lock_lock() will fail as 'context' is realy no more. - */ - coap_lock_being_freed(context, return); - coap_free_context_locked(context); - /* No need to unlock as context is no longer there */ -} - -void -coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents) { - coap_lock_lock(ctx, return); - coap_io_do_epoll_locked(ctx, events, nevents); - coap_lock_unlock(ctx); -} - -void -coap_io_do_io(coap_context_t *ctx, coap_tick_t now) { - coap_lock_lock(ctx, return); - coap_io_do_io(ctx, now); - coap_lock_unlock(ctx); -} - -int -coap_io_pending(coap_context_t *context) { - int ret; - - coap_lock_lock(context, return 0); - ret = coap_io_pending_locked(context); - coap_lock_unlock(context); - return ret; -} - -unsigned int -coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now) { - unsigned int ret; - - coap_lock_lock(ctx, return 0); - ret = coap_io_prepare_epoll(ctx, now); - coap_lock_unlock(ctx); - return ret; -} - -/* - * return 0 No i/o pending - * +ve millisecs to next i/o activity - */ -unsigned int -coap_io_prepare_io(coap_context_t *ctx, - coap_socket_t *sockets[], - unsigned int max_sockets, - unsigned int *num_sockets, - coap_tick_t now) { - unsigned int ret; - coap_lock_lock(ctx, return 0); - ret = coap_io_prepare_io_locked(ctx, sockets, max_sockets, num_sockets, now); - coap_lock_unlock(ctx); - return ret; -} - -int -coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) { - int ret; - - coap_lock_lock(ctx, return 0); - ret = coap_io_process_locked(ctx, timeout_ms); - coap_lock_unlock(ctx); - return ret; -} - -#if !defined(WITH_LWIP) -#ifdef HAVE_SYS_SELECT_H -int -coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, - int enfds, fd_set *ereadfds, fd_set *ewritefds, - fd_set *eexceptfds) { - int ret; - - coap_lock_lock(ctx, return 0); - ret = coap_io_process_with_fds_locked(ctx, timeout_ms, enfds, ereadfds, ewritefds, - eexceptfds); - coap_lock_unlock(ctx); - return ret; -} -#endif /* HAVE_SYS_SELECT_H */ -#endif /* WITH_LWIP */ - uint16_t coap_new_message_id(coap_session_t *session) { uint16_t mid; diff --git a/tests/test_sendqueue.c b/tests/test_sendqueue.c index 3c4555b7..76f24a34 100644 --- a/tests/test_sendqueue.c +++ b/tests/test_sendqueue.c @@ -320,15 +320,12 @@ t_sendqueue_tests_create(void) { static int t_sendqueue_tests_remove(void) { size_t n; - /* As coap_delete_node() is not in the Public API, need to lock */ - coap_lock_lock(ctx, return 1); for (n = 0; n < sizeof(node)/sizeof(coap_queue_t *); n++) { if (node[n]) { coap_delete_node(node[n]); node[n] = NULL; } } - coap_lock_unlock(ctx); coap_free_context(ctx); ctx = NULL; return 0;