atom_* renamed to atomic_*; add mutex.*

This commit is contained in:
2022-08-31 08:57:57 +02:00
parent f9856341c1
commit 330efbdbb2
7 changed files with 52 additions and 12 deletions

View File

@@ -35,6 +35,8 @@ OBJS+= usartu.o
OBJS+= scheduler.o
OBJS+= atomic.o
OBJS+= semaphore.o
OBJS+= mutex.o
main.elf: $(OBJS)
$(TARGET)-gcc $(^F) $(LDFLAGS) -o $@

View File

@@ -3,10 +3,10 @@
.text
.globl atom_inc32
.type atom_inc32, %function
.globl atomic_inc32
.type atomic_inc32, %function
atom_inc32:
atomic_inc32:
1: ldrex r2, [r0]
add r2, r2, r1
strex r3, r2, [r0]
@@ -16,10 +16,10 @@ atom_inc32:
bx lr
.globl atom_dec32
.type atom_dec32, %function
.globl atomic_dec32
.type atomic_dec32, %function
atom_dec32:
atomic_dec32:
2: ldrex r2, [r0]
sub r2, r2, r1
strex r3, r2, [r0]

View File

@@ -7,7 +7,7 @@
#include <stdint.h>
int32_t atom_inc32(volatile int32_t *addr, int32_t value);
int32_t atom_dec32(volatile int32_t *addr, int32_t value);
int32_t atomic_inc32(volatile int32_t *addr, int32_t value);
int32_t atomic_dec32(volatile int32_t *addr, int32_t value);
#endif

2
main.c
View File

@@ -92,7 +92,7 @@ void task3(void) {
void task4(void) {
static volatile int32_t t4;
while (true) {
atom_inc32(&t4, (int32_t)1);
atomic_inc32(&t4, (int32_t)1);
sem_wait(&g_sem);
printf("task 4 %d %lu\r\n", g_uptime, t4);
sem_post(&g_sem);

24
mutex.c Normal file
View File

@@ -0,0 +1,24 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include "mutex.h"
#include "atomic.h"
void mutex_init(mutex_t* mutex) {
mutex->value = 1;
}
int32_t mutex_wait(mutex_t* mutex) {
//while(mutex->value <= 0);
//mutex->value--;
//return mutex->value;
while (atomic_dec32(&(mutex->value), (int32_t)0) <= 0);
return atomic_dec32(&(mutex->value), (int32_t)1);
}
int32_t mutex_post(mutex_t* mutex) {
//mutex->value++;
//return mutex->value;
return atomic_inc32(&(mutex->value), (int32_t)1);
}

14
mutex.h Normal file
View File

@@ -0,0 +1,14 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdint.h>
typedef struct {
int32_t value;
} mutex_t;
void mutex_init(mutex_t* mutex);
int32_t mutex_wait(mutex_t* mutex);
int32_t mutex_post(mutex_t* mutex);

View File

@@ -13,12 +13,12 @@ int32_t sem_wait(sem_t* sem) {
//while(sem->value <= 0);
//sem->value--;
//return sem->value;
while (atom_dec32(&(sem->value), (int32_t)0) <= 0);
return atom_dec32(&(sem->value), (int32_t)1);
while (atomic_dec32(&(sem->value), (int32_t)0) <= 0);
return atomic_dec32(&(sem->value), (int32_t)1);
}
int32_t sem_post(sem_t* sem) {
//sem->value++;
//return sem->value;
return atom_inc32(&(sem->value), (int32_t)1);
return atomic_inc32(&(sem->value), (int32_t)1);
}