Add Semaphore part to RIdhaOS

This commit is contained in:
Ridha Noomane 2025-01-15 08:59:26 +01:00
parent f7cd7387dc
commit aa087399ba
4 changed files with 57 additions and 6 deletions

View File

@ -3,6 +3,7 @@
#include "RIDHAOS_CONF.h" #include "RIDHAOS_CONF.h"
#include "ridhaOsScheduler.h" #include "ridhaOsScheduler.h"
#include "ridhaOsSemaphore.h"
/* This Struct used to restore next thread and save the running thread */ /* This Struct used to restore next thread and save the running thread */
typedef struct tControlBlock typedef struct tControlBlock
{ {

View File

@ -0,0 +1,11 @@
#ifndef __R_SEMAPHORE_H__
#define __R_SEMAPHORE_H__
#include "ridhaOs.h"
void ridhaOsSemaphoreInit(int32_t *semaphore, int32_t value);
void ridhaOsSemaphoreSet(int32_t * semaphore);
void ridhaOsSemaphoreWait(int32_t * semaphore);
#endif /* __R_SEMAPHORE_H__ */

View File

@ -0,0 +1,25 @@
#include "ridhaOsSemaphore.h"
void ridhaOsSemaphoreInit(int32_t *semaphore, int32_t value)
{
*semaphore = value;
}
void ridhaOsSemaphoreSet(int32_t * semaphore)
{
__disable_irq();
*semaphore += 1;
__enable_irq();
}
void ridhaOsSemaphoreWait(int32_t * semaphore)
{
__disable_irq();
while (*semaphore <= 0)
{
__disable_irq();
__enable_irq();
}
*semaphore -= 1;
__enable_irq();
}

22
main.c
View File

@ -5,6 +5,8 @@
#include "fsl_common.h" #include "fsl_common.h"
#include "ridhaOs.h" #include "ridhaOs.h"
int32_t semaphore1, semaphore2;
typedef uint32_t TaskProfiler; typedef uint32_t TaskProfiler;
TaskProfiler Task0_Profiler, Task1_Profiler, Task2_Profiler; TaskProfiler Task0_Profiler, Task1_Profiler, Task2_Profiler;
@ -22,12 +24,18 @@ void SystemInitHook()
int main(void){ int main(void){
SystemInitHook(); SystemInitHook();
uart_init();
/* Init Semaphore */
ridhaOsSemaphoreInit(&semaphore1, 1);
ridhaOsSemaphoreInit(&semaphore2, 0);
/* Add threads */ /* Add threads */
ridhaOsAddThreads(&task0, &task1, &task2); ridhaOsAddThreads(&task0, &task1, &task2);
ridhaOsStart(); ridhaOsStart();
/* We can't reach this point of program */ /* We can't reach this point of program */
while(1);
} }
@ -36,7 +44,10 @@ void task0(void)
Task0_Profiler = 0; Task0_Profiler = 0;
while(1) while(1)
{ {
//PRINTF("Task 0"); ridhaOsSemaphoreWait(&semaphore1);
PRINTF("Task 0\n");
ridhaOsSemaphoreSet(&semaphore2);
Task0_Profiler++; Task0_Profiler++;
} }
} }
@ -46,7 +57,10 @@ void task1(void)
Task1_Profiler = 0; Task1_Profiler = 0;
while(1) while(1)
{ {
//PRINTF("Task 1"); ridhaOsSemaphoreWait(&semaphore2);
PRINTF("Task 1\n");
ridhaOsSemaphoreSet(&semaphore1);
Task1_Profiler++; Task1_Profiler++;
} }
} }
@ -56,7 +70,7 @@ void task2(void)
Task2_Profiler = 0; Task2_Profiler = 0;
while(1) while(1)
{ {
//PRINTF("Task 2"); //PRINTF("Task 2\n");
Task2_Profiler++; Task2_Profiler++;
} }
} }