FD.io VPP  v19.08-27-gf4dcae4
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  void (*fp)(void *a1, ...); \
74  int i; \
75  for (i = 0; i < vec_len (h); i++) \
76  { \
77  fp = (void *)(h[i]); \
78  (*fp) (__VA_ARGS__); \
79  } \
80  } while (0);
81 
82 /** @brief predicate function says whether the specified function is enabled
83  * @param h the callback set
84  * @param l clib_spinlock_t lock to protect the vector, may be 0
85  * @param f the function to search for
86  * @return 1 if the function is enabled, 0 if not
87  */
88 #define clib_callback_is_set(h,l,f) \
89 ({ \
90  int _i; \
91  int _found = 0; \
92  clib_spinlock_lock_if_init(&l); \
93  for (_i = 0; _i < vec_len (h); _i++) \
94  if (((void *)h[_i]) == (void *) f) \
95  { \
96  _found=1; \
97  break; \
98  } \
99  clib_spinlock_unlock_if_init(&l); \
100  _found; \
101  })
102 
103 #endif /* included_callback_h */
104 
105 /*
106  * fd.io coding-style-patch-verification: ON
107  *
108  * Local Variables:
109  * eval: (c-set-style "gnu")
110  * End:
111  */