FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
lock.h
Go to the documentation of this file.
1 /*
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 
21 typedef struct
22 {
23  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
25 #if CLIB_DEBUG > 0
26  pid_t pid;
29 #endif
31 
32 static inline void
34 {
36  memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES);
37 }
38 
39 static inline void
41 {
42  if (*p)
43  {
44  clib_mem_free ((void *) *p);
45  *p = 0;
46  }
47 }
48 
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)->thread_index = os_get_thread_index ();
61 #endif
62 }
63 
66 {
67  if (PREDICT_FALSE (*p != 0))
69 }
70 
73 {
74 #if CLIB_DEBUG > 0
75  (*p)->frame_address = 0;
76  (*p)->pid = 0;
77  (*p)->thread_index = 0;
78 #endif
79  /* Make sure all writes are complete before releasing the lock */
81  (*p)->lock = 0;
82 }
83 
86 {
87  if (PREDICT_FALSE (*p != 0))
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  */
uword thread_index
Definition: lock.h:27
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:68
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:72
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:50
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:85
static void clib_spinlock_free(clib_spinlock_t *p)
Definition: lock.h:40
#define static_always_inline
Definition: clib.h:93
u32 lock
Definition: lock.h:24
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:33
#define PREDICT_FALSE(x)
Definition: clib.h:105
pid_t pid
Definition: lock.h:26
unsigned int u32
Definition: types.h:88
static void clib_mem_free(void *p)
Definition: mem.h:179
u64 uword
Definition: types.h:112
static_always_inline uword os_get_thread_index(void)
Definition: os.h:62
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:120
void * frame_address
Definition: lock.h:28
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:109
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:65