added mem-block semaphore

This commit is contained in:
2022-08-30 19:01:21 +02:00
parent 4c67d0bba4
commit 6974c2b8e6
6 changed files with 68 additions and 18 deletions

View File

@@ -14,6 +14,7 @@ CFLAGS+= -fno-common -ffunction-sections -fdata-sections
CFLAGS+= -g -gdwarf-2
CFLAGS+= -Wall
LDFLAGS+= ${CFLAGS}
LDFLAGS+= --static
#LDFLAGS+= -nostartfiles
@@ -32,7 +33,8 @@ OBJS+= main.o
OBJS+= syscall.o
OBJS+= usartu.o
OBJS+= scheduler.o
OBJS+= semoper.o
OBJS+= semaphore.o
main.elf: $(OBJS)
$(TARGET)-gcc $(^F) $(LDFLAGS) -o $@
@@ -42,7 +44,7 @@ main.elf: $(OBJS)
$(TARGET)-gcc $(CFLAGS) -c -o $@ $<
%.o: %.S
$(TARGET)-as $(ASFLAGS) -o $@ $<
$(TARGET)-gcc $(CFLAGS) -c -o $@ $<
%.bin: %.elf
$(TARGET)-objcopy -O binary $< $@

6
main.c
View File

@@ -18,6 +18,7 @@
#include "scheduler.h"
#include "usartu.h"
#include "semoper.h"
void delay(uint32_t n) {
@@ -81,8 +82,11 @@ void task3(void) {
}
void task4(void) {
static volatile int32_t t4;
while (true) {
printf("task 4 %d\r\n", g_uptime);
sem_post32(&t4, (int32_t)1);
printf("task 4 %d %lu\r\n", g_uptime, t4);
delay(3300);
};
}

View File

@@ -3,21 +3,19 @@
*/
#include "semaphore.h"
#include "semoper.h"
typedef struct {
int value;
} sem_t;
int sem_init(sem_t* sem, int value) {
void sem_init(sem_t* sem, int32_t value) {
sem->value = value;
}
int sem_wait(sem_t* sem) {
while(sem->value > 0);
sem->value--;
int32_t sem_wait(sem_t* sem) {
//while(sem->value > 0);
//sem->value--;
return sem_wait32(&(sem->value), (int32_t)1);
}
int sem_post(sem_t* sem) {
sem->value++;
int32_t sem_post(sem_t* sem) {
//sem->value++;
return sem_post32(&(sem->value), (int32_t)1);
}

View File

@@ -3,10 +3,12 @@
*/
#include <stdint.h>
typedef struct {
int value;
int32_t value;
} sem_t;
int sem_init(sem_t* sem, int value);
int sem_wait(sem_t* sem);
int sem_post(sem_t* sem);
void sem_init(sem_t* sem, int32_t value);
int32_t sem_wait(sem_t* sem);
int32_t sem_post(sem_t* sem);

31
semoper.S Normal file
View File

@@ -0,0 +1,31 @@
.thumb
.syntax unified
.text
.globl sem_post32
.type sem_post32, %function
sem_post32:
1:
ldrex r2, [r0]
add r2, r2, r1
strex r3, r2, [r0]
teq r3, #0
bne 1b
mov r0, r2
bx lr
.globl sem_wait32
.type sem_wait32, %function
sem_wait32:
1:
ldrex r2, [r0]
sub r2, r2, r1
strex r3, r2, [r0]
teq r3, #0
bne 1b
mov r0, r2
bx lr

13
semoper.h Normal file
View File

@@ -0,0 +1,13 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#ifndef SEMOPER_H_QWERTY
#define SEMOPER_H_QWERTY
#include <stdint.h>
int32_t sem_post32(volatile int32_t *addr, int32_t value);
int32_t sem_wait32(volatile int32_t *addr, int32_t value);
#endif