libmetal  latest
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 
28 };
29 
31 #define METAL_SPINLOCK_INIT {ATOMIC_FLAG_INIT}
32 
37 static inline void metal_spinlock_init(struct metal_spinlock *slock)
38 {
39  atomic_flag_clear(&slock->v);
40 }
41 
47 static inline void metal_spinlock_acquire(struct metal_spinlock *slock)
48 {
49  while (atomic_flag_test_and_set(&slock->v)) {
51  }
52 }
53 
59 static inline void metal_spinlock_release(struct metal_spinlock *slock)
60 {
61  atomic_flag_clear(&slock->v);
62 }
63 
66 #ifdef __cplusplus
67 }
68 #endif
69 
70 #endif /* __METAL_SPINLOCK__H__ */
#define atomic_flag_test_and_set(FLAG)
Definition: atomic.h:44
#define atomic_flag_clear(FLAG)
Definition: atomic.h:48
int atomic_flag
Definition: atomic.h:19
static void metal_spinlock_init(struct metal_spinlock *slock)
Initialize a libmetal spinlock.
Definition: spinlock.h:37
static void metal_spinlock_release(struct metal_spinlock *slock)
Release a previously acquired spinlock.
Definition: spinlock.h:59
static void metal_spinlock_acquire(struct metal_spinlock *slock)
Acquire a spinlock.
Definition: spinlock.h:47
#define metal_cpu_yield()
Definition: cpu.h:15
Definition: spinlock.h:26
atomic_flag v
Definition: spinlock.h:27