Chroma/chroma/lainlib/mutex/ticketlock.c
Curle a915465fc1
Add startings of new kernel-side library
Lainlib is libk. It is separate from Helix, which will become the 3D engine common to the kernel and the userspace.
2020-11-26 04:03:40 +00:00

21 lines
776 B
C

#include <kernel/chroma.h>
#include <lainlib/lainlib.h>
void TicketLock(ticketlock_t* Lock) {
size_t Ticket = atomic_fetch_add_explicit(&Lock->NextTicket, 1, memory_order_relaxed);
while(atomic_load_explicit(&Lock->NowServing, memory_order_acquire) != Ticket) {
PAUSE;
}
}
bool TicketAttemptLock(ticketlock_t* Lock) {
size_t Ticket = atomic_load_explicit(&Lock->NowServing, memory_order_relaxed);
return atomic_compare_exchange_strong_explicit(&Lock->NowServing, &Ticket, Ticket + 1, memory_order_acquire, memory_order_relaxed);
}
void TicketUnlock(ticketlock_t* Lock) {
size_t NextTicket = atomic_load_explicit(&Lock->NowServing, memory_order_relaxed) + 1;
atomic_store_explicit(&Lock->NowServing, NextTicket, memory_order_release);
}