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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_clib_lock_h
17 #define included_clib_lock_h
19 #include <vppinfra/clib.h>
23 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
33 clib_spinlock_init (clib_spinlock_t * p)
35 *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
36 memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
40 clib_spinlock_free (clib_spinlock_t * p)
44 clib_mem_free ((void *) *p);
49 static_always_inline void
50 clib_spinlock_lock (clib_spinlock_t * p)
52 while (__sync_lock_test_and_set (&(*p)->lock, 1))
54 __builtin_ia32_pause ()
58 (*p)->frame_address = __builtin_frame_address (0);
59 (*p)->pid = getpid ();
60 (*p)->thread_index = os_get_thread_index ();
64 static_always_inline void
65 clib_spinlock_lock_if_init (clib_spinlock_t * p)
67 if (PREDICT_FALSE (*p != 0))
68 clib_spinlock_lock (p);
71 static_always_inline void
72 clib_spinlock_unlock (clib_spinlock_t * p)
75 (*p)->frame_address = 0;
77 (*p)->thread_index = 0;
79 /* Make sure all writes are complete before releasing the lock */
80 CLIB_MEMORY_BARRIER ();
84 static_always_inline void
85 clib_spinlock_unlock_if_init (clib_spinlock_t * p)
87 if (PREDICT_FALSE (*p != 0))
88 clib_spinlock_unlock (p);
94 * fd.io coding-style-patch-verification: ON
97 * eval: (c-set-style "gnu")