Skip to content

Commit

Permalink
gnrc_netif_pktq: protect against concurrent access
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jun 1, 2022
1 parent ac524e6 commit 61cc168
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions sys/net/gnrc/netif/pktq/gnrc_netif_pktq.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@
#define ENABLE_DEBUG 0
#include "debug.h"

static mutex_t _pool_lock = MUTEX_INIT;
static gnrc_pktqueue_t _pool[CONFIG_GNRC_NETIF_PKTQ_POOL_SIZE];

static gnrc_pktqueue_t *_get_free_entry(void)
static gnrc_pktqueue_t *_get_free_entry(gnrc_pktsnip_t *pkt)
{
gnrc_pktqueue_t *entry = NULL;

mutex_lock(&_pool_lock);
for (unsigned i = 0; i < CONFIG_GNRC_NETIF_PKTQ_POOL_SIZE; i++) {
if (_pool[i].pkt == NULL) {
return &_pool[i];
_pool[i].pkt = pkt;
entry = &_pool[i];
break;
}
}
return NULL;
mutex_unlock(&_pool_lock);

return entry;
}

unsigned gnrc_netif_pktq_usage(void)
Expand All @@ -52,12 +60,11 @@ int gnrc_netif_pktq_put(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
assert(netif != NULL);
assert(pkt != NULL);

gnrc_pktqueue_t *entry = _get_free_entry();
gnrc_pktqueue_t *entry = _get_free_entry(pkt);

if (entry == NULL) {
return -1;
}
entry->pkt = pkt;
gnrc_pktqueue_add(&netif->send_queue.queue, entry);
return 0;
}
Expand Down Expand Up @@ -94,12 +101,11 @@ int gnrc_netif_pktq_push_back(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
assert(netif != NULL);
assert(pkt != NULL);

gnrc_pktqueue_t *entry = _get_free_entry();
gnrc_pktqueue_t *entry = _get_free_entry(pkt);

if (entry == NULL) {
return -1;
}
entry->pkt = pkt;
LL_PREPEND(netif->send_queue.queue, entry);
return 0;
}
Expand Down

0 comments on commit 61cc168

Please sign in to comment.