#pragma once #include #include // TODO: for cplusplus. /************************ *** Team Kitty, 2020 *** *** Chroma *** ***********************/ #ifdef __cplusplus extern "C" { #endif /* This file provides a simple implementation of a ticket-based locking system. * You should probably prefer Spinlock over Ticketlock. * * Create a new lock with NEW_TICKETLOCK(), * lock a resource with TicketLock(). * * Use TicketUnlock() to free the resource after you are done. * */ typedef struct { atomic_size_t NowServing; atomic_size_t NextTicket; } ticketlock_t; #define NEW_TICKETLOCK() (ticketlock_t{0}) void TicketLock(ticketlock_t* Lock); bool TicketAttemptLock(ticketlock_t* Lock); void TicketUnlock(ticketlock_t* Lock); #ifdef __cplusplus } #endif