BMOSP/kernel/lock.c
2024-01-22 21:46:36 +03:00

43 lines
862 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* lock.c
* Функции блокировок
*
* Функционал блокировок и синхронизации
*
*/
#include <fb.h>
#include <lock.h>
#include <log.h>
#include <stdint.h>
#include <tool.h>
// Если не заблокировано - блокируем
int lock_swap(lock_t lock) {
if (lock.lock) { return 0; }
lock.lock = 1;
return 1;
}
// Запрос блокировки ресурса
void lock_acquire(lock_t lock) {
uint64_t count = 0;
for (;;) {
if (lock_swap(lock)) { break; }
count++;
if (count > 10000) {
LOG("%s:%u блокировка зависла", lock.func, lock.line);
assert(0);
}
asm volatile("pause");
if (task_f_init) { task_switch( ); }
}
}
// Запрос разблокировки ресурса
void lock_release(lock_t lock) {
if (lock.lock) { lock.lock = 0; }
}