summaryrefslogtreecommitdiffstats
path: root/.gitreview
AgeCommit message (Expand)AuthorFilesLines
2018-04-04Hm this ended up on master, but should not have.v18.07-rc0Chris Luke1-1/+0
2018-04-04Setup for branch stable/1804Chris Luke1-0/+1
2016-01-13Fix gitreview to fd.ioEd Warnicke1-1/+1
2015-12-08Initial commit of vpp code.v1.0.0Ed Warnicke1-0/+4
>101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** @file
 * @brief Callback multiplex scheme
 */

#ifndef included_callback_data_h
#define included_callback_data_h
#include <vppinfra/clib.h>

/** @brief Declare and define a callback set type
 * @param set_t_ The set type to define
 * @param cb_t_ The callback type to use
 */
#define clib_callback_data_typedef(set_t_, cb_t_)   \
typedef struct set_t_                               \
{                                                   \
  cb_t_* curr;                                      \
  cb_t_* volatile next;                             \
  cb_t_* spare;                                     \
  clib_spinlock_t* lock;                            \
} set_t_

/** @brief Initialize a callback set
 * @param set_ The callback set to initialize
 * @param lock_ The lock to use, if any
 */
#define clib_callback_data_init(set_,lock_)  \
do {                                         \
  (set_)->lock = (lock_);                    \
  (set_)->curr = 0;                          \
  (set_)->next = 0;                          \
  (set_)->spare = 0;                         \
} while (0)

/** @brief Add a callback to the specified callback set
 * @param set_ The callback set
 * @param value_ The value_ to assign the callback
 *
 * Add a callback from the indicated callback set.  If the set is
 * currently being iterated, then the change will be applied after the
 * current full iteration, and prior to the next full iteration.
 */
#define clib_callback_data_add(set_,value_)          \
do {                                                 \
  clib_spinlock_lock_if_init ((set_)->lock);         \
  typeof ((set_)->next) next_ = (set_)->next;        \
  if (PREDICT_TRUE (next_ == 0))                     \
    {                                                \
      next_ = (set_)->spare;                         \
      (set_)->spare = 0;                             \
      vec_append (next_, (set_)->curr);              \
    }                                                \
  u32 sz_ = vec_len (next_);                         \
  vec_validate (next_, sz_);                         \
  next_[sz_] = (value_);                             \
  (set_)->next = next_;                              \
  clib_spinlock_unlock_if_init ((set_)->lock);       \
} while (0)

/** @brief Remove a callback from the specified callback set
 * @param set_ The callback set
 * @param fp_ The current callback function
 * @return 1 if the function was removed, 0 if not
 *
 * Remove a callback from the indicated callback set.  Idempotent.  If
 * the set is currently being iterated, then the change will be applied
 * after the current full iteration, and prior to the next full
 * iteration.
 */
#define clib_callback_data_remove(set_,fp_)          \
({                                                   \
  int found_ = 0;                                    \
  clib_spinlock_lock_if_init ((set_)->lock);         \
  typeof ((set_)->next) next_ = (set_)->next;        \
  if (PREDICT_TRUE (next_ == 0))                     \
    {                                                \
      next_ = (set_)->spare;                         \
      (set_)->spare = 0;                             \
      vec_append (next_, (set_)->curr);              \
    }                                                \
  u32 sz_ = vec_len (next_);                         \
  u32 i_;                                            \
  for (i_ = 0; i_ < sz_; i_++)                       \
    if (next_[i_].fp == (fp_))                       \
      {                                              \
        vec_delete (next_, 1, i_);                   \
        found_ = 1;                                  \
        break;                                       \
      }                                              \
  (set_)->next = next_;                              \
  clib_spinlock_unlock_if_init ((set_)->lock);       \
  found_;                                            \
})

/** @brief Swap a callback in the specified callback set
 * @param set_ The callback set
 * @param fp_ The current callback function
 * @param value_ The value_ to assign the callback
 * @return 1 if the function was swapped, 0 if not
 *
 * Swap a callback in the indicated callback set.  If the callback is
 * not found, then nothing is done.  If the set is currently being
 * iterated, then the change will be applied after the current full
 * iteration, and prior to the next full iteration.
 */
#define clib_callback_data_swap(set_,fp_,value_)     \
({                                                   \
  int found_ = 0;                                    \
  clib_spinlock_lock_if_init ((set_)->lock);         \
  typeof ((set_)->next) next_ = (set_)->next;        \
  if (PREDICT_TRUE (next_ == 0))                     \
    {                                                \
      next_ = (set_)->spare;                         \
      (set_)->spare = 0;                             \
      vec_append (next_, (set_)->curr);              \
    }                                                \
  u32 sz_ = vec_len (next_);                         \
  u32 i_;                                            \
  for (i_ = 0; i_ < sz_; i_++)                       \
    if (next_[i_].fp == (fp_))                       \
      {                                              \
        next_[i_] = (value_);                        \
        found_ = 1;                                  \
        break;                                       \
      }                                              \
  (set_)->next = next_;                              \
  clib_spinlock_unlock_if_init ((set_)->lock);       \
  found_;                                            \
})

/** @brief Ensure a callback is in the specified callback set
 * @param set_ The callback set
 * @param value_ The value_ to assign the callback
 * @return 1 if the function was swapped, 0 if not
 *
 * Add or swap a callback in the indicated callback set.  If the
 * callback is already in the set, it is replaced.  If the callback is
 * not found, then it is added.  If the set is currently being
 * iterated, then the change will be applied after the current full
 * iteration, and prior to the next full iteration.
 */
