Skip to content

Commit

Permalink
coap_cleanup: Support multiple usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Jun 21, 2024
1 parent b1d305c commit 63c5077
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
8 changes: 6 additions & 2 deletions man/coap_init.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ until *coap_cleanup*() is called.

The *coap_cleanup*() function is used to cleanup / free any information set
up by the *coap_startup*() function and should be the last *coap_**() function
called. It is possible to call *coap_startup*() after *coap_cleanup*() to
re-initialize the libcoap logic.
called. The only safe function that can be called after *coap_cleanup*() is
*coap_startup*() to re-initialize the libcoap logic.

*NOTE:* Calling *coap_cleanup*() in one thread while continuing to use other
*coap_**() function calls in a different thread is not supported - even if they
are using a different coap_context_t.

*NOTE:* All other libcoap cleanups should called prior to *coap_cleanup*(), e.g.
*coap_free_context*(3).
Expand Down
4 changes: 3 additions & 1 deletion src/coap_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -4453,6 +4453,9 @@ coap_startup(void) {

void
coap_cleanup(void) {
if (!coap_started)
return;
coap_started = 0;
#if defined(HAVE_WINSOCK2_H)
WSACleanup();
#elif defined(WITH_CONTIKI)
Expand All @@ -4469,7 +4472,6 @@ coap_cleanup(void) {
coap_mutex_destroy(&m_persist_add);
#endif /* COAP_CONSTRAINED_STACK */
coap_debug_reset();
coap_started = 0;
}

void
Expand Down
8 changes: 4 additions & 4 deletions src/coap_threadsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ coap_lock_unlock_func(coap_lock_t *lock, const char *file, int line) {

int
coap_lock_lock_func(coap_lock_t *lock, int force, const char *file, int line) {
if (!force && lock->being_freed) {
/* context is going away */
if ((!force && lock->being_freed) || !coap_started) {
/* context is going away or libcoap not initialized with coap_startup() */
return 0;
}
if (coap_mutex_trylock(&lock->mutex)) {
Expand Down Expand Up @@ -88,8 +88,8 @@ coap_lock_unlock_func(coap_lock_t *lock) {

int
coap_lock_lock_func(coap_lock_t *lock, int force) {
if (!force && lock->being_freed) {
/* context is going away */
if ((!force && lock->being_freed) || !coap_started) {
/* context is going away or libcoap not initialized with coap_startup() */
return 0;
}
/*
Expand Down

0 comments on commit 63c5077

Please sign in to comment.