blob: 337c5a3f0ad4bc5ca60cd04818afdeef05c4b8ef [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
Damjan Marion1927da22017-03-27 17:08:20 +020045typedef struct
46{
47 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
48 u32 lock;
49#if CLIB_DEBUG > 0
50 pid_t pid;
Damjan Marionf55f9b82017-05-10 21:06:28 +020051 uword thread_index;
Damjan Marion1927da22017-03-27 17:08:20 +020052 void *frame_address;
53#endif
54} *clib_spinlock_t;
55
56static inline void
57clib_spinlock_init (clib_spinlock_t * p)
58{
59 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040060 clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion1927da22017-03-27 17:08:20 +020061}
62
63static inline void
64clib_spinlock_free (clib_spinlock_t * p)
65{
66 if (*p)
67 {
68 clib_mem_free ((void *) *p);
69 *p = 0;
70 }
71}
72
73static_always_inline void
74clib_spinlock_lock (clib_spinlock_t * p)
75{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +000076 while (clib_atomic_test_and_set (&(*p)->lock))
Florin Coras1f9600e2018-01-23 18:17:44 -080077 CLIB_PAUSE ();
78 CLIB_LOCK_DBG (p);
Damjan Marion1927da22017-03-27 17:08:20 +020079}
80
81static_always_inline void
82clib_spinlock_lock_if_init (clib_spinlock_t * p)
83{
84 if (PREDICT_FALSE (*p != 0))
85 clib_spinlock_lock (p);
86}
87
88static_always_inline void
89clib_spinlock_unlock (clib_spinlock_t * p)
90{
Florin Coras1f9600e2018-01-23 18:17:44 -080091 CLIB_LOCK_DBG_CLEAR (p);
Dave Barachfa77e8f2017-10-15 17:20:40 -040092 /* Make sure all writes are complete before releasing the lock */
93 CLIB_MEMORY_BARRIER ();
94 (*p)->lock = 0;
Damjan Marion1927da22017-03-27 17:08:20 +020095}
96
97static_always_inline void
98clib_spinlock_unlock_if_init (clib_spinlock_t * p)
99{
100 if (PREDICT_FALSE (*p != 0))
101 clib_spinlock_unlock (p);
102}
103
Florin Coras1f9600e2018-01-23 18:17:44 -0800104/*
105 * Readers-Writer Lock
106 */
107
108typedef struct clib_rw_lock_
109{
110 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
111 volatile u32 n_readers;
Florin Corasa332c462018-01-31 06:52:17 -0800112 volatile u32 n_readers_lock;
Florin Coras1f9600e2018-01-23 18:17:44 -0800113 volatile u32 writer_lock;
114#if CLIB_DEBUG > 0
115 pid_t pid;
116 uword thread_index;
117 void *frame_address;
118#endif
119} *clib_rwlock_t;
120
121always_inline void
122clib_rwlock_init (clib_rwlock_t * p)
123{
124 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400125 clib_memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
Florin Coras1f9600e2018-01-23 18:17:44 -0800126}
127
128always_inline void
129clib_rwlock_free (clib_rwlock_t * p)
130{
131 if (*p)
132 {
133 clib_mem_free ((void *) *p);
134 *p = 0;
135 }
136}
137
138always_inline void
139clib_rwlock_reader_lock (clib_rwlock_t * p)
140{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000141 while (clib_atomic_test_and_set (&(*p)->n_readers_lock))
Florin Corasa332c462018-01-31 06:52:17 -0800142 CLIB_PAUSE ();
143
144 (*p)->n_readers += 1;
145 if ((*p)->n_readers == 1)
Florin Coras1f9600e2018-01-23 18:17:44 -0800146 {
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000147 while (clib_atomic_test_and_set (&(*p)->writer_lock))
Florin Coras1f9600e2018-01-23 18:17:44 -0800148 CLIB_PAUSE ();
149 }
Florin Corasa332c462018-01-31 06:52:17 -0800150 CLIB_MEMORY_BARRIER ();
151 (*p)->n_readers_lock = 0;
152
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 {
168 CLIB_MEMORY_BARRIER ();
169 (*p)->writer_lock = 0;
170 }
Florin Corasa332c462018-01-31 06:52:17 -0800171
172 CLIB_MEMORY_BARRIER ();
173 (*p)->n_readers_lock = 0;
Florin Coras1f9600e2018-01-23 18:17:44 -0800174}
175
176always_inline void
177clib_rwlock_writer_lock (clib_rwlock_t * p)
178{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000179 while (clib_atomic_test_and_set (&(*p)->writer_lock))
Florin Coras1f9600e2018-01-23 18:17:44 -0800180 CLIB_PAUSE ();
181 CLIB_LOCK_DBG (p);
182}
183
184always_inline void
185clib_rwlock_writer_unlock (clib_rwlock_t * p)
186{
187 CLIB_LOCK_DBG_CLEAR (p);
188 CLIB_MEMORY_BARRIER ();
189 (*p)->writer_lock = 0;
190}
191
Damjan Marion1927da22017-03-27 17:08:20 +0200192#endif
193
194/*
195 * fd.io coding-style-patch-verification: ON
196 *
197 * Local Variables:
198 * eval: (c-set-style "gnu")
199 * End:
200 */