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:
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.
17 * @brief Callback multiplex scheme
18 * For a fully worked-out example, see .../src/vlib/main.[ch] and
19 * .../src/plugins/perfmon.c
22 #ifndef included_callback_h
23 #define included_callback_h
24 #include <vppinfra/clib.h>
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
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.
38 #define clib_callback_enable_disable(h,tmp,l,f,enable) \
41 clib_spinlock_lock_if_init(&l); \
42 vec_reset_length(tmp); \
45 vec_add1 (tmp,(void *)f); \
49 for (i = 0; i < vec_len (tmp); i++) \
50 if (((void *)tmp[i]) == (void *)f) \
52 vec_delete (tmp, 1, i); \
57 CLIB_MEMORY_STORE_BARRIER(); \
60 clib_spinlock_unlock_if_init(&l); \
63 /** @brief call the specified callback set
64 * @param h the callback set
65 * @param varargs additional callback parameters
67 #define clib_call_callbacks(h, ... ) \
70 * Note: fp exists to shut up gcc-6, which \
71 * produces a warning not seen with gcc-7 or 8 \
73 typeof (h) h_ = (h); \
75 for (i = 0; i < vec_len (h_); i++) \
77 (h_[i]) (__VA_ARGS__); \
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
87 #define clib_callback_is_set(h,l,f) \
91 clib_spinlock_lock_if_init(&l); \
92 for (_i = 0; _i < vec_len (h); _i++) \
93 if (((void *)h[_i]) == (void *) f) \
98 clib_spinlock_unlock_if_init(&l); \
102 #endif /* included_callback_h */
105 * fd.io coding-style-patch-verification: ON
108 * eval: (c-set-style "gnu")