blob: cc6a7f086b1665b65fb4ffac126ead969ad7c7bf [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>
20
Florin Coras1f9600e2018-01-23 18:17:44 -080021#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) \
29do { \
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) \
35do { \
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
jaszha035cdde5c2019-07-11 20:47:24 +000045#define CLIB_SPINLOCK_IS_LOCKED(_p) (*(_p))->lock
46#define CLIB_SPINLOCK_ASSERT_LOCKED(_p) ASSERT(CLIB_SPINLOCK_IS_LOCKED((_p)))
47
Damjan Marion1927da22017-03-27 17:08:20 +020048typedef struct
49{
50 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
51 u32 lock;
52#if CLIB_DEBUG > 0
53 pid_t pid;
Damjan Marionf55f9b82017-05-10 21:06:28 +020054 uword thread_index;
Damjan Marion1927da22017-03-27 17:08:20 +020055 void *frame_address;
56#endif
57} *clib_spinlock_t;
58
59static inline void
60clib_spinlock_init (clib_spinlock_t * p)
61{
62 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040063 clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion1927da22017-03-27 17:08:20 +020064}
65
66static inline void
67clib_spinlock_free (clib_spinlock_t * p)
68{
69 if (*p)
70 {
71 clib_mem_free ((void *) *p);
72 *p = 0;
73 }
74}
75
76static_always_inline void
77clib_spinlock_lock (clib_spinlock_t * p)
78{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +000079 while (clib_atomic_test_and_set (&(*p)->lock))
Florin Coras1f9600e2018-01-23 18:17:44 -080080 CLIB_PAUSE ();
81 CLIB_LOCK_DBG (p);
Damjan Marion1927da22017-03-27 17:08:20 +020082}
83
84static_always_inline void
85clib_spinlock_lock_if_init (clib_spinlock_t * p)
86{
87 if (PREDICT_FALSE (*p != 0))
88 clib_spinlock_lock (p);
89}
90
91static_always_inline void
92clib_spinlock_unlock (clib_spinlock_t * p)
93{
Florin Coras1f9600e2018-01-23 18:17:44 -080094 CLIB_LOCK_DBG_CLEAR (p);
jaszha03f25e7cf2019-06-12 16:01:19 -050095 /* Make sure all reads/writes are complete before releasing the lock */
96 clib_atomic_release (&(*p)->lock);
Damjan Marion1927da22017-03-27 17:08:20 +020097}
98
99static_always_inline void
100clib_spinlock_unlock_if_init (clib_spinlock_t * p)
101{
102 if (PREDICT_FALSE (*p != 0))
103 clib_spinlock_unlock (p);
104}
105
Florin Coras1f9600e2018-01-23 18:17:44 -0800106/*
107 * Readers-Writer Lock
108 */
109
110typedef struct clib_rw_lock_
111{
112 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
113 volatile u32 n_readers;
Florin Corasa332c462018-01-31 06:52:17 -0800114 volatile u32 n_readers_lock;
Florin Coras1f9600e2018-01-23 18:17:44 -0800115 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
123always_inline void
124clib_rwlock_init (clib_rwlock_t * p)
125{
126 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400127 clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
Florin Coras1f9600e2018-01-23 18:17:44 -0800128}
129
130always_inline void
131clib_rwlock_free (clib_rwlock_t * p)
132{
133 if (*p)
134 {
135 clib_mem_free ((void *) *p);
136 *p = 0;
137 }
138}
139
140always_inline void
141clib_rwlock_reader_lock (clib_rwlock_t * p)
142{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000143 while (clib_atomic_test_and_set (&(*p)->n_readers_lock))
Florin Corasa332c462018-01-31 06:52:17 -0800144 CLIB_PAUSE ();
145
146 (*p)->n_readers += 1;
147 if ((*p)->n_readers == 1)
Florin Coras1f9600e2018-01-23 18:17:44 -0800148 {
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000149 while (clib_atomic_test_and_set (&(*p)->writer_lock))
Florin Coras1f9600e2018-01-23 18:17:44 -0800150 CLIB_PAUSE ();
151 }
jaszha03f25e7cf2019-06-12 16:01:19 -0500152 clib_atomic_release (&(*p)->n_readers_lock);
Florin Coras1f9600e2018-01-23 18:17:44 -0800153 CLIB_LOCK_DBG (p);
154}
155
156always_inline void
157clib_rwlock_reader_unlock (clib_rwlock_t * p)
158{
Florin Corasa332c462018-01-31 06:52:17 -0800159 ASSERT ((*p)->n_readers > 0);
Florin Coras1f9600e2018-01-23 18:17:44 -0800160 CLIB_LOCK_DBG_CLEAR (p);
Florin Corasa332c462018-01-31 06:52:17 -0800161
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000162 while (clib_atomic_test_and_set (&(*p)->n_readers_lock))
Florin Corasa332c462018-01-31 06:52:17 -0800163 CLIB_PAUSE ();
164
165 (*p)->n_readers -= 1;
166 if ((*p)->n_readers == 0)
Florin Coras1f9600e2018-01-23 18:17:44 -0800167 {
jaszha03f25e7cf2019-06-12 16:01:19 -0500168 clib_atomic_release (&(*p)->writer_lock);
Florin Coras1f9600e2018-01-23 18:17:44 -0800169 }
jaszha03f25e7cf2019-06-12 16:01:19 -0500170 clib_atomic_release (&(*p)->n_readers_lock);
Florin Coras1f9600e2018-01-23 18:17:44 -0800171}
172
173always_inline void
174clib_rwlock_writer_lock (clib_rwlock_t * p)
175{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000176 while (clib_atomic_test_and_set (&(*p)->writer_lock))
Florin Coras1f9600e2018-01-23 18:17:44 -0800177 CLIB_PAUSE ();
178 CLIB_LOCK_DBG (p);
179}
180
181always_inline void
182clib_rwlock_writer_unlock (clib_rwlock_t * p)
183{
184 CLIB_LOCK_DBG_CLEAR (p);
jaszha03f25e7cf2019-06-12 16:01:19 -0500185 clib_atomic_release (&(*p)->writer_lock);
Florin Coras1f9600e2018-01-23 18:17:44 -0800186}
187
Damjan Marion1927da22017-03-27 17:08:20 +0200188#endif
189
190/*
191 * fd.io coding-style-patch-verification: ON
192 *
193 * Local Variables:
194 * eval: (c-set-style "gnu")
195 * End:
196 */