Chroma/inc/lainlib/mutex/ticketlock.h

40 lines
773 B
C
Raw Normal View History

#pragma once
#include <stdbool.h>
2021-07-15 03:43:46 +00:00
#include <stddef.h>
/************************
*** Team Kitty, 2020 ***
*** Chroma ***
***********************/
2021-07-04 20:47:09 +00:00
#ifdef __cplusplus
extern "C" {
#endif
/* 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(),
* lock a resource with TicketLock().
2021-07-04 20:47:09 +00:00
*
* Use TicketUnlock() to free the resource after you are done.
2021-07-04 20:47:09 +00:00
*
*/
typedef struct {
2021-07-15 03:43:46 +00:00
size_t NowServing;
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);
2021-07-04 20:47:09 +00:00
#ifdef __cplusplus
}
#endif