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