1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Arm Limited
5 #ifndef _RTE_TICKETLOCK_H_
6 #define _RTE_TICKETLOCK_H_
13 * This file defines an API for ticket locks, which give each waiting
14 * thread a ticket and take the lock one by one, first come, first
17 * All locks must be initialised before use, and only initialised once.
25 #include <rte_common.h>
26 #include <rte_lcore.h>
27 #include <rte_pause.h>
30 * The rte_ticketlock_t type.
41 * A static ticketlock initializer.
43 #define RTE_TICKETLOCK_INITIALIZER { 0 }
46 * Initialize the ticketlock to an unlocked state.
49 * A pointer to the ticketlock.
52 rte_ticketlock_init(rte_ticketlock_t *tl)
54 __atomic_store_n(&tl->tickets, 0, __ATOMIC_RELAXED);
58 * Take the ticketlock.
61 * A pointer to the ticketlock.
64 rte_ticketlock_lock(rte_ticketlock_t *tl)
66 uint16_t me = __atomic_fetch_add(&tl->s.next, 1, __ATOMIC_RELAXED);
67 rte_wait_until_equal_16(&tl->s.current, me, __ATOMIC_ACQUIRE);
71 * Release the ticketlock.
74 * A pointer to the ticketlock.
77 rte_ticketlock_unlock(rte_ticketlock_t *tl)
79 uint16_t i = __atomic_load_n(&tl->s.current, __ATOMIC_RELAXED);
80 __atomic_store_n(&tl->s.current, i + 1, __ATOMIC_RELEASE);
84 * Try to take the lock.
87 * A pointer to the ticketlock.
89 * 1 if the lock is successfully taken; 0 otherwise.
92 rte_ticketlock_trylock(rte_ticketlock_t *tl)
94 rte_ticketlock_t oldl, newl;
95 oldl.tickets = __atomic_load_n(&tl->tickets, __ATOMIC_RELAXED);
96 newl.tickets = oldl.tickets;
98 if (oldl.s.next == oldl.s.current) {
99 if (__atomic_compare_exchange_n(&tl->tickets, &oldl.tickets,
100 newl.tickets, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
108 * Test if the lock is taken.
111 * A pointer to the ticketlock.
113 * 1 if the lock is currently taken; 0 otherwise.
116 rte_ticketlock_is_locked(rte_ticketlock_t *tl)
118 rte_ticketlock_t tic;
119 tic.tickets = __atomic_load_n(&tl->tickets, __ATOMIC_ACQUIRE);
120 return (tic.s.current != tic.s.next);
124 * The rte_ticketlock_recursive_t type.
126 #define TICKET_LOCK_INVALID_ID -1
129 rte_ticketlock_t tl; /**< the actual ticketlock */
130 int user; /**< core id using lock, TICKET_LOCK_INVALID_ID for unused */
131 unsigned int count; /**< count of time this lock has been called */
132 } rte_ticketlock_recursive_t;
135 * A static recursive ticketlock initializer.
137 #define RTE_TICKETLOCK_RECURSIVE_INITIALIZER {RTE_TICKETLOCK_INITIALIZER, \
138 TICKET_LOCK_INVALID_ID, 0}
141 * Initialize the recursive ticketlock to an unlocked state.
144 * A pointer to the recursive ticketlock.
147 rte_ticketlock_recursive_init(rte_ticketlock_recursive_t *tlr)
149 rte_ticketlock_init(&tlr->tl);
150 __atomic_store_n(&tlr->user, TICKET_LOCK_INVALID_ID, __ATOMIC_RELAXED);
155 * Take the recursive ticketlock.
158 * A pointer to the recursive ticketlock.
161 rte_ticketlock_recursive_lock(rte_ticketlock_recursive_t *tlr)
163 int id = rte_gettid();
165 if (__atomic_load_n(&tlr->user, __ATOMIC_RELAXED) != id) {
166 rte_ticketlock_lock(&tlr->tl);
167 __atomic_store_n(&tlr->user, id, __ATOMIC_RELAXED);
173 * Release the recursive ticketlock.
176 * A pointer to the recursive ticketlock.
179 rte_ticketlock_recursive_unlock(rte_ticketlock_recursive_t *tlr)
181 if (--(tlr->count) == 0) {
182 __atomic_store_n(&tlr->user, TICKET_LOCK_INVALID_ID,
184 rte_ticketlock_unlock(&tlr->tl);
189 * Try to take the recursive lock.
192 * A pointer to the recursive ticketlock.
194 * 1 if the lock is successfully taken; 0 otherwise.
197 rte_ticketlock_recursive_trylock(rte_ticketlock_recursive_t *tlr)
199 int id = rte_gettid();
201 if (__atomic_load_n(&tlr->user, __ATOMIC_RELAXED) != id) {
202 if (rte_ticketlock_trylock(&tlr->tl) == 0)
204 __atomic_store_n(&tlr->user, id, __ATOMIC_RELAXED);
214 #endif /* _RTE_TICKETLOCK_H_ */