net/gcoap: Protect update to open requests with mutex.
This commit is contained in:
parent
24b21dbfbc
commit
aa2f97c80f
@ -212,6 +212,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
#include "net/sock/udp.h"
|
#include "net/sock/udp.h"
|
||||||
|
#include "mutex.h"
|
||||||
#include "nanocoap.h"
|
#include "nanocoap.h"
|
||||||
#include "xtimer.h"
|
#include "xtimer.h"
|
||||||
|
|
||||||
@ -439,6 +440,7 @@ typedef struct {
|
|||||||
* @brief Container for the state of gcoap itself
|
* @brief Container for the state of gcoap itself
|
||||||
*/
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
mutex_t lock; /**< Shares state attributes safely */
|
||||||
gcoap_listener_t *listeners; /**< List of registered listeners */
|
gcoap_listener_t *listeners; /**< List of registered listeners */
|
||||||
gcoap_request_memo_t open_reqs[GCOAP_REQ_WAITING_MAX];
|
gcoap_request_memo_t open_reqs[GCOAP_REQ_WAITING_MAX];
|
||||||
/**< Storage for open requests; if first
|
/**< Storage for open requests; if first
|
||||||
|
|||||||
@ -585,6 +585,7 @@ kernel_pid_t gcoap_init(void)
|
|||||||
_pid = thread_create(_msg_stack, sizeof(_msg_stack), THREAD_PRIORITY_MAIN - 1,
|
_pid = thread_create(_msg_stack, sizeof(_msg_stack), THREAD_PRIORITY_MAIN - 1,
|
||||||
THREAD_CREATE_STACKTEST, _event_loop, NULL, "coap");
|
THREAD_CREATE_STACKTEST, _event_loop, NULL, "coap");
|
||||||
|
|
||||||
|
mutex_init(&_coap_state.lock);
|
||||||
/* Blank lists so we know if an entry is available. */
|
/* Blank lists so we know if an entry is available. */
|
||||||
memset(&_coap_state.open_reqs[0], 0, sizeof(_coap_state.open_reqs));
|
memset(&_coap_state.open_reqs[0], 0, sizeof(_coap_state.open_reqs));
|
||||||
memset(&_coap_state.observers[0], 0, sizeof(_coap_state.observers));
|
memset(&_coap_state.observers[0], 0, sizeof(_coap_state.observers));
|
||||||
@ -683,6 +684,7 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
|
|||||||
assert(resp_handler != NULL);
|
assert(resp_handler != NULL);
|
||||||
|
|
||||||
/* Find empty slot in list of open requests. */
|
/* Find empty slot in list of open requests. */
|
||||||
|
mutex_lock(&_coap_state.lock);
|
||||||
for (int i = 0; i < GCOAP_REQ_WAITING_MAX; i++) {
|
for (int i = 0; i < GCOAP_REQ_WAITING_MAX; i++) {
|
||||||
if (_coap_state.open_reqs[i].state == GCOAP_MEMO_UNUSED) {
|
if (_coap_state.open_reqs[i].state == GCOAP_MEMO_UNUSED) {
|
||||||
memo = &_coap_state.open_reqs[i];
|
memo = &_coap_state.open_reqs[i];
|
||||||
@ -690,6 +692,8 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mutex_unlock(&_coap_state.lock);
|
||||||
|
|
||||||
if (memo) {
|
if (memo) {
|
||||||
memcpy(&memo->hdr_buf[0], buf, GCOAP_HEADER_MAXLEN);
|
memcpy(&memo->hdr_buf[0], buf, GCOAP_HEADER_MAXLEN);
|
||||||
memo->resp_handler = resp_handler;
|
memo->resp_handler = resp_handler;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user