mirror of
https://github.com/riscv-software-src/opensbi.git
synced 2025-08-24 15:31:22 +01:00

Replace the test-and-set spinlock implementation with ticket locks in order to get fairness (in form of FIFO order). The implementation uses a 32-bit wide struct, which consists of two 16-bit counters (owner and next). This is inspired by similar spinlock implementations on other architectures. This allows that the code works for both, RV32 and RV64. Signed-off-by: Christoph Muellner <cmuellner@linux.com> Reviewed-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Xiang W <wxjstz@126.com>
46 lines
843 B
C
46 lines
843 B
C
/*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*
|
|
* Copyright (c) 2019 Western Digital Corporation or its affiliates.
|
|
* Copyright (c) 2021 Christoph Müllner <cmuellner@linux.com>
|
|
*/
|
|
|
|
#ifndef __RISCV_LOCKS_H__
|
|
#define __RISCV_LOCKS_H__
|
|
|
|
#include <sbi/sbi_types.h>
|
|
|
|
#define TICKET_SHIFT 16
|
|
|
|
typedef struct {
|
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
u16 next;
|
|
u16 owner;
|
|
#else
|
|
u16 owner;
|
|
u16 next;
|
|
#endif
|
|
} __aligned(4) spinlock_t;
|
|
|
|
#define __SPIN_LOCK_UNLOCKED \
|
|
(spinlock_t) { 0, 0 }
|
|
|
|
#define SPIN_LOCK_INIT(x) \
|
|
x = __SPIN_LOCK_UNLOCKED
|
|
|
|
#define SPIN_LOCK_INITIALIZER \
|
|
__SPIN_LOCK_UNLOCKED
|
|
|
|
#define DEFINE_SPIN_LOCK(x) \
|
|
spinlock_t SPIN_LOCK_INIT(x)
|
|
|
|
int spin_lock_check(spinlock_t *lock);
|
|
|
|
int spin_trylock(spinlock_t *lock);
|
|
|
|
void spin_lock(spinlock_t *lock);
|
|
|
|
void spin_unlock(spinlock_t *lock);
|
|
|
|
#endif
|