FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
callback.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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 /** @file
17  * @brief Callback multiplex scheme
18  * For a fully worked-out example, see .../src/vlib/main.[ch] and
19  * .../src/plugins/perfmon.c
20  */
21 
22 #ifndef included_callback_h
23 #define included_callback_h
24 #include <vppinfra/clib.h>
25 
26 /** @brief Add or remove a callback to the specified callback set
27  * @param h head of the callback vector
28  * @param tmp vector to build result
29  * @param l clib_spinlock_t lock to protect the vector, may be 0
30  * @param f function to add or remove
31  * @param enable 1 adds f to the vector, 0 removes f from the vector
32  *
33  * Add or remove a callback from the indicated callback vector.
34  * Caller must provide locking to prevent > 1 concurrent writer
35  * Swaps the head of the callback vector and a tmp vector in one
36  * motion, after a write barrier to ensure that the write is atomic.
37  */
38 #define clib_callback_enable_disable(h,tmp,l,f,enable) \
39 do { \
40  void *tmp2; \
41  clib_spinlock_lock_if_init(&l); \
42  vec_reset_length(tmp); \
43  vec_append(tmp, h); \
44  if (enable) \
45  vec_add1 (tmp,(void *)f); \
46  else \
47  { \
48  int i; \
49  for (i = 0; i < vec_len (tmp); i++) \
50  if (((void *)tmp[i]) == (void *)f) \
51  { \
52  vec_delete (tmp, 1, i); \
53  break; \
54  } \
55  } \
56  tmp2 = h; \
57  CLIB_MEMORY_STORE_BARRIER(); \
58  h = tmp; \
59  tmp = tmp2; \
60  clib_spinlock_unlock_if_init(&l); \
61 } while(0);
62 
63 /** @brief call the specified callback set
64  * @param h the callback set
65  * @param varargs additional callback parameters
66  */
67 #define clib_call_callbacks(h, ... ) \
68 do { \
69  /* \
70  * Note: fp exists to shut up gcc-6, which \
71  * produces a warning not seen with gcc-7 or 8 \
72  */ \
73  typeof (h) h_ = (h); \
74  int i; \
75  for (i = 0; i < vec_len (h_); i++) \
76  { \
77  (h_[i]) (__VA_ARGS__); \
78  } \
79  } while (0);
80 
81 /** @brief predicate function says whether the specified function is enabled
82  * @param h the callback set
83  * @param l clib_spinlock_t lock to protect the vector, may be 0
84  * @param f the function to search for
85  * @return 1 if the function is enabled, 0 if not
86  */
87 #define clib_callback_is_set(h,l,f) \
88 ({ \
89  int _i; \
90  int _found = 0; \
91  clib_spinlock_lock_if_init(&l); \
92  for (_i = 0; _i < vec_len (h); _i++) \
93  if (((void *)h[_i]) == (void *) f) \
94  { \
95  _found=1; \
96  break; \
97  } \
98  clib_spinlock_unlock_if_init(&l); \
99  _found; \
100  })
101 
102 #endif /* included_callback_h */
103 
104 /*
105  * fd.io coding-style-patch-verification: ON
106  *
107  * Local Variables:
108  * eval: (c-set-style "gnu")
109  * End:
110  */
clib.h