Commit 4e2be3d0 by triazo

Merge branch 'mutex' into 'master'

Mutex

:snail::dash: :zap::zap:

Basic mutex implementation, uses a try to lock model, where unsuccessfully obtaining a lock returns a 1, but does not hang.

See merge request !17
parents cc18f318 188429fd
Showing with 53 additions and 0 deletions
#include "mutex.h"
mutex mutex_create()
{
mutex m;
m.lock_var=0;
m.value=0;
m.thread_id=0;
return m;
}
uint32_t mutex_lock(mutex m)
{
asm volatile
(
"mov $1, %%eax; \
xchg %[lock], %%eax; \
mov %%eax, %[value]"
:[value] "=r" (m.value)
:[lock] "r" (m.lock_var)
);
return m.value;
}
uint32_t mutex_unlock(mutex m)
{
asm volatile
(
"mov $0, %%eax; \
xchg %[lock], %%eax; \
mov %%eax, %[value]"
:[value] "=r" (m.value)
:[lock] "r" (m.lock_var)
);
return m.value;
}
#ifndef _MUTEX_INC
#define _MUTEX_INC
#include <stdint.h>
typedef struct
{
uint32_t lock_var;
uint32_t value;
uint32_t thread_id;
}__attribute__((packed)) mutex;
mutex mutex_create();
uint32_t mutex_lock(mutex m);
uint32_t mutex_unlock(mutex m);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment