blob: 7d241675b122d920eca5d37a9331d01790b1b0d1 [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
21typedef struct
22{
23 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
24 u32 lock;
25#if CLIB_DEBUG > 0
26 pid_t pid;
Damjan Marionf55f9b82017-05-10 21:06:28 +020027 uword thread_index;
Damjan Marion1927da22017-03-27 17:08:20 +020028 void *frame_address;
29#endif
30} *clib_spinlock_t;
31
32static inline void
33clib_spinlock_init (clib_spinlock_t * p)
34{
35 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
36 memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
37}
38
39static inline void
40clib_spinlock_free (clib_spinlock_t * p)
41{
42 if (*p)
43 {
44 clib_mem_free ((void *) *p);
45 *p = 0;
46 }
47}
48
49static_always_inline void
50clib_spinlock_lock (clib_spinlock_t * p)
51{
52 while (__sync_lock_test_and_set (&(*p)->lock, 1))
53#if __x86_64__
54 __builtin_ia32_pause ()
55#endif
56 ;
57#if CLIB_DEBUG > 0
58 (*p)->frame_address = __builtin_frame_address (0);
59 (*p)->pid = getpid ();
Damjan Marionf55f9b82017-05-10 21:06:28 +020060 (*p)->thread_index = os_get_thread_index ();
Damjan Marion1927da22017-03-27 17:08:20 +020061#endif
62}
63
64static_always_inline void
65clib_spinlock_lock_if_init (clib_spinlock_t * p)
66{
67 if (PREDICT_FALSE (*p != 0))
68 clib_spinlock_lock (p);
69}
70
71static_always_inline void
72clib_spinlock_unlock (clib_spinlock_t * p)
73{
Damjan Marion1927da22017-03-27 17:08:20 +020074#if CLIB_DEBUG > 0
75 (*p)->frame_address = 0;
76 (*p)->pid = 0;
Damjan Marionf55f9b82017-05-10 21:06:28 +020077 (*p)->thread_index = 0;
Damjan Marion1927da22017-03-27 17:08:20 +020078#endif
Dave Barachfa77e8f2017-10-15 17:20:40 -040079 /* Make sure all writes are complete before releasing the lock */
80 CLIB_MEMORY_BARRIER ();
81 (*p)->lock = 0;
Damjan Marion1927da22017-03-27 17:08:20 +020082}
83
84static_always_inline void
85clib_spinlock_unlock_if_init (clib_spinlock_t * p)
86{
87 if (PREDICT_FALSE (*p != 0))
88 clib_spinlock_unlock (p);
89}
90
91#endif
92
93/*
94 * fd.io coding-style-patch-verification: ON
95 *
96 * Local Variables:
97 * eval: (c-set-style "gnu")
98 * End:
99 */