diff --git a/include/coap3/coap_mutex_internal.h b/include/coap3/coap_mutex_internal.h index 5dad5a2de4..f14d2fdc41 100644 --- a/include/coap3/coap_mutex_internal.h +++ b/include/coap3/coap_mutex_internal.h @@ -29,22 +29,24 @@ #include typedef pthread_mutex_t coap_mutex_t; -#define COAP_MUTEX_DEFINE(_name) \ - static coap_mutex_t _name = PTHREAD_MUTEX_INITIALIZER -#define coap_mutex_lock(a) pthread_mutex_lock(a) + +#define coap_mutex_init(a) pthread_mutex_init(a, NULL) +#define coap_mutex_destroy(a) pthread_mutex_destroy(a) +#define coap_mutex_lock(a) pthread_mutex_lock(a) #define coap_mutex_trylock(a) pthread_mutex_trylock(a) -#define coap_mutex_unlock(a) pthread_mutex_unlock(a) +#define coap_mutex_unlock(a) pthread_mutex_unlock(a) #elif defined(RIOT_VERSION) /* use RIOT's mutex API */ #include typedef mutex_t coap_mutex_t; -#define COAP_MUTEX_DEFINE(_name) \ - static coap_mutex_t _name = MUTEX_INIT -#define coap_mutex_lock(a) mutex_lock(a) + +#define coap_mutex_init(a) mutex_init(a) +#define coap_mutex_destroy(a) +#define coap_mutex_lock(a) mutex_lock(a) #define coap_mutex_trylock(a) mutex_trylock(a) -#define coap_mutex_unlock(a) mutex_unlock(a) +#define coap_mutex_unlock(a) mutex_unlock(a) #elif defined(WITH_LWIP) /* Use LwIP's mutex API */ @@ -52,54 +54,65 @@ typedef mutex_t coap_mutex_t; #if NO_SYS /* Single threaded, no-op'd in lwip/sys.h */ typedef int coap_mutex_t; -#define COAP_MUTEX_DEFINE(_name) \ - static coap_mutex_t _name -#define coap_mutex_lock(a) *(a) = 1 + +#define coap_mutex_init(a) *(a) = 0 +#define coap_mutex_destroy(a) *(a) = 0 +#define coap_mutex_lock(a) *(a) = 1 #define coap_mutex_trylock(a) *(a) = 1 -#define coap_mutex_unlock(a) *(a) = 0 +#define coap_mutex_unlock(a) *(a) = 0 + #else /* !NO SYS */ #include -typedef sys_mutex_t *coap_mutex_t; -#define COAP_MUTEX_DEFINE(_name) \ - static coap_mutex_t _name -#define TOKENPASTE(x, y) x ## y -#define TOKENPASTE2(x, y) TOKENPASTE(x, y) -#define COAP_MUTEX_INITIALIZER (&TOKENPASTE2(coapMutexAt, __LINE__)) -#define coap_mutex_lock(a) sys_mutex_lock(*a) -#define coap_mutex_unlock(a) sys_mutex_unlock(*a) +typedef sys_mutex_t coap_mutex_t; + +#define coap_mutex_init(a) sys_mutex_new(a) +#define coap_mutex_destroy(a) sys_mutex_set_invalid(a) +#define coap_mutex_lock(a) sys_mutex_lock(a) +#define coap_mutex_trylock(a) sys_mutex_lock(a) +#define coap_mutex_unlock(a) sys_mutex_unlock(a) #endif /* !NO SYS */ #elif defined(WITH_CONTIKI) /* Contiki does not have a mutex API, used as single thread */ typedef int coap_mutex_t; -#define COAP_MUTEX_DEFINE(_name) \ - static coap_mutex_t _name -#define coap_mutex_lock(a) *(a) = 1 + +#define coap_mutex_init(a) *(a) = 0 +#define coap_mutex_destroy(a) *(a) = 0 +#define coap_mutex_lock(a) *(a) = 1 #define coap_mutex_trylock(a) *(a) = 1 -#define coap_mutex_unlock(a) *(a) = 0 +#define coap_mutex_unlock(a) *(a) = 0 #elif defined(__ZEPHYR__) #include typedef struct k_mutex coap_mutex_t; -#define COAP_MUTEX_DEFINE(_name) \ - static SYS_MUTEX_DEFINE(_name) -#define coap_mutex_lock(a) sys_mutex_lock(a, K_FOREVER) + +#define coap_mutex_init(a) sys_mutex_init(a) +#define coap_mutex_destroy(a) +#define coap_mutex_lock(a) sys_mutex_lock(a, K_FOREVER) #define coap_mutex_trylock(a) sys_mutex_lock(a, K_NO_WAIT) -#define coap_mutex_unlock(a) sys_mutex_unlock(a) +#define coap_mutex_unlock(a) sys_mutex_unlock(a) #else /* !__ZEPYR__ && !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */ /* define stub mutex functions */ #warning "stub mutex functions" typedef int coap_mutex_t; -#define COAP_MUTEX_DEFINE(_name) \ - static coap_mutex_t _name -#define coap_mutex_lock(a) *(a) = 1 + +#define coap_mutex_init(a) *(a) = 0 +#define coap_mutex_destroy(a) *(a) = 0 +#define coap_mutex_lock(a) *(a) = 1 #define coap_mutex_trylock(a) *(a) = 1 -#define coap_mutex_unlock(a) *(a) = 0 +#define coap_mutex_unlock(a) *(a) = 0 #endif /* !WITH_CONTIKI && !WITH_LWIP && !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */ +extern coap_mutex_t m_show_pdu; +extern coap_mutex_t m_log_impl; +extern coap_mutex_t m_dtls_recv; +extern coap_mutex_t m_read_session; +extern coap_mutex_t m_read_endpoint; +extern coap_mutex_t m_persist_add; + #endif /* COAP_CONSTRAINED_STACK */ #endif /* COAP_MUTEX_INTERNAL_H_ */ diff --git a/src/coap_debug.c b/src/coap_debug.c index ec6cc02d06..d2856c060a 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -702,8 +702,8 @@ is_binary(int content_format) { void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { #if COAP_CONSTRAINED_STACK - COAP_MUTEX_DEFINE(static_show_pdu_mutex); /* Proxy-Uri: can be 1034 bytes long */ + /* buf and outbuf protected by mutex m_show_pdu */ static unsigned char buf[min(COAP_DEBUG_BUF_SIZE, 1035)]; static char outbuf[COAP_DEBUG_BUF_SIZE]; #else /* ! COAP_CONSTRAINED_STACK */ @@ -729,7 +729,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { return; #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&static_show_pdu_mutex); + coap_mutex_lock(&m_show_pdu); #endif /* COAP_CONSTRAINED_STACK */ if (!pdu->session || COAP_PROTO_NOT_RELIABLE(pdu->session->proto)) { @@ -1045,7 +1045,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { COAP_DO_SHOW_OUTPUT_LINE; #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&static_show_pdu_mutex); + coap_mutex_unlock(&m_show_pdu); #endif /* COAP_CONSTRAINED_STACK */ } @@ -1188,7 +1188,7 @@ coap_log_impl(coap_log_t level, const char *format, ...) { if (log_handler) { #if COAP_CONSTRAINED_STACK - COAP_MUTEX_DEFINE(static_log_mutex); + /* message protected by mutex m_log_impl */ static char message[COAP_DEBUG_BUF_SIZE]; #else /* ! COAP_CONSTRAINED_STACK */ char message[COAP_DEBUG_BUF_SIZE]; @@ -1196,14 +1196,14 @@ coap_log_impl(coap_log_t level, const char *format, ...) { va_list ap; va_start(ap, format); #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&static_log_mutex); + coap_mutex_lock(&m_log_impl); #endif /* COAP_CONSTRAINED_STACK */ vsnprintf(message, sizeof(message), format, ap); va_end(ap); log_handler(level, message); #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&static_log_mutex); + coap_mutex_unlock(&m_log_impl); #endif /* COAP_CONSTRAINED_STACK */ } else { char timebuf[32]; diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 47d2ef3db7..3bd6808516 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -2068,14 +2068,14 @@ coap_dtls_receive(coap_session_t *c_session, if (m_env->established) { #if COAP_CONSTRAINED_STACK - COAP_MUTEX_DEFINE(b_static_mutex); + /* pdu protected by mutex m_dtls_recv */ static uint8_t pdu[COAP_RXBUFFER_SIZE]; #else /* ! COAP_CONSTRAINED_STACK */ uint8_t pdu[COAP_RXBUFFER_SIZE]; #endif /* ! COAP_CONSTRAINED_STACK */ #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&b_static_mutex); + coap_mutex_lock(&m_dtls_recv); #endif /* COAP_CONSTRAINED_STACK */ if (c_session->state == COAP_SESSION_STATE_HANDSHAKE) { @@ -2088,7 +2088,7 @@ coap_dtls_receive(coap_session_t *c_session, if (ret > 0) { ret = coap_handle_dgram(c_session->context, c_session, pdu, (size_t)ret); #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&b_static_mutex); + coap_mutex_unlock(&m_dtls_recv); #endif /* COAP_CONSTRAINED_STACK */ goto finish; } @@ -2107,7 +2107,7 @@ coap_dtls_receive(coap_session_t *c_session, break; } #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&b_static_mutex); + coap_mutex_unlock(&m_dtls_recv); #endif /* COAP_CONSTRAINED_STACK */ ret = -1; } else { diff --git a/src/coap_net.c b/src/coap_net.c index c7b3caf65a..1c9b8413dc 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -1778,7 +1778,7 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now static void coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) { #if COAP_CONSTRAINED_STACK - COAP_MUTEX_DEFINE(s_static_mutex); + /* payload and packet protected by mutex m_read_session */ static unsigned char payload[COAP_RXBUFFER_SIZE]; static coap_packet_t s_packet; #else /* ! COAP_CONSTRAINED_STACK */ @@ -1788,7 +1788,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) coap_packet_t *packet = &s_packet; #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&s_static_mutex); + coap_mutex_lock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ assert(session->sock.flags & (COAP_SOCKET_CONNECTED | COAP_SOCKET_MULTICAST)); @@ -1830,7 +1830,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) pdu = coap_pdu_init(0, 0, 0, coap_session_max_pdu_rcv_size(session)); if (!pdu) { #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&s_static_mutex); + coap_mutex_unlock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ return; } @@ -1840,13 +1840,13 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) coap_log_warn("discard malformed PDU\n"); coap_delete_pdu(pdu); #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&s_static_mutex); + coap_mutex_unlock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ return; } #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&s_static_mutex); + coap_mutex_unlock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ coap_dispatch(ctx, session, pdu); coap_delete_pdu(pdu); @@ -1880,11 +1880,11 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) 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); + coap_mutex_unlock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ coap_dispatch(ctx, session, session->partial_pdu); #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&s_static_mutex); + coap_mutex_lock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ } coap_delete_pdu(session->partial_pdu); @@ -1932,11 +1932,11 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) if (size == 0) { if (coap_pdu_parse_header(session->partial_pdu, session->proto)) { #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&s_static_mutex); + coap_mutex_unlock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ coap_dispatch(ctx, session, session->partial_pdu); #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&s_static_mutex); + coap_mutex_lock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ } coap_delete_pdu(session->partial_pdu); @@ -1963,7 +1963,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) #endif /* !COAP_DISABLE_TCP */ } #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&s_static_mutex); + coap_mutex_unlock(&m_read_session); #endif /* COAP_CONSTRAINED_STACK */ } @@ -1973,7 +1973,7 @@ coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t n ssize_t bytes_read = -1; int result = -1; /* the value to be returned */ #if COAP_CONSTRAINED_STACK - COAP_MUTEX_DEFINE(e_static_mutex); + /* payload and e_packet protected by mutex m_read_endpoint */ static unsigned char payload[COAP_RXBUFFER_SIZE]; static coap_packet_t e_packet; #else /* ! COAP_CONSTRAINED_STACK */ @@ -1986,7 +1986,7 @@ coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t n assert(endpoint->sock.flags & COAP_SOCKET_BOUND); #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&e_static_mutex); + coap_mutex_lock(&m_read_endpoint); #endif /* COAP_CONSTRAINED_STACK */ /* Need to do this as there may be holes in addr_info */ @@ -2010,7 +2010,7 @@ coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t n } } #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&e_static_mutex); + coap_mutex_unlock(&m_read_endpoint); #endif /* COAP_CONSTRAINED_STACK */ return result; } @@ -3980,6 +3980,15 @@ coap_check_async(coap_context_t *context, coap_tick_t now) { int coap_started = 0; +#if COAP_CONSTRAINED_STACK +coap_mutex_t m_show_pdu; +coap_mutex_t m_log_impl; +coap_mutex_t m_dtls_recv; +coap_mutex_t m_read_session; +coap_mutex_t m_read_endpoint; +coap_mutex_t m_persist_add; +#endif /* COAP_CONSTRAINED_STACK */ + void coap_startup(void) { coap_tick_t now; @@ -3990,6 +3999,16 @@ coap_startup(void) { if (coap_started) return; coap_started = 1; + +#if COAP_CONSTRAINED_STACK + coap_mutex_init(&m_show_pdu); + coap_mutex_init(&m_log_impl); + coap_mutex_init(&m_dtls_recv); + coap_mutex_init(&m_read_session); + coap_mutex_init(&m_read_endpoint); + coap_mutex_init(&m_persist_add); +#endif /* COAP_CONSTRAINED_STACK */ + #if defined(HAVE_WINSOCK2_H) WORD wVersionRequested = MAKEWORD(2, 2); WSADATA wsaData; @@ -4025,6 +4044,15 @@ coap_cleanup(void) { coap_stop_io_process(); #endif coap_dtls_shutdown(); + +#if COAP_CONSTRAINED_STACK + coap_mutex_destroy(&m_show_pdu); + coap_mutex_destroy(&m_log_impl); + coap_mutex_destroy(&m_dtls_recv); + coap_mutex_destroy(&m_read_session); + coap_mutex_destroy(&m_read_endpoint); + coap_mutex_destroy(&m_persist_add); +#endif /* COAP_CONSTRAINED_STACK */ } void diff --git a/src/coap_subscribe.c b/src/coap_subscribe.c index d69a7d9e81..0eb8db1abe 100644 --- a/src/coap_subscribe.c +++ b/src/coap_subscribe.c @@ -57,7 +57,7 @@ coap_persist_observe_add(coap_context_t *context, size_t data_len; coap_pdu_t *pdu = NULL; #if COAP_CONSTRAINED_STACK - COAP_MUTEX_DEFINE(e_static_mutex); + /* e_packet protected by mutex m_persist_add */ static coap_packet_t e_packet; #else /* ! COAP_CONSTRAINED_STACK */ coap_packet_t e_packet; @@ -88,7 +88,7 @@ coap_persist_observe_add(coap_context_t *context, } #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&e_static_mutex); + coap_mutex_lock(&m_persist_add); #endif /* COAP_CONSTRAINED_STACK */ /* Build up packet */ @@ -277,7 +277,7 @@ coap_persist_observe_add(coap_context_t *context, #endif /* ! COAP_OSCORE_SUPPORT */ coap_delete_pdu(pdu); #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&e_static_mutex); + coap_mutex_unlock(&m_persist_add); #endif /* COAP_CONSTRAINED_STACK */ return s; @@ -285,7 +285,7 @@ coap_persist_observe_add(coap_context_t *context, coap_log_warn("coap_persist_observe_add: discard malformed PDU\n"); fail: #if COAP_CONSTRAINED_STACK - coap_mutex_unlock(&e_static_mutex); + coap_mutex_unlock(&m_persist_add); #endif /* COAP_CONSTRAINED_STACK */ coap_delete_string(uri_path); coap_delete_pdu(pdu);