#define clib_callback_data_ensure(set_,value_)       \
do {                                                 \
  int found_ = 0;                                    \
  clib_spinlock_lock_if_init ((set_)->lock);         \
  typeof ((set_)->next) next_ = (set_)->next;        \
  if (PREDICT_TRUE (next_ == 0))                     \
    {                                                \
      next_ = (set_)->spare;                         \
      (set_)->spare = 0;                             \
      vec_append (next_, (set_)->curr);              \
    }                                                \
  u32 sz_ = vec_len (next_);                         \
  u32 i_;                                            \
  for (i_ = 0; i_ < sz_; i_++)                       \
    if (next_[i_].fp == (value_).fp)                 \
      {                                              \
        found_ = 1;                                  \
        break;                                       \
      }                                              \
  if (!found_)                                       \
    vec_validate (next_, i_);                        \
  next_[i_] = (value_);                              \
  (set_)->next = next_;                              \
  clib_spinlock_unlock_if_init ((set_)->lock);       \
} while(0)

/** @brief Enable/Disable the specified callback
 * @param set_ The callback set
 * @param fp_ The callback function
 * @param ena_ 1 to enable, 0 to disable
 *
 * Enable or disable a callback function, with no data.
 */
#define clib_callback_data_enable_disable(set_,fp_,ena_)   \
do {                                                       \
  if (ena_)                                                \
    {                                                      \
      typeof ((set_)->next[0]) data_ = { .fp = (fp_) };    \
      clib_callback_data_add ((set_), data_);              \
    }                                                      \
  else                                                     \
    clib_callback_data_remove ((set_), (fp_));             \
} while (0)

/** @brief Get the value of a callback, if set.
 * @param set_ The callback set
 * @param fp_ The callback function
 * @param v_ Set to the callback's current value
 * @return 1 if the function is in the set, 0 if not
 */
#define clib_callback_data_get_value(set_,fp_,v_)    \
({                                                   \
  int found_ = 0;                                    \
  clib_spinlock_lock_if_init ((set_)->lock);         \
  typeof ((set_)->next) search_ = (set_)->next;      \
  if (PREDICT_TRUE (search_ == 0))                   \
    search_ = (set_)->curr;                          \
  u32 sz_ = vec_len (search_);                       \
  u32 i_;                                            \
  for (i_ = 0; i_ < sz_; i_++)                       \
    if (search_[i_].fp == (fp_))                     \
      {                                              \
        (v_) = search_[i];                           \
        found_ = 1;                                  \
        break;                                       \
      }                                              \
  clib_spinlock_unlock_if_init ((set_)->lock);       \
  found_;                                            \
})

/** @brief Check if callback is set
 * @param set_ The callback set
 * @param fp_ The callback function
 * @return 1 if the function is in the set, 0 if not
 */
#define clib_callback_data_is_set(set_,fp_)          \
({                                                   \
  int found_ = 0;                                    \
  clib_spinlock_lock_if_init ((set_)->lock);         \
  typeof ((set_)->next) search_ = (set_)->next;      \
  if (PREDICT_TRUE (search_ == 0))                   \
    search_ = (set_)->curr;                          \
  u32 sz_ = vec_len (search_);                       \
  u32 i_;                                            \
  for (i_ = 0; i_ < sz_; i_++)                       \
    if (search_[i_].fp == (fp_))                     \
      {                                              \
        found_ = 1;                                  \
        break;                                       \
      }                                              \
  clib_spinlock_unlock_if_init ((set_)->lock);       \
  found_;                                            \
})

/** @brief Check for and get current callback set
 * @param set_ the callback set
 * @param varargs additional callback parameters
 */
#define clib_callback_data_check_and_get(set_)       \
({                                                   \
  typeof ((set_)->curr) curr_ = (set_)->curr;        \
  if (PREDICT_FALSE ((set_)->next != 0))             \
    {                                                \
      clib_spinlock_lock_if_init ((set_)->lock);     \
      vec_reset_length (curr_);                      \
      (set_)->spare = curr_;                         \
      curr_ = (set_)->next;                          \
      (set_)->next = 0;                              \
      if (PREDICT_FALSE (0 == vec_len (curr_)))      \
        vec_free (curr_);                            \
      (set_)->curr = curr_;                          \
      clib_spinlock_unlock_if_init ((set_)->lock);   \
    }                                                \
  curr_;                                             \
})

/** @brief Iterate and call a callback vector
 * @param vec_ the callback vector
 * @param varargs additional callback parameters
 */
#define clib_callback_data_call_vec(vec_, ...)                                \
  do                                                                          \
    {                                                                         \
      u32 sz_ = vec_len (vec_);                                               \
      u32 i_;                                                                 \
      for (i_ = 0; i_ < sz_; i_++)                                            \
	{                                                                     \
	  clib_prefetch_store (&vec_[i_ + 1]);                                \
	  (vec_[i_].fp) (&vec_[i_], __VA_ARGS__);                             \
	}                                                                     \
    }                                                                         \
  while (0)

/** @brief Call the specified callback set
 * @param set_ the callback set
 * @param varargs additional callback parameters
 */
#define clib_callback_data_call(set_, ...)                           \
do {                                                                 \
  typeof ((set_)->curr) v_ = clib_callback_data_check_and_get(set_); \
  clib_callback_data_iterate (v_, __VA_ARGS__);                      \
} while (0)

/** @brief prefetch the callback set
 * @param set_ The callback set
 */
#define clib_callback_data_prefetch(set_)                                     \
  do                                                                          \
    {                                                                         \
      if (PREDICT_FALSE ((set_)->curr))                                       \
	clib_prefetch_store ((set_)->curr);                                   \
    }                                                                         \
  while (0)

#endif /* included_callback_data_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */