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> |
| 20 | |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 21 | #if __x86_64__ |
| 22 | #define CLIB_PAUSE() __builtin_ia32_pause () |
| 23 | #else |
| 24 | #define CLIB_PAUSE() |
| 25 | #endif |
| 26 | |
| 27 | #if CLIB_DEBUG > 1 |
| 28 | #define CLIB_LOCK_DBG(_p) \ |
| 29 | do { \ |
| 30 | (*_p)->frame_address = __builtin_frame_address (0); \ |
| 31 | (*_p)->pid = getpid (); \ |
| 32 | (*_p)->thread_index = os_get_thread_index (); \ |
| 33 | } while (0) |
| 34 | #define CLIB_LOCK_DBG_CLEAR(_p) \ |
| 35 | do { \ |
| 36 | (*_p)->frame_address = 0; \ |
| 37 | (*_p)->pid = 0; \ |
| 38 | (*_p)->thread_index = 0; \ |
| 39 | } while (0) |
| 40 | #else |
| 41 | #define CLIB_LOCK_DBG(_p) |
| 42 | #define CLIB_LOCK_DBG_CLEAR(_p) |
| 43 | #endif |
| 44 | |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame^] | 45 | #define CLIB_SPINLOCK_IS_LOCKED(_p) (*(_p))->lock |
| 46 | #define CLIB_SPINLOCK_ASSERT_LOCKED(_p) ASSERT(CLIB_SPINLOCK_IS_LOCKED((_p))) |
| 47 | |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 48 | typedef struct |
| 49 | { |
| 50 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 51 | u32 lock; |
| 52 | #if CLIB_DEBUG > 0 |
| 53 | pid_t pid; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 54 | uword thread_index; |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 55 | void *frame_address; |
| 56 | #endif |
| 57 | } *clib_spinlock_t; |
| 58 | |
| 59 | static inline void |
| 60 | clib_spinlock_init (clib_spinlock_t * p) |
| 61 | { |
| 62 | *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] | 63 | clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static inline void |
| 67 | clib_spinlock_free (clib_spinlock_t * p) |
| 68 | { |
| 69 | if (*p) |
| 70 | { |
| 71 | clib_mem_free ((void *) *p); |
| 72 | *p = 0; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static_always_inline void |
| 77 | clib_spinlock_lock (clib_spinlock_t * p) |
| 78 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 79 | while (clib_atomic_test_and_set (&(*p)->lock)) |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 80 | CLIB_PAUSE (); |
| 81 | CLIB_LOCK_DBG (p); |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static_always_inline void |
| 85 | clib_spinlock_lock_if_init (clib_spinlock_t * p) |
| 86 | { |
| 87 | if (PREDICT_FALSE (*p != 0)) |
| 88 | clib_spinlock_lock (p); |
| 89 | } |
| 90 | |
| 91 | static_always_inline void |
| 92 | clib_spinlock_unlock (clib_spinlock_t * p) |
| 93 | { |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 94 | CLIB_LOCK_DBG_CLEAR (p); |
jaszha03 | f25e7cf | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 95 | /* Make sure all reads/writes are complete before releasing the lock */ |
| 96 | clib_atomic_release (&(*p)->lock); |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static_always_inline void |
| 100 | clib_spinlock_unlock_if_init (clib_spinlock_t * p) |
| 101 | { |
| 102 | if (PREDICT_FALSE (*p != 0)) |
| 103 | clib_spinlock_unlock (p); |
| 104 | } |
| 105 | |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 106 | /* |
| 107 | * Readers-Writer Lock |
| 108 | */ |
| 109 | |
| 110 | typedef struct clib_rw_lock_ |
| 111 | { |
| 112 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 113 | volatile u32 n_readers; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 114 | volatile u32 n_readers_lock; |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 115 | volatile u32 writer_lock; |
| 116 | #if CLIB_DEBUG > 0 |
| 117 | pid_t pid; |
| 118 | uword thread_index; |
| 119 | void *frame_address; |
| 120 | #endif |
| 121 | } *clib_rwlock_t; |
| 122 | |
| 123 | always_inline void |
| 124 | clib_rwlock_init (clib_rwlock_t * p) |
| 125 | { |
| 126 | *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] | 127 | clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | always_inline void |
| 131 | clib_rwlock_free (clib_rwlock_t * p) |
| 132 | { |
| 133 | if (*p) |
| 134 | { |
| 135 | clib_mem_free ((void *) *p); |
| 136 | *p = 0; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | always_inline void |
| 141 | clib_rwlock_reader_lock (clib_rwlock_t * p) |
| 142 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 143 | while (clib_atomic_test_and_set (&(*p)->n_readers_lock)) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 144 | CLIB_PAUSE (); |
| 145 | |
| 146 | (*p)->n_readers += 1; |
| 147 | if ((*p)->n_readers == 1) |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 148 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 149 | while (clib_atomic_test_and_set (&(*p)->writer_lock)) |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 150 | CLIB_PAUSE (); |
| 151 | } |
jaszha03 | f25e7cf | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 152 | clib_atomic_release (&(*p)->n_readers_lock); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 153 | CLIB_LOCK_DBG (p); |
| 154 | } |
| 155 | |
| 156 | always_inline void |
| 157 | clib_rwlock_reader_unlock (clib_rwlock_t * p) |
| 158 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 159 | ASSERT ((*p)->n_readers > 0); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 160 | CLIB_LOCK_DBG_CLEAR (p); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 161 | |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 162 | while (clib_atomic_test_and_set (&(*p)->n_readers_lock)) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 163 | CLIB_PAUSE (); |
| 164 | |
| 165 | (*p)->n_readers -= 1; |
| 166 | if ((*p)->n_readers == 0) |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 167 | { |
jaszha03 | f25e7cf | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 168 | clib_atomic_release (&(*p)->writer_lock); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 169 | } |
jaszha03 | f25e7cf | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 170 | clib_atomic_release (&(*p)->n_readers_lock); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | always_inline void |
| 174 | clib_rwlock_writer_lock (clib_rwlock_t * p) |
| 175 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 176 | while (clib_atomic_test_and_set (&(*p)->writer_lock)) |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 177 | CLIB_PAUSE (); |
| 178 | CLIB_LOCK_DBG (p); |
| 179 | } |
| 180 | |
| 181 | always_inline void |
| 182 | clib_rwlock_writer_unlock (clib_rwlock_t * p) |
| 183 | { |
| 184 | CLIB_LOCK_DBG_CLEAR (p); |
jaszha03 | f25e7cf | 2019-06-12 16:01:19 -0500 | [diff] [blame] | 185 | clib_atomic_release (&(*p)->writer_lock); |
Florin Coras | 1f9600e | 2018-01-23 18:17:44 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 188 | #endif |
| 189 | |
| 190 | /* |
| 191 | * fd.io coding-style-patch-verification: ON |
| 192 | * |
| 193 | * Local Variables: |
| 194 | * eval: (c-set-style "gnu") |
| 195 | * End: |
| 196 | */ |