List of all members | Public Member Functions
casadi::RevWeakCache< K, T > Class Template Reference

#include <generic_shared.hpp>

Detailed Description

template<typename K, typename T>
class casadi::RevWeakCache< K, T >

Key is stored as a weakref Value is stored as regular ref

Definition at line 343 of file generic_shared.hpp.

Public Member Functions

void tocache (const K &key, const T &f, bool needs_lock=true, bool prune=true)
 
void tocache_if_missing (const K &key, T &f, bool prune=true)
 
bool incache (const K &key, T &f, bool needs_lock=true) const
 

Member Function Documentation

◆ incache()

template<typename K , typename T >
bool casadi::RevWeakCache< K, T >::incache ( const K &  key,
T &  f,
bool  needs_lock = true 
) const
inline

Definition at line 393 of file generic_shared.hpp.

393  {
394 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
395  // Safe access to cache_
396  casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
397 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
398  const void* k = key.get();
399  auto it = pre_cache_.find(k);
400  K temp;
401  if (it!=pre_cache_.end() && it->second.shared_if_alive(temp)) {
402  auto it2 = cache_.find(k);
403  f = it2->second;
404  return true;
405  } else {
406  return false;
407  }
408  }

◆ tocache()

template<typename K , typename T >
void casadi::RevWeakCache< K, T >::tocache ( const K &  key,
const T &  f,
bool  needs_lock = true,
bool  prune = true 
)
inline

Definition at line 345 of file generic_shared.hpp.

346  {
347 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
348  // Safe access to cache_
349  casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
350 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
351  // Add to cache
352  const void* k = key.get();
353  auto it = pre_cache_.find(k);
354 
355  if (it == pre_cache_.end()) {
356  pre_cache_.emplace(k, key);
357  cache_.emplace(k, f);
358  } else if (it->second.get() != typename K::base_type(key).weak()->get()) {
359  // The address was recycled. Compare weak-reference identities without
360  // reading liveness, and replace both entries in place.
361  it->second = key;
362  cache_.find(k)->second = f;
363  }
364 
365  // Remove a lost reference, if any, to prevent uncontrolled growth
366  if (!prune) return;
367  for (auto it = pre_cache_.begin(); it!=pre_cache_.end(); ++it) {
368  if (!it->second.alive()) {
369  const void* dead = it->first;
370  pre_cache_.erase(it);
371  cache_.erase(dead);
372  break; // just one dead reference is enough
373  }
374  }
375  }

◆ tocache_if_missing()

template<typename K , typename T >
void casadi::RevWeakCache< K, T >::tocache_if_missing ( const K &  key,
T &  f,
bool  prune = true 
)
inline

Definition at line 384 of file generic_shared.hpp.

384  {
385 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
386  // Safe access to cache_
387  std::lock_guard<std::mutex> lock(mtx_);
388 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
389  if (!incache(key, f, false)) {
390  tocache(key, f, false, prune);
391  }
392  }
void tocache(const K &key, const T &f, bool needs_lock=true, bool prune=true)
bool incache(const K &key, T &f, bool needs_lock=true) const

The documentation for this class was generated from the following file: