blob: c60ff4146122d7bfa868e0ef8897967d7a6bdb30 [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;
27 uword cpu_index;
28 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 ();
60 (*p)->cpu_index = os_get_cpu_number ();
61#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{
74 (*p)->lock = 0;
75#if CLIB_DEBUG > 0
76 (*p)->frame_address = 0;
77 (*p)->pid = 0;
78 (*p)->cpu_index = 0;
79#endif
80}
81
82static_always_inline void
83clib_spinlock_unlock_if_init (clib_spinlock_t * p)
84{
85 if (PREDICT_FALSE (*p != 0))
86 clib_spinlock_unlock (p);
87}
88
89#endif
90
91/*
92 * fd.io coding-style-patch-verification: ON
93 *
94 * Local Variables:
95 * eval: (c-set-style "gnu")
96 * End:
97 */