Mutexes: Clean up / simplify use of mutexes

Not all OS implementations properly support static initializations.
This commit is contained in:
Jon Shallow authored and Jon Shallow committed 2023-07-29 11:31:22 +01:00
1 parent 3f8bb33807
commit cd7b5de109
5 files changed
+100 -59

No files matched your search

+45 -32
View File
@@ -29,22 +29,24 @@
#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 */
@@ -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 <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_ */