From ededa698590344a0a926033be97ee21ee61ee1d7 Mon Sep 17 00:00:00 2001 From: Oleg Borodin Date: Tue, 30 Aug 2022 19:42:30 +0200 Subject: [PATCH] fixed mistakes around semaphores --- main.c | 2 +- semaphore.c | 5 +++-- semoper.S | 12 ++++++------ semoper.h | 4 ++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 55b5668..d50c57d 100644 --- a/main.c +++ b/main.c @@ -85,7 +85,7 @@ void task4(void) { static volatile int32_t t4; while (true) { - sem_post32(&t4, (int32_t)1); + atom_inc32(&t4, (int32_t)1); printf("task 4 %d %lu\r\n", g_uptime, t4); delay(3300); }; diff --git a/semaphore.c b/semaphore.c index a8e0032..df8ff1e 100644 --- a/semaphore.c +++ b/semaphore.c @@ -12,10 +12,11 @@ void sem_init(sem_t* sem, int32_t value) { int32_t sem_wait(sem_t* sem) { //while(sem->value > 0); //sem->value--; - return sem_wait32(&(sem->value), (int32_t)1); + while (atom_dec32(&(sem->value), (int32_t)0) > 0); + return atom_dec32(&(sem->value), (int32_t)1); } int32_t sem_post(sem_t* sem) { //sem->value++; - return sem_post32(&(sem->value), (int32_t)1); + return atom_inc32(&(sem->value), (int32_t)1); } diff --git a/semoper.S b/semoper.S index 561f1f2..52d185d 100644 --- a/semoper.S +++ b/semoper.S @@ -3,10 +3,10 @@ .text -.globl sem_post32 -.type sem_post32, %function +.globl atom_inc32 +.type atom_inc32, %function -sem_post32: +atom_inc32: 1: ldrex r2, [r0] add r2, r2, r1 @@ -17,10 +17,10 @@ sem_post32: bx lr -.globl sem_wait32 -.type sem_wait32, %function +.globl atom_dec32 +.type atom_dec32, %function -sem_wait32: +atom_dec32: 1: ldrex r2, [r0] sub r2, r2, r1 diff --git a/semoper.h b/semoper.h index 23d2d10..90563eb 100644 --- a/semoper.h +++ b/semoper.h @@ -7,7 +7,7 @@ #include -int32_t sem_post32(volatile int32_t *addr, int32_t value); -int32_t sem_wait32(volatile int32_t *addr, int32_t value); +int32_t atom_inc32(volatile int32_t *addr, int32_t value); +int32_t atom_dec32(volatile int32_t *addr, int32_t value); #endif