libmetal
spinlock.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Xilinx Inc. and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * @file spinlock.h
9  * @brief Spinlock primitives for libmetal.
10  */
11 
12 #ifndef __METAL_SPINLOCK__H__
13 #define __METAL_SPINLOCK__H__
14 
15 #include <metal/atomic.h>
16 #include <metal/cpu.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
26 };
27 
29 #define METAL_SPINLOCK_INIT {ATOMIC_VAR_INIT(0)}
30 
35 static inline void metal_spinlock_init(struct metal_spinlock *slock)
36 {
37  atomic_store(&slock->v, 0);
38 }
39 
45 static inline void metal_spinlock_acquire(struct metal_spinlock *slock)
46 {
47  while (atomic_flag_test_and_set(&slock->v)) {
49  }
50 }
51 
57 static inline void metal_spinlock_release(struct metal_spinlock *slock)
58 {
59  atomic_flag_clear(&slock->v);
60 }
61 
64 #ifdef __cplusplus
65 }
66 #endif
67 
68 #endif /* __METAL_SPINLOCK__H__ */
Definition: spinlock.h:24
static void metal_spinlock_release(struct metal_spinlock *slock)
Release a previously acquired spinlock.
Definition: spinlock.h:57
#define metal_cpu_yield()
Definition: cpu.h:15
static void metal_spinlock_init(struct metal_spinlock *slock)
Initialize a libmetal spinlock.
Definition: spinlock.h:35
static void metal_spinlock_acquire(struct metal_spinlock *slock)
Acquire a spinlock.
Definition: spinlock.h:45
#define atomic_flag_test_and_set(FLAG)
Definition: atomic.h:43
#define atomic_flag_clear(FLAG)
Definition: atomic.h:47
#define atomic_store(OBJ, VAL)
Definition: atomic.h:55
atomic_int v
Definition: spinlock.h:25
int atomic_int
Definition: atomic.h:24