mirror of
https://github.com/obgm/libcoap.git
synced 2026-12-03 03:01:05 +03:00
multi-threading: Lock resource structure when calling request handlers
If an app request call-back modifies any data associated with a resource, then multiple threads can attempt to unsafely modify the data at the same time. Lock the resource when calling the app request handler (as global_lock is unlocked) to prevent concurrent changes to the resource information. If the app request handler is definately known to not modify the resource information, then COAP_RESOURCE_SAFE_REQUEST_HANDLER can be set in the flags for coap_resource_init(). Usually if there is a PUT, POST, PATCH or IPATCH handler it is unsafe to set this. Setting this will reduce lock contention.
This commit is contained in:
1 parent
efec6cf653
commit
39b79e23b4
13 files changed
+391
-112
No files matched your search
@@ -171,6 +171,15 @@ typedef void (*coap_method_handler_t)(coap_resource_t *resource,
|
||||
*/
|
||||
#define COAP_RESOURCE_HIDE_WELLKNOWN_CORE 0x2000
|
||||
|
||||
/**
|
||||
* Don't lock this resource when calling app call-back handler for requests
|
||||
* as handler will not be manipulating any resource held data.
|
||||
* Not locking the resource (if safe) can save lock contention on a busy system.
|
||||
* Caution: If there is a POST or PUT handler, then it is likely this should
|
||||
* not be set.
|
||||
*/
|
||||
#define COAP_RESOURCE_SAFE_REQUEST_HANDLER 0x4000
|
||||
|
||||
/**
|
||||
* Creates a new resource object and initializes the link field to the string
|
||||
* @p uri_path. This function returns the new coap_resource_t object.
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#ifndef COAP_RESOURCE_INTERNAL_H_
|
||||
#define COAP_RESOURCE_INTERNAL_H_
|
||||
|
||||
#include "coap_threadsafe_internal.h"
|
||||
#include "coap_uthash_internal.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -115,6 +116,9 @@ struct coap_resource_t {
|
||||
* the coap handler.
|
||||
*/
|
||||
void *user_data;
|
||||
#if COAP_THREAD_SAFE
|
||||
coap_lock_t lock;
|
||||
#endif /* COAP_THREAD_SAFE */
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -113,35 +113,37 @@ typedef struct coap_lock_t {
|
||||
} coap_lock_t;
|
||||
|
||||
/**
|
||||
* Unlock the global_lock lock.
|
||||
* Unlock the specific lock.
|
||||
*
|
||||
* If this is a nested lock (Public API - libcoap - app call-back - Public API),
|
||||
* then the lock remains locked, but global_lock.in_callback is decremented.
|
||||
* then the lock remains locked, but lock->in_callback is decremented.
|
||||
*
|
||||
* Note: Invoked by wrapper macro, not used directly.
|
||||
*
|
||||
* @param lock The lock to act on.
|
||||
* @param file The file from which coap_lock_unlock_func() is getting called.
|
||||
* @param line The line no from which coap_lock_unlock_func() is getting called.
|
||||
*/
|
||||
void coap_lock_unlock_func(const char *file, int line);
|
||||
void coap_lock_unlock_func(coap_lock_t *lock, const char *file, int line);
|
||||
|
||||
/**
|
||||
* Lock the global_lock lock.
|
||||
* Lock the specfic lock.
|
||||
*
|
||||
* If this is a nested lock (Public API - libcoap - app call-back - Public API),
|
||||
* then increment the global_lock.in_callback.
|
||||
* then increment the lock->in_callback.
|
||||
*
|
||||
* Note: Invoked by wrapper macro, not used directly.
|
||||
*
|
||||
* @param lock The lock to act on.
|
||||
* @param file The file from which coap_lock_lock_func() is getting called.
|
||||
* @param line The line no from which coap_lock_lock_func() is getting called.
|
||||
*
|
||||
* @return @c 0 if libcoap has not started (coap_startup() not called), else @c 1.
|
||||
*/
|
||||
int coap_lock_lock_func(const char *file, int line);
|
||||
int coap_lock_lock_func(coap_lock_t *lock, const char *file, int line);
|
||||
|
||||
/**
|
||||
* libcoap library code. Lock The global_lock.
|
||||
* libcoap library code. Lock the global_lock.
|
||||
*
|
||||
* Invoked when
|
||||
* Not locked at all
|
||||
@@ -156,20 +158,56 @@ int coap_lock_lock_func(const char *file, int line);
|
||||
*
|
||||
*/
|
||||
#define coap_lock_lock(failed) do { \
|
||||
if (!coap_lock_lock_func(__FILE__, __LINE__)) { \
|
||||
if (!coap_lock_lock_func(&global_lock, __FILE__, __LINE__)) { \
|
||||
failed; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Unlock The global_lock.
|
||||
* libcoap library code. Unlock the global_lock.
|
||||
*
|
||||
* Unlocked when
|
||||
* Same thread locked context
|
||||
* Not when called from app call-back
|
||||
*/
|
||||
#define coap_lock_unlock() do { \
|
||||
coap_lock_unlock_func(__FILE__, __LINE__); \
|
||||
coap_lock_unlock_func(&global_lock, __FILE__, __LINE__); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Lock a specific lock.
|
||||
*
|
||||
* Invoked when
|
||||
* Not locked at all
|
||||
* Locked, app call-back, call from app call-back
|
||||
* Locked, app call-back, call from app call-back, app call-back, call from app call-back
|
||||
* Result
|
||||
* global_lock locked.
|
||||
* global_lock not locked if libcoap not started and @p failed is executed. @p failed must
|
||||
* be code that skips doing the lock protected code.
|
||||
*
|
||||
* @param lock The specifc lock to lock.
|
||||
* @param failed Code to execute on lock failure.
|
||||
*
|
||||
*/
|
||||
#define coap_lock_specific_lock(lock, failed) do { \
|
||||
if (!coap_lock_lock_func(lock, __FILE__, __LINE__)) { \
|
||||
failed; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Unlock a specific lock.
|
||||
*
|
||||
* Unlocked when
|
||||
* Same thread locked context
|
||||
* Not when called from app call-back
|
||||
*
|
||||
* @param lock The specifc lock to unlock.
|
||||
*
|
||||
*/
|
||||
#define coap_lock_specific_unlock(lock) do { \
|
||||
coap_lock_unlock_func(lock, __FILE__, __LINE__); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
@@ -246,6 +284,32 @@ int coap_lock_lock_func(const char *file, int line);
|
||||
coap_lock_lock(failed); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Invoke an app callback, unlocking global_lock first,
|
||||
* followed by locking specified lock, and after callback unlock specified lock
|
||||
* and lock global_lock..
|
||||
*
|
||||
* Called when
|
||||
* Locked
|
||||
*
|
||||
* @param lock lock to lock before callback.
|
||||
* @param func app call-back function to invoke.
|
||||
* @param failed Code to execute on (re-)lock failure.
|
||||
*
|
||||
*/
|
||||
#define coap_lock_specific_callback_release(lock,func,failed) do { \
|
||||
coap_lock_check_locked(); \
|
||||
coap_lock_unlock(); \
|
||||
coap_lock_specific_lock(lock, failed); \
|
||||
(lock)->in_callback++; \
|
||||
(lock)->callback_file = __FILE__; \
|
||||
(lock)->callback_line = __LINE__; \
|
||||
func; \
|
||||
(lock)->in_callback--; \
|
||||
coap_lock_specific_unlock(lock); \
|
||||
coap_lock_lock(failed); \
|
||||
} while (0)
|
||||
|
||||
extern coap_lock_t global_lock;
|
||||
|
||||
# else /* ! COAP_THREAD_RECURSIVE_CHECK */
|
||||
@@ -268,8 +332,10 @@ typedef struct coap_lock_t {
|
||||
*
|
||||
* Note: Invoked by wrapper macro, not used directly.
|
||||
*
|
||||
* @param lock The lock to act on.
|
||||
*
|
||||
*/
|
||||
void coap_lock_unlock_func(void);
|
||||
void coap_lock_unlock_func(coap_lock_t *lock);
|
||||
|
||||
/**
|
||||
* Lock the global_lock lock.
|
||||
@@ -279,12 +345,14 @@ void coap_lock_unlock_func(void);
|
||||
*
|
||||
* Note: Invoked by wrapper macro, not used directly.
|
||||
*
|
||||
* @param lock The lock to act on.
|
||||
*
|
||||
* @return @c 0 if libcoap has not started (coap_startup() not called), else @c 1.
|
||||
*/
|
||||
int coap_lock_lock_func(void);
|
||||
int coap_lock_lock_func(coap_lock_t *lock);
|
||||
|
||||
/**
|
||||
* libcoap library code. Lock The global_lock.
|
||||
* libcoap library code. Lock the global_lock.
|
||||
*
|
||||
* Invoked when
|
||||
* Not locked at all
|
||||
@@ -299,20 +367,56 @@ int coap_lock_lock_func(void);
|
||||
*
|
||||
*/
|
||||
#define coap_lock_lock(failed) do { \
|
||||
if (!coap_lock_lock_func()) { \
|
||||
if (!coap_lock_lock_func(&global_lock)) { \
|
||||
failed; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Unlock The global_lock.
|
||||
* libcoap library code. Lock the specific lock.
|
||||
*
|
||||
* Invoked when
|
||||
* Not locked at all
|
||||
* Locked, app call-back, call from app call-back
|
||||
* Locked, app call-back, call from app call-back, app call-back, call from app call-back
|
||||
* Result
|
||||
* global_lock locked.
|
||||
* global not locked if libcoap not started and @p failed is executed. @p failed must
|
||||
* be code that skips doing the lock protected code.
|
||||
*
|
||||
* @param lock The specifc lock to lock.
|
||||
* @param failed Code to execute on lock failure
|
||||
*
|
||||
*/
|
||||
#define coap_lock_specific_lock(lock, failed) do { \
|
||||
if (!coap_lock_lock_func(lock)) { \
|
||||
failed; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Unlock the global_lock.
|
||||
*
|
||||
* Unlocked when
|
||||
* Same thread locked context.
|
||||
* Not when called from app call-back.
|
||||
*/
|
||||
#define coap_lock_unlock() do { \
|
||||
coap_lock_unlock_func(); \
|
||||
coap_lock_unlock_func(&global_lock); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Unlock the specific lock..
|
||||
*
|
||||
* Unlocked when
|
||||
* Same thread locked context.
|
||||
* Not when called from app call-back.
|
||||
*
|
||||
* @param lock The specifc lock to unlock.
|
||||
*
|
||||
*/
|
||||
#define coap_lock_specific_unlock(lock) do { \
|
||||
coap_lock_unlock_func(lock); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
@@ -366,6 +470,30 @@ int coap_lock_lock_func(void);
|
||||
coap_lock_lock(failed); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Invoke an app callback, unlocking global_lock first,
|
||||
* followed by locking specified lock, and after callback unlock specified lock
|
||||
* and lock global_lock..
|
||||
*
|
||||
* Called when
|
||||
* Locked (need to unlock over app call-back)
|
||||
*
|
||||
* @param lock lock to lock before callback.
|
||||
* @param func app call-back function to invoke.
|
||||
* @param failed Code to execute on (re-)lock failure.
|
||||
*
|
||||
*/
|
||||
#define coap_lock_specific_callback_release(lock,func,failed) do { \
|
||||
coap_lock_check_locked(); \
|
||||
coap_lock_unlock(); \
|
||||
coap_lock_specific_lock(lock, failed); \
|
||||
(lock)->in_callback++; \
|
||||
func; \
|
||||
(lock)->in_callback--; \
|
||||
coap_lock_specific_unlock(lock); \
|
||||
coap_lock_lock(failed); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* libcoap library code. Invoke an app callback that has a return value,
|
||||
* unlocking global_lock first.
|
||||
@@ -388,11 +516,11 @@ int coap_lock_lock_func(void);
|
||||
# endif /* ! COAP_THREAD_RECURSIVE_CHECK */
|
||||
|
||||
/**
|
||||
* libcoap library code. Initialize the global_lock.
|
||||
* libcoap library code. Initialize the specified lock.
|
||||
*/
|
||||
#define coap_lock_init() do { \
|
||||
memset(&global_lock.mutex, 0, sizeof(global_lock.mutex)); \
|
||||
coap_mutex_init(&global_lock.mutex); \
|
||||
#define coap_lock_init(lock) do { \
|
||||
memset(&(lock)->mutex, 0, sizeof((lock)->mutex)); \
|
||||
coap_mutex_init(&(lock)->mutex); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
@@ -433,7 +561,7 @@ typedef coap_mutex_t coap_lock_t;
|
||||
/**
|
||||
* Dummy for no thread-safe code
|
||||
*
|
||||
* libcoap library code. Lock The global_lock.
|
||||
* libcoap library code. Lock the global_lock.
|
||||
*
|
||||
* Invoked when
|
||||
* Not locked at all
|
||||
@@ -452,19 +580,51 @@ typedef coap_mutex_t coap_lock_t;
|
||||
/**
|
||||
* Dummy for no thread-safe code
|
||||
*
|
||||
* libcoap library code. Unlock The global_lock.
|
||||
* libcoap library code. Lock the specific lock.
|
||||
*
|
||||
* Invoked when
|
||||
* Not locked at all
|
||||
* Locked, app call-back, call from app call-back
|
||||
* Locked, app call-back, call from app call-back, app call-back, call from app call-back
|
||||
* Result
|
||||
* global_lock locked.
|
||||
* global_lock not locked if libcoap not started and @p failed is executed. @p failed must
|
||||
* be code that skips doing the lock protected code.
|
||||
*
|
||||
* @param lock The specifc lock to lock.
|
||||
* @param failed Code to execute on lock failure.
|
||||
*
|
||||
*/
|
||||
#define coap_lock_specific_lock(lock,failed)
|
||||
|
||||
/**
|
||||
* Dummy for no thread-safe code
|
||||
*
|
||||
* libcoap library code. Unlock the global_lock.
|
||||
*
|
||||
* Unlocked when
|
||||
* Same thread locked context
|
||||
*/
|
||||
#define coap_lock_unlock()
|
||||
|
||||
/**
|
||||
* Dummy for no thread-safe code
|
||||
*
|
||||
* libcoap library code. Unlock the specific lock.
|
||||
*
|
||||
* Unlocked when
|
||||
* Same thread locked context
|
||||
*
|
||||
* @param lock The specifc lock to unlock.
|
||||
*/
|
||||
#define coap_lock_specific_unlock(lock)
|
||||
|
||||
/**
|
||||
* Dummy for no thread-safe code
|
||||
*
|
||||
* libcoap library code. Initialize the global_lock.
|
||||
*/
|
||||
#define coap_lock_init()
|
||||
#define coap_lock_init(lock)
|
||||
|
||||
/**
|
||||
* Dummy for no thread-safe code
|
||||
@@ -515,6 +675,18 @@ typedef coap_mutex_t coap_lock_t;
|
||||
*/
|
||||
#define coap_lock_callback_release(func,failed) func
|
||||
|
||||
/**
|
||||
* Dummy for no thread-safe code
|
||||
*
|
||||
* libcoap library code. Invoke an app callback.
|
||||
*
|
||||
* @param lock lock to unlock before callback.
|
||||
* @param func app call-back function to invoke.
|
||||
* @param failed Code to execute on (re-)lock failure.
|
||||
*
|
||||
*/
|
||||
#define coap_lock_specific_callback_release(lock,func,failed) func
|
||||
|
||||
/**
|
||||
* Dummy for no thread-safe code
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user