blob: b7b3d00a90530535f93b43abecefd33191426f52 [file] [log] [blame]
Damjan Marion1927da22017-03-27 17:08:20 +02001/*
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>
jaszha03fb1ccc72019-06-26 17:06:59 -050020#include <vppinfra/atomics.h>
Damjan Marion1927da22017-03-27 17:08:20 +020021
Florin Coras1f9600e2018-01-23 18:17:44 -080022#if __x86_64__
23#define CLIB_PAUSE() __builtin_ia32_pause ()
jaszha0318512b02019-09-27 11:48:23 -050024#elif defined (__aarch64__) || defined (__arm__)
25#define CLIB_PAUSE() __asm__ ("yield")
Florin Coras1f9600e2018-01-23 18:17:44 -080026#else
27#define CLIB_PAUSE()
28#endif
29
30#if CLIB_DEBUG > 1
31#define CLIB_LOCK_DBG(_p) \
32do { \
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) \
38do { \
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
jaszha035cdde5c2019-07-11 20:47:24 +000048#define CLIB_SPINLOCK_IS_LOCKED(_p) (*(_p))->lock
49#define CLIB_SPINLOCK_ASSERT_LOCKED(_p) ASSERT(CLIB_SPINLOCK_IS_LOCKED((_p)))
50
Dave Barach402606c2020-10-19 16:05:07 -040051struct clib_spinlock_s
Damjan Marion1927da22017-03-27 17:08:20 +020052{
53 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
54 u32 lock;
55#if CLIB_DEBUG > 0
56 pid_t pid;
Damjan Marionf55f9b82017-05-10 21:06:28 +020057 uword thread_index;
Damjan Marion1927da22017-03-27 17:08:20 +020058 void *frame_address;
59#endif
Dave Barach402606c2020-10-19 16:05:07 -040060};
61
62typedef struct clib_spinlock_s *clib_spinlock_t;
Damjan Marion1927da22017-03-27 17:08:20 +020063
64static inline void
65clib_spinlock_init (clib_spinlock_t * p)
66{
67 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040068 clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion1927da22017-03-27 17:08:20 +020069}
70
71static inline void
72clib_spinlock_free (clib_spinlock_t * p)
73{
74 if (*p)
75 {
76 clib_mem_free ((void *) *p);
77 *p = 0;
78 }
79}
80
81static_always_inline void
82clib_spinlock_lock (clib_spinlock_t * p)
83{
jaszha03fb1ccc72019-06-26 17:06:59 -050084 u32 free = 0;
85 while (!clib_atomic_cmp_and_swap_acq_relax_n (&(*p)->lock, &free, 1, 0))
86 {
87 /* atomic load limits number of compare_exchange executions */
88 while (clib_atomic_load_relax_n (&(*p)->lock))
89 CLIB_PAUSE ();
90 /* on failure, compare_exchange writes (*p)->lock into free */
91 free = 0;
92 }
Florin Coras1f9600e2018-01-23 18:17:44 -080093 CLIB_LOCK_DBG (p);
Damjan Marion1927da22017-03-27 17:08:20 +020094}
95
Benoît Ganneb6b484d2020-09-15 10:58:07 +020096static_always_inline int
97clib_spinlock_trylock (clib_spinlock_t * p)
98{
99 if (PREDICT_FALSE (CLIB_SPINLOCK_IS_LOCKED (p)))
100 return 0;
101 clib_spinlock_lock (p);
102 return 1;
103}
104
Damjan Marion1927da22017-03-27 17:08:20 +0200105static_always_inline void
106clib_spinlock_lock_if_init (clib_spinlock_t * p)
107{
108 if (PREDICT_FALSE (*p != 0))
109 clib_spinlock_lock (p);
110}
111
Benoît Ganneb6b484d2020-09-15 10:58:07 +0200112static_always_inline int
113clib_spinlock_trylock_if_init (clib_spinlock_t * p)
114{
115 if (PREDICT_FALSE (*p != 0))
116 return clib_spinlock_trylock (p);
117 return 1;
118}
119
Damjan Marion1927da22017-03-27 17:08:20 +0200120static_always_inline void
121clib_spinlock_unlock (clib_spinlock_t * p)
122{
Florin Coras1f9600e2018-01-23 18:17:44 -0800123 CLIB_LOCK_DBG_CLEAR (p);
jaszha03f25e7cf2019-06-12 16:01:19 -0500124 /* Make sure all reads/writes are complete before releasing the lock */
125 clib_atomic_release (&(*p)->lock);
Damjan Marion1927da22017-03-27 17:08:20 +0200126}
127
128static_always_inline void
129clib_spinlock_unlock_if_init (clib_spinlock_t * p)
130{
131 if (PREDICT_FALSE (*p != 0))
132 clib_spinlock_unlock (p);
133}
134
Florin Coras1f9600e2018-01-23 18:17:44 -0800135/*
136 * Readers-Writer Lock
137 */
138
139typedef struct clib_rw_lock_
140{
141 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
jaszha0330aaf972019-07-01 17:08:57 -0500142 /* -1 when W lock held, > 0 when R lock held */
143 volatile i32 rw_cnt;
Florin Coras1f9600e2018-01-23 18:17:44 -0800144#if CLIB_DEBUG > 0
145 pid_t pid;
146 uword thread_index;
147 void *frame_address;
148#endif
149} *clib_rwlock_t;
150
151always_inline void
152clib_rwlock_init (clib_rwlock_t * p)
153{
154 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400155 clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
Florin Coras1f9600e2018-01-23 18:17:44 -0800156}
157
158always_inline void
159clib_rwlock_free (clib_rwlock_t * p)
160{
161 if (*p)
162 {
163 clib_mem_free ((void *) *p);
164 *p = 0;
165 }
166}
167
168always_inline void
169clib_rwlock_reader_lock (clib_rwlock_t * p)
170{
jaszha0330aaf972019-07-01 17:08:57 -0500171 i32 cnt;
172 do
Florin Coras1f9600e2018-01-23 18:17:44 -0800173 {
jaszha0330aaf972019-07-01 17:08:57 -0500174 /* rwlock held by a writer */
175 while ((cnt = clib_atomic_load_relax_n (&(*p)->rw_cnt)) < 0)
Florin Coras1f9600e2018-01-23 18:17:44 -0800176 CLIB_PAUSE ();
177 }
jaszha0330aaf972019-07-01 17:08:57 -0500178 while (!clib_atomic_cmp_and_swap_acq_relax_n
179 (&(*p)->rw_cnt, &cnt, cnt + 1, 1));
Florin Coras1f9600e2018-01-23 18:17:44 -0800180 CLIB_LOCK_DBG (p);
181}
182
183always_inline void
184clib_rwlock_reader_unlock (clib_rwlock_t * p)
185{
jaszha0330aaf972019-07-01 17:08:57 -0500186 ASSERT ((*p)->rw_cnt > 0);
Florin Coras1f9600e2018-01-23 18:17:44 -0800187 CLIB_LOCK_DBG_CLEAR (p);
jaszha0330aaf972019-07-01 17:08:57 -0500188 clib_atomic_fetch_sub_rel (&(*p)->rw_cnt, 1);
Florin Coras1f9600e2018-01-23 18:17:44 -0800189}
190
191always_inline void
192clib_rwlock_writer_lock (clib_rwlock_t * p)
193{
jaszha0330aaf972019-07-01 17:08:57 -0500194 i32 cnt = 0;
195 do
196 {
197 /* rwlock held by writer or reader(s) */
198 while ((cnt = clib_atomic_load_relax_n (&(*p)->rw_cnt)) != 0)
199 CLIB_PAUSE ();
200 }
201 while (!clib_atomic_cmp_and_swap_acq_relax_n (&(*p)->rw_cnt, &cnt, -1, 1));
Florin Coras1f9600e2018-01-23 18:17:44 -0800202 CLIB_LOCK_DBG (p);
203}
204
205always_inline void
206clib_rwlock_writer_unlock (clib_rwlock_t * p)
207{
208 CLIB_LOCK_DBG_CLEAR (p);
jaszha0330aaf972019-07-01 17:08:57 -0500209 clib_atomic_release (&(*p)->rw_cnt);
Florin Coras1f9600e2018-01-23 18:17:44 -0800210}
211
Damjan Marion1927da22017-03-27 17:08:20 +0200212#endif
213
214/*
215 * fd.io coding-style-patch-verification: ON
216 *
217 * Local Variables:
218 * eval: (c-set-style "gnu")
219 * End:
220 */