Skip to content

Commit

Permalink
Zephyr platform change only: Fix for thread memory pool (#708)
Browse files Browse the repository at this point in the history
Systempool is now allocated during startup in z_sys_init_run_level,
threads created afterwards will inherit this pool.
This resolves a known issue in Zephyr when calling UBXLIB API from
threads that isn't the Zephyr main thread.
  • Loading branch information
mekm committed Sep 21, 2022
1 parent 6b54f06 commit fb63a1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 12 additions & 0 deletions port/platform/zephyr/src/u_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "u_port_private.h"

#include "zephyr.h"
#include <device.h>

/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
Expand Down Expand Up @@ -158,4 +159,15 @@ U_INLINE void uPortExitCritical()
{
irq_unlock(gIrqLockKey);
}

static int ubxlib_preinit(const struct device *arg)
{
ARG_UNUSED(arg);

k_thread_system_pool_assign(k_current_get());
return 0;
}

SYS_INIT(ubxlib_preinit, PRE_KERNEL_1, 0);

// End of file
12 changes: 4 additions & 8 deletions port/platform/zephyr/src/u_port_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,10 @@ static void freeThreadInstance(struct k_thread *threadPtr)
// Initialise thread pool.
void uPortOsPrivateInit()
{
// There is a bug in Zephyr where main thread.resource_pool
// contains an uninitilized value. This results in crashes
// when using some of the Zephyr API that allocates memory
// using the resource pool (such as k_msgq_alloc_init()).
// Below is a workaround until this is fixed.
extern struct k_thread z_main_thread;
k_thread_system_pool_assign(&z_main_thread);

// systempool is now allocated during startup in z_sys_init_run_level.
// threads created afterwards will inherit this pool.
// this resolves a knownn issue in Zephyr when calling UBXLIB API from
// threads that isn't the Zephyr main thread.
for (int32_t i = 0; i < U_CFG_OS_MAX_THREADS; i++) {
gThreadInstances[i].pThread = NULL;
gThreadInstances[i].pStack = NULL;
Expand Down

0 comments on commit fb63a1f

Please sign in to comment.