Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef included_clib_lock_h |
| 17 | #define included_clib_lock_h |
| 18 | |
| 19 | #include <vppinfra/clib.h> |
jaszha03 | fb1ccc7 | 2019-06-26 17:06:59 -0500 | [diff] [blame] | 20 | #include <vppinfra/atomics.h> |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 21 | |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 22 | #if __x86_64__ |
| 23 | #define CLIB_PAUSE() __builtin_ia32_pause () |
jaszha03 | 18512b0 | 2019-09-27 11:48:23 -0500 | [diff] [blame] | 24 | #elif defined (__aarch64__) || defined (__arm__) |
| 25 | #define CLIB_PAUSE() __asm__ ("yield") |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 26 | #else |
| 27 | #define CLIB_PAUSE() |
| 28 | #endif |
| 29 | |
| 30 | #if CLIB_DEBUG > 1 |
| 31 | #define CLIB_LOCK_DBG(_p) \ |
| 32 | do { \ |
| 33 | (*_p)->frame_address = __builtin_frame_address (0); \ |
| 34 | (*_p)->pid = getpid (); \ |
| 35 | (*_p)->thread_index = os_get_thread_index (); \ |
| 36 | } while (0) |
| 37 | #define CLIB_LOCK_DBG_CLEAR(_p) \ |
| 38 | do { \ |
| 39 | (*_p)->frame_address = 0; \ |
| 40 | (*_p)->pid = 0; \ |
| 41 | (*_p)->thread_index = 0; \ |
| 42 | } while (0) |
| 43 | #else |
| 44 | #define CLIB_LOCK_DBG(_p) |
| 45 | #define CLIB_LOCK_DBG_CLEAR(_p) |
| 46 | #endif |
| 47 | |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame] | 48 | #define CLIB_SPINLOCK_IS_LOCKED(_p) (*(_p))->lock |
| 49 | #define CLIB_SPINLOCK_ASSERT_LOCKED(_p) ASSERT(CLIB_SPINLOCK_IS_LOCKED((_p))) |
| 50 | |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 51 | typedef struct |
| 52 | { |
| 53 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 54 | u32 lock; |
| 55 | #if CLIB_DEBUG > 0 |
| 56 | pid_t pid; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 57 | uword thread_index; |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 58 | void *frame_address; |
| 59 | #endif |
| 60 | } *clib_spinlock_t; |
| 61 | |
| 62 | static inline void |
| 63 | clib_spinlock_init (clib_spinlock_t * p) |
| 64 | { |
| 65 | *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 66 | clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static inline void |
| 70 | clib_spinlock_free (clib_spinlock_t * p) |
| 71 | { |
| 72 | if (*p) |
| 73 | { |
| 74 | clib_mem_free ((void *) *p); |
| 75 | *p = 0; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static_always_inline void |
| 80 | clib_spinlock_lock (clib_spinlock_t * p) |
| 81 | { |
jaszha03 | fb1ccc7 | 2019-06-26 17:06:59 -0500 | [diff] [blame] | 82 | u32 free = 0; |
| 83 | while (!clib_atomic_cmp_and_swap_acq_relax_n (&(*p)->lock, &free, 1, 0)) |
| 84 | { |
| 85 | /* atomic load limits number of compare_exchange executions */ |
| 86 | while (clib_atomic_load_relax_n (&(*p)->lock)) |
| 87 | CLIB_PAUSE (); |
| 88 | /* on failure, compare_exchange writes (*p)->lock into free */ |
| 89 | free = 0; |
| 90 | } |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 91 | CLIB_LOCK_DBG (p); |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static_always_inline void |
| 95 | clib_spinlock_lock_if_init (clib_spinlock_t * p) |
| 96 | { |
| 97 | if (PREDICT_FALSE (*p != 0)) |
| 98 | clib_spinlock_lock (p); |
| 99 | } |
| 100 | |
| 101 | static_always_inline void |
| 102 | clib_spinlock_unlock (clib_spinlock_t * p) |
| 103 | { |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 104 | CLIB_LOCK_DBG_CLEAR (p); |
jaszha03 | f25e7cf | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 105 | /* Make sure all reads/writes are complete before releasing the lock */ |
| 106 | clib_atomic_release (&(*p)->lock); |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static_always_inline void |
| 110 | clib_spinlock_unlock_if_init (clib_spinlock_t * p) |
| 111 | { |
| 112 | if (PREDICT_FALSE (*p != 0)) |
| 113 | clib_spinlock_unlock (p); |
| 114 | } |
| 115 | |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 116 | /* |
| 117 | * Readers-Writer Lock |
| 118 | */ |
| 119 | |
| 120 | typedef struct clib_rw_lock_ |
| 121 | { |
| 122 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
jaszha03 | 30aaf97 | 2019-07-01 17:08:57 -0500 | [diff] [blame] | 123 | /* -1 when W lock held, > 0 when R lock held */ |
| 124 | volatile i32 rw_cnt; |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 125 | #if CLIB_DEBUG > 0 |
| 126 | pid_t pid; |
| 127 | uword thread_index; |
| 128 | void *frame_address; |
| 129 | #endif |
| 130 | } *clib_rwlock_t; |
| 131 | |
| 132 | always_inline void |
| 133 | clib_rwlock_init (clib_rwlock_t * p) |
| 134 | { |
| 135 | *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 136 | clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | always_inline void |
| 140 | clib_rwlock_free (clib_rwlock_t * p) |
| 141 | { |
| 142 | if (*p) |
| 143 | { |
| 144 | clib_mem_free ((void *) *p); |
| 145 | *p = 0; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | always_inline void |
| 150 | clib_rwlock_reader_lock (clib_rwlock_t * p) |
| 151 | { |
jaszha03 | 30aaf97 | 2019-07-01 17:08:57 -0500 | [diff] [blame] | 152 | i32 cnt; |
| 153 | do |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 154 | { |
jaszha03 | 30aaf97 | 2019-07-01 17:08:57 -0500 | [diff] [blame] | 155 | /* rwlock held by a writer */ |
| 156 | while ((cnt = clib_atomic_load_relax_n (&(*p)->rw_cnt)) < 0) |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 157 | CLIB_PAUSE (); |
| 158 | } |
jaszha03 | 30aaf97 | 2019-07-01 17:08:57 -0500 | [diff] [blame] | 159 | while (!clib_atomic_cmp_and_swap_acq_relax_n |
| 160 | (&(*p)->rw_cnt, &cnt, cnt + 1, 1)); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 161 | CLIB_LOCK_DBG (p); |
| 162 | } |
| 163 | |
| 164 | always_inline void |
| 165 | clib_rwlock_reader_unlock (clib_rwlock_t * p) |
| 166 | { |
jaszha03 | 30aaf97 | 2019-07-01 17:08:57 -0500 | [diff] [blame] | 167 | ASSERT ((*p)->rw_cnt > 0); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 168 | CLIB_LOCK_DBG_CLEAR (p); |
jaszha03 | 30aaf97 | 2019-07-01 17:08:57 -0500 | [diff] [blame] | 169 | clib_atomic_fetch_sub_rel (&(*p)->rw_cnt, 1); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | always_inline void |
| 173 | clib_rwlock_writer_lock (clib_rwlock_t * p) |
| 174 | { |
jaszha03 | 30aaf97 | 2019-07-01 17:08:57 -0500 | [diff] [blame] | 175 | i32 cnt = 0; |
| 176 | do |
| 177 | { |
| 178 | /* rwlock held by writer or reader(s) */ |
| 179 | while ((cnt = clib_atomic_load_relax_n (&(*p)->rw_cnt)) != 0) |
| 180 | CLIB_PAUSE (); |
| 181 | } |
| 182 | while (!clib_atomic_cmp_and_swap_acq_relax_n (&(*p)->rw_cnt, &cnt, -1, 1)); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 183 | CLIB_LOCK_DBG (p); |
| 184 | } |
| 185 | |
| 186 | always_inline void |
| 187 | clib_rwlock_writer_unlock (clib_rwlock_t * p) |
| 188 | { |
| 189 | CLIB_LOCK_DBG_CLEAR (p); |
jaszha03 | 30aaf97 | 2019-07-01 17:08:57 -0500 | [diff] [blame] | 190 | clib_atomic_release (&(*p)->rw_cnt); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 193 | #endif |
| 194 | |
| 195 | /* |
| 196 | * fd.io coding-style-patch-verification: ON |
| 197 | * |
| 198 | * Local Variables: |
| 199 | * eval: (c-set-style "gnu") |
| 200 | * End: |
| 201 | */ |