Skip to content

Commit

Permalink
locking: After coap_free_context(), handle API calls from call-backs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed May 18, 2024
1 parent fb6b09e commit 628fc36
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/coap_threadsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ coap_lock_lock_func(coap_lock_t *lock, const char *file, int line) {
/* This is called from within an app callback */
lock->lock_count++;
assert(lock->in_callback == lock->lock_count);
return 1;
goto being_freed_check;
} else {
coap_log_alert("Thread Deadlock: Last %s: %u, this %s: %u\n",
lock->lock_file, lock->lock_line, file, line);
Expand All @@ -712,11 +712,15 @@ coap_lock_lock_func(coap_lock_t *lock, const char *file, int line) {
/* Wait for the other thread to unlock */
coap_mutex_lock(&lock->mutex);
}
/* Just got the lock, so should not be in a locked callback */
assert(!lock->in_callback);
lock->pid = coap_thread_pid;
lock->lock_file = file;
lock->lock_line = line;
if (lock->in_callback) {
/* This is when called from within an app callback and context is going away */
lock->lock_count++;
assert(lock->in_callback == lock->lock_count);
}
being_freed_check:
if (lock->being_freed) {
/* context is in the process of being deleted */
coap_lock_unlock_func(lock, file, line);
Expand Down Expand Up @@ -746,13 +750,19 @@ coap_lock_lock_func(coap_lock_t *lock) {
* cannot use that here and have to rely on lock-pid being stable
*/
if (lock->in_callback && coap_thread_pid == lock->pid) {
if (lock->being_freed) {
return 0;
}
lock->lock_count++;
assert(lock->in_callback == lock->lock_count);
} else {
coap_mutex_lock(&lock->mutex);
/* Just got the lock, so should not be in a locked callback */
assert(!lock->in_callback);
lock->pid = coap_thread_pid;
if (lock->in_callback) {
/* This is when called from within an app callback and context is going away */
lock->lock_count++;
assert(lock->in_callback == lock->lock_count);
}
if (lock->being_freed) {
coap_lock_unlock_func(lock);
return 0;
Expand Down

0 comments on commit 628fc36

Please sign in to comment.