Skip to content

Commit

Permalink
Mutexes: Clean up / simplify use of mutexes
Browse files Browse the repository at this point in the history
Not all OS implementations properly support static initializations.
  • Loading branch information
mrdeep1 committed Jul 29, 2023
1 parent 3f8bb33 commit cd7b5de
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 59 deletions.
77 changes: 45 additions & 32 deletions include/coap3/coap_mutex_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,77 +29,90 @@
#include <pthread.h>

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 <mutex.h>

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 */

#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 <lwip/sys.h>
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 <zephyr/sys/mutex.h>

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_ */
12 changes: 6 additions & 6 deletions src/coap_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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)) {
Expand Down Expand Up @@ -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 */
}

Expand Down Expand Up @@ -1188,22 +1188,22 @@ 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];
#endif /* ! COAP_CONSTRAINED_STACK */
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];
Expand Down
8 changes: 4 additions & 4 deletions src/coap_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down
54 changes: 41 additions & 13 deletions src/coap_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
}

Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit cd7b5de

Please sign in to comment.