fixed mistakes around semaphores

This commit is contained in:
2022-08-30 19:42:30 +02:00
parent 6974c2b8e6
commit ededa69859
4 changed files with 12 additions and 11 deletions

2
main.c
View File

@@ -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);
};

View File

@@ -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);
}

View File

@@ -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

View File

@@ -7,7 +7,7 @@
#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);
int32_t atom_inc32(volatile int32_t *addr, int32_t value);
int32_t atom_dec32(volatile int32_t *addr, int32_t value);
#endif