Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mneedes committed Feb 12, 2023
1 parent be37667 commit c6e0403
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2019-2020 Matthew Christopher Needes
Copyright 2019-2023 Matthew Christopher Needes

Permission is hereby granted, free of charge, to any person or legal entity
obtaining a copy of this software and associated documentation files (the
Expand Down
2 changes: 1 addition & 1 deletion apps/tests/stm32f767/hal_tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static s32 HalPulseReceiverThread(s32 arg) {
MOS_UNUSED(arg);
pulse_counter = 0;
mosInitSem(&pulse_sem, 0);
mosSetTermHandler(mosGetThreadPtr(), HalPulseReceiverTermHandler, TEST_PASS);
mosSetTermHandler(mosGetRunningThread(), HalPulseReceiverTermHandler, TEST_PASS);
// Set interrupt to high priority (higher than scheduler at least)
NVIC_SetPriority(EXTI15_10_IRQn, 0);
NVIC_EnableIRQ(EXTI15_10_IRQn);
Expand Down
24 changes: 12 additions & 12 deletions apps/tests/tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static u32 queue[4];
static MosQueue TestQueue;

bool IsStopRequested() {
return (bool)mosGetThreadPtr()->pUser;
return (bool)mosGetRunningThread()->pUser;
}

void RequestThreadStop(MosThread * pThd) {
Expand Down Expand Up @@ -156,40 +156,40 @@ static s32 KillTestHandler(s32 arg) {

static s32 KillTestThread(s32 arg) {
if (arg) {
mosSetTermHandler(mosGetThreadPtr(), KillTestHandler, TEST_PASS_HANDLER);
mosSetTermHandler(mosGetRunningThread(), KillTestHandler, TEST_PASS_HANDLER);
// Lock mutex a couple times... need to release it in handler
mosLockMutex(&TestMutex);
mosLockMutex(&TestMutex);
} else mosSetTermArg(mosGetThreadPtr(), TEST_PASS_HANDLER);
} else mosSetTermArg(mosGetRunningThread(), TEST_PASS_HANDLER);
mosLogTrace(TRACE_INFO, "KillTestThread: Blocking\n");
mosWaitForSem(&TestSem);
return TEST_FAIL;
}

static s32 KillSelfTestThread(s32 arg) {
if (arg) {
mosSetTermHandler(mosGetThreadPtr(), KillTestHandler, TEST_PASS_HANDLER);
mosSetTermHandler(mosGetRunningThread(), KillTestHandler, TEST_PASS_HANDLER);
// Lock mutex a couple times... need to release it in handler
mosLockMutex(&TestMutex);
mosLockMutex(&TestMutex);
} else mosSetTermArg(mosGetThreadPtr(), TEST_PASS_HANDLER);
} else mosSetTermArg(mosGetRunningThread(), TEST_PASS_HANDLER);
mosLogTrace(TRACE_INFO, "KillSelfTestThread: Killing Self\n");
mosKillThread(mosGetThreadPtr());
mosKillThread(mosGetRunningThread());
return TEST_FAIL;
}

static s32 ExcTestThread(s32 arg) {
MOS_UNUSED(arg);
mosPrintf("Running Exception Thread %X\n", arg);
mosSetTermArg(mosGetThreadPtr(), TEST_PASS_HANDLER + 1);
mosSetTermArg(mosGetRunningThread(), TEST_PASS_HANDLER + 1);
mosDelayThread(50);
CauseCrash();
return TEST_FAIL;
}

#ifdef DEBUG
static s32 AssertTestThread(s32 arg) {
mosSetTermArg(mosGetThreadPtr(), TEST_PASS_HANDLER);
mosSetTermArg(mosGetRunningThread(), TEST_PASS_HANDLER);
mosAssert(arg == 0x1234);
return TEST_FAIL;
}
Expand All @@ -202,7 +202,7 @@ static s32 FPTestThread(s32 arg) {
x = x + 1.0;
if (arg > 1 && (TestHisto[arg] == 1000)) {
// Create an integer div-by-0 exception in FP thread
mosSetTermArg(mosGetThreadPtr(), TEST_PASS_HANDLER + 1);
mosSetTermArg(mosGetRunningThread(), TEST_PASS_HANDLER + 1);
volatile u32 y = (20 / (arg - 2));
(void)y;
return TEST_FAIL;
Expand Down Expand Up @@ -1874,7 +1874,7 @@ static s32 StackPrintThread(s32 arg) {
#if (MOS_ENABLE_SPLIM_SUPPORT == true)

static s32 MOS_OPT(0) StackOverflowThread(s32 arg) {
mosSetTermArg(mosGetThreadPtr(), TEST_PASS_HANDLER + 1);
mosSetTermArg(mosGetRunningThread(), TEST_PASS_HANDLER + 1);
return StackOverflowThread(arg);
}

Expand Down Expand Up @@ -1904,9 +1904,9 @@ static bool MiscTests(void) {
mosPrint("Misc Test: Stack stats\n");
{
u32 size = 0, usage = 0, max_usage = 0;
mosGetStackStats(mosGetThreadPtr(), &size, &usage, &max_usage);
mosGetStackStats(mosGetRunningThread(), &size, &usage, &max_usage);
mosPrintf("Stack: size: %u usage: %u max_usage: %u\n", size, usage, max_usage);
if (size != mosGetStackSize(mosGetThreadPtr())) test_pass = false;
if (size != mosGetStackSize(mosGetRunningThread())) test_pass = false;
}
if (test_pass) mosPrint(" Passed\n");
else {
Expand Down
2 changes: 1 addition & 1 deletion include/mos/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void mosResetTimer(MosTimer * pTmr);

/// Obtain pointer to currently running thread.
///
MosThread * mosGetThreadPtr(void);
MosThread * mosGetRunningThread(void);
/// Delay thread a number of ticks, zero input yields thread (see mosYieldThread).
///
void mosDelayThread(u32 ticks);
Expand Down
12 changes: 7 additions & 5 deletions source/mos/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
#include <mos/internal/security.h>
#include <errno.h>

// TODO: "Better Fit" Allocator improvement.
// TODO: "Better Fit" Allocator improvement. ASSERT on errors.
// TODO: Assert failure should hang/crash
// TODO: Thread-local storage
// TODO: Consolidate "TO" APIs to single API function.
// TODO: Atomic handle pool
// TODO: Logging

// TODO: Hooks for other timers such as LPTIM ?
Expand Down Expand Up @@ -435,7 +437,7 @@ static s32 IdleThreadEntry(s32 arg) {
return 0;
}

MosThread * mosGetThreadPtr(void) {
MosThread * mosGetRunningThread(void) {
return (MosThread *)pRunningThread;
}

Expand Down Expand Up @@ -839,7 +841,7 @@ static u32 MOS_USED Scheduler(u32 sp) {
} else {
pRunningThread = &IdleThread;
#if (MOS_ARM_RTOS_ON_NON_SECURE_SIDE == true)
_NSC_MosInitSecureContexts(KPrint, RawPrintBuffer);
_NSC_mosInitSecureContexts(KPrint, RawPrintBuffer);
#endif
}
// Update Running Thread state
Expand Down Expand Up @@ -922,10 +924,10 @@ static u32 MOS_USED Scheduler(u32 sp) {
// If there is a new secure context, only load the next context, don't save it.
// otherwise only save/load the context if it is different.
if (pRunningThread->secure_context_new != pRunningThread->secure_context) {
_NSC_MosSwitchSecureContext(-1, runThd->secure_context);
_NSC_mosSwitchSecureContext(-1, runThd->secure_context);
pRunningThread->secure_context = pRunningThread->secure_context_new;
} else if (pRunningThread->secure_context != runThd->secure_context)
_NSC_MosSwitchSecureContext(pRunningThread->secure_context, runThd->secure_context);
_NSC_mosSwitchSecureContext(pRunningThread->secure_context, runThd->secure_context);
#endif
// Set next thread ID and errno and return its stack pointer
pRunningThread = runThd;
Expand Down

0 comments on commit c6e0403

Please sign in to comment.