blob: 49e849b1bdd6b65791968f83ba676a88f1feb86b [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 ()
24#else
25#define CLIB_PAUSE()
26#endif
27
28#if CLIB_DEBUG > 1
29#define CLIB_LOCK_DBG(_p) \
30do { \
31 (*_p)->frame_address = __builtin_frame_address (0); \
32 (*_p)->pid = getpid (); \
33 (*_p)->thread_index = os_get_thread_index (); \
34} while (0)
35#define CLIB_LOCK_DBG_CLEAR(_p) \
36do { \
37 (*_p)->frame_address = 0; \
38 (*_p)->pid = 0; \
39 (*_p)->thread_index = 0; \
40} while (0)
41#else
42#define CLIB_LOCK_DBG(_p)
43#define CLIB_LOCK_DBG_CLEAR(_p)
44#endif
45
jaszha035cdde5c2019-07-11 20:47:24 +000046#define CLIB_SPINLOCK_IS_LOCKED(_p) (*(_p))->lock
47#define CLIB_SPINLOCK_ASSERT_LOCKED(_p) ASSERT(CLIB_SPINLOCK_IS_LOCKED((_p)))
48
Damjan Marion1927da22017-03-27 17:08:20 +020049typedef struct
50{
51 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
52 u32 lock;
53#if CLIB_DEBUG > 0
54 pid_t pid;
Damjan Marionf55f9b82017-05-10 21:06:28 +020055 uword thread_index;
Damjan Marion1927da22017-03-27 17:08:20 +020056 void *frame_address;
57#endif
58} *clib_spinlock_t;
59
60static inline void
61clib_spinlock_init (clib_spinlock_t * p)
62{
63 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040064 clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion1927da22017-03-27 17:08:20 +020065}
66
67static inline void
68clib_spinlock_free (clib_spinlock_t * p)
69{
70 if (*p)
71 {
72 clib_mem_free ((void *) *p);
73 *p = 0;
74 }
75}
76
77static_always_inline void
78clib_spinlock_lock (clib_spinlock_t * p)
79{
jaszha03fb1ccc72019-06-26 17:06:59 -050080 u32 free = 0;
81 while (!clib_atomic_cmp_and_swap_acq_relax_n (&(*p)->lock, &free, 1, 0))
82 {
83 /* atomic load limits number of compare_exchange executions */
84 while (clib_atomic_load_relax_n (&(*p)->lock))
85 CLIB_PAUSE ();
86 /* on failure, compare_exchange writes (*p)->lock into free */
87 free = 0;
88 }
Florin Coras1f9600e2018-01-23 18:17:44 -080089 CLIB_LOCK_DBG (p);
Damjan Marion1927da22017-03-27 17:08:20 +020090}
91
92static_always_inline void
93clib_spinlock_lock_if_init (clib_spinlock_t * p)
94{
95 if (PREDICT_FALSE (*p != 0))
96 clib_spinlock_lock (p);
97}
98
99static_always_inline void
100clib_spinlock_unlock (clib_spinlock_t * p)
101{
Florin Coras1f9600e2018-01-23 18:17:44 -0800102 CLIB_LOCK_DBG_CLEAR (p);
jaszha03f25e7cf2019-06-12 16:01:19 -0500103 /* Make sure all reads/writes are complete before releasing the lock */
104 clib_atomic_release (&(*p)->lock);
Damjan Marion1927da22017-03-27 17:08:20 +0200105}
106
107static_always_inline void
108clib_spinlock_unlock_if_init (clib_spinlock_t * p)
109{
110 if (PREDICT_FALSE (*p != 0))
111 clib_spinlock_unlock (p);
112}
113
Florin Coras1f9600e2018-01-23 18:17:44 -0800114/*
115 * Readers-Writer Lock
116 */
117
118typedef struct clib_rw_lock_
119{
120 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
jaszha0330aaf972019-07-01 17:08:57 -0500121 /* -1 when W lock held, > 0 when R lock held */
122 volatile i32 rw_cnt;
Florin Coras1f9600e2018-01-23 18:17:44 -0800123#if CLIB_DEBUG > 0
124 pid_t pid;
125 uword thread_index;
126 void *frame_address;
127#endif
128} *clib_rwlock_t;
129
130always_inline void
131clib_rwlock_init (clib_rwlock_t * p)
132{
133 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400134 clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
Florin Coras1f9600e2018-01-23 18:17:44 -0800135}
136
137always_inline void
138clib_rwlock_free (clib_rwlock_t * p)
139{
140 if (*p)
141 {
142 clib_mem_free ((void *) *p);
143 *p = 0;
144 }
145}
146
147always_inline void
148clib_rwlock_reader_lock (clib_rwlock_t * p)
149{
jaszha0330aaf972019-07-01 17:08:57 -0500150 i32 cnt;
151 do
Florin Coras1f9600e2018-01-23 18:17:44 -0800152 {
jaszha0330aaf972019-07-01 17:08:57 -0500153 /* rwlock held by a writer */
154 while ((cnt = clib_atomic_load_relax_n (&(*p)->rw_cnt)) < 0)
Florin Coras1f9600e2018-01-23 18:17:44 -0800155 CLIB_PAUSE ();
156 }
jaszha0330aaf972019-07-01 17:08:57 -0500157 while (!clib_atomic_cmp_and_swap_acq_relax_n
158 (&(*p)->rw_cnt, &cnt, cnt + 1, 1));
Florin Coras1f9600e2018-01-23 18:17:44 -0800159 CLIB_LOCK_DBG (p);
160}
161
162always_inline void
163clib_rwlock_reader_unlock (clib_rwlock_t * p)
164{
jaszha0330aaf972019-07-01 17:08:57 -0500165 ASSERT ((*p)->rw_cnt > 0);
Florin Coras1f9600e2018-01-23 18:17:44 -0800166 CLIB_LOCK_DBG_CLEAR (p);
jaszha0330aaf972019-07-01 17:08:57 -0500167 clib_atomic_fetch_sub_rel (&(*p)->rw_cnt, 1);
Florin Coras1f9600e2018-01-23 18:17:44 -0800168}
169
170always_inline void
171clib_rwlock_writer_lock (clib_rwlock_t * p)
172{
jaszha0330aaf972019-07-01 17:08:57 -0500173 i32 cnt = 0;
174 do
175 {
176 /* rwlock held by writer or reader(s) */
177 while ((cnt = clib_atomic_load_relax_n (&(*p)->rw_cnt)) != 0)
178 CLIB_PAUSE ();
179 }
180 while (!clib_atomic_cmp_and_swap_acq_relax_n (&(*p)->rw_cnt, &cnt, -1, 1));
Florin Coras1f9600e2018-01-23 18:17:44 -0800181 CLIB_LOCK_DBG (p);
182}
183
184always_inline void
185clib_rwlock_writer_unlock (clib_rwlock_t * p)
186{
187 CLIB_LOCK_DBG_CLEAR (p);
jaszha0330aaf972019-07-01 17:08:57 -0500188 clib_atomic_release (&(*p)->rw_cnt);
Florin Coras1f9600e2018-01-23 18:17:44 -0800189}
190
Damjan Marion1927da22017-03-27 17:08:20 +0200191#endif
192
193/*
194 * fd.io coding-style-patch-verification: ON
195 *
196 * Local Variables:
197 * eval: (c-set-style "gnu")
198 * End:
199 */