2020-08-31 20:44:54 +00:00
|
|
|
#pragma once
|
|
|
|
#include <stdbool.h>
|
2021-07-15 03:43:46 +00:00
|
|
|
#include <stddef.h>
|
2020-08-31 20:44:54 +00:00
|
|
|
|
|
|
|
/************************
|
|
|
|
*** Team Kitty, 2020 ***
|
|
|
|
*** Chroma ***
|
|
|
|
***********************/
|
|
|
|
|
2021-07-04 20:47:09 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2020-08-31 20:44:54 +00:00
|
|
|
/* This file provides a simple implementation of a ticket-based locking system.
|
|
|
|
* You should probably prefer Spinlock over Ticketlock.
|
2021-07-04 20:47:09 +00:00
|
|
|
*
|
|
|
|
* Create a new lock with NEW_TICKETLOCK(),
|
2020-08-31 20:44:54 +00:00
|
|
|
* lock a resource with TicketLock().
|
2021-07-04 20:47:09 +00:00
|
|
|
*
|
2020-08-31 20:44:54 +00:00
|
|
|
* Use TicketUnlock() to free the resource after you are done.
|
2021-07-04 20:47:09 +00:00
|
|
|
*
|
2020-08-31 20:44:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2021-07-15 03:43:46 +00:00
|
|
|
size_t NowServing;
|
|
|
|
size_t NextTicket;
|
2020-08-31 20:44:54 +00:00
|
|
|
} ticketlock_t;
|
|
|
|
|
|
|
|
#define NEW_TICKETLOCK() (ticketlock_t{0})
|
|
|
|
|
|
|
|
void TicketLock(ticketlock_t* Lock);
|
|
|
|
|
|
|
|
bool TicketAttemptLock(ticketlock_t* Lock);
|
|
|
|
|
|
|
|
void TicketUnlock(ticketlock_t* Lock);
|
|
|
|
|
2021-07-04 20:47:09 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|