atom_* renamed to atomic_*; add mutex.*
This commit is contained in:
@@ -35,6 +35,8 @@ OBJS+= usartu.o
|
|||||||
OBJS+= scheduler.o
|
OBJS+= scheduler.o
|
||||||
OBJS+= atomic.o
|
OBJS+= atomic.o
|
||||||
OBJS+= semaphore.o
|
OBJS+= semaphore.o
|
||||||
|
OBJS+= mutex.o
|
||||||
|
|
||||||
|
|
||||||
main.elf: $(OBJS)
|
main.elf: $(OBJS)
|
||||||
$(TARGET)-gcc $(^F) $(LDFLAGS) -o $@
|
$(TARGET)-gcc $(^F) $(LDFLAGS) -o $@
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
|
|
||||||
.text
|
.text
|
||||||
|
|
||||||
.globl atom_inc32
|
.globl atomic_inc32
|
||||||
.type atom_inc32, %function
|
.type atomic_inc32, %function
|
||||||
|
|
||||||
atom_inc32:
|
atomic_inc32:
|
||||||
1: ldrex r2, [r0]
|
1: ldrex r2, [r0]
|
||||||
add r2, r2, r1
|
add r2, r2, r1
|
||||||
strex r3, r2, [r0]
|
strex r3, r2, [r0]
|
||||||
@@ -16,10 +16,10 @@ atom_inc32:
|
|||||||
bx lr
|
bx lr
|
||||||
|
|
||||||
|
|
||||||
.globl atom_dec32
|
.globl atomic_dec32
|
||||||
.type atom_dec32, %function
|
.type atomic_dec32, %function
|
||||||
|
|
||||||
atom_dec32:
|
atomic_dec32:
|
||||||
2: ldrex r2, [r0]
|
2: ldrex r2, [r0]
|
||||||
sub r2, r2, r1
|
sub r2, r2, r1
|
||||||
strex r3, r2, [r0]
|
strex r3, r2, [r0]
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
int32_t atom_inc32(volatile int32_t *addr, int32_t value);
|
int32_t atomic_inc32(volatile int32_t *addr, int32_t value);
|
||||||
int32_t atom_dec32(volatile int32_t *addr, int32_t value);
|
int32_t atomic_dec32(volatile int32_t *addr, int32_t value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ void task3(void) {
|
|||||||
void task4(void) {
|
void task4(void) {
|
||||||
static volatile int32_t t4;
|
static volatile int32_t t4;
|
||||||
while (true) {
|
while (true) {
|
||||||
atom_inc32(&t4, (int32_t)1);
|
atomic_inc32(&t4, (int32_t)1);
|
||||||
sem_wait(&g_sem);
|
sem_wait(&g_sem);
|
||||||
printf("task 4 %d %lu\r\n", g_uptime, t4);
|
printf("task 4 %d %lu\r\n", g_uptime, t4);
|
||||||
sem_post(&g_sem);
|
sem_post(&g_sem);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
+3
-3
@@ -13,12 +13,12 @@ int32_t sem_wait(sem_t* sem) {
|
|||||||
//while(sem->value <= 0);
|
//while(sem->value <= 0);
|
||||||
//sem->value--;
|
//sem->value--;
|
||||||
//return sem->value;
|
//return sem->value;
|
||||||
while (atom_dec32(&(sem->value), (int32_t)0) <= 0);
|
while (atomic_dec32(&(sem->value), (int32_t)0) <= 0);
|
||||||
return atom_dec32(&(sem->value), (int32_t)1);
|
return atomic_dec32(&(sem->value), (int32_t)1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t sem_post(sem_t* sem) {
|
int32_t sem_post(sem_t* sem) {
|
||||||
//sem->value++;
|
//sem->value++;
|
||||||
//return sem->value;
|
//return sem->value;
|
||||||
return atom_inc32(&(sem->value), (int32_t)1);
|
return atomic_inc32(&(sem->value), (int32_t)1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user