Changeset 3681dd for src/CodePatterns


Ignore:
Timestamp:
Oct 30, 2015, 11:44:33 AM (10 years ago)
Author:
Frederik Heber <heber@…>
Children:
315a1e
Parents:
6e2f3b
git-author:
Frederik Heber <heber@…> (10/30/15 11:40:59)
git-committer:
Frederik Heber <heber@…> (10/30/15 11:44:33)
Message:

FIX: Equipped Cacheable with mutex locking read&write to owner.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodePatterns/Cacheable.hpp

    r6e2f3b r3681dd  
    1818#include <boost/function.hpp>
    1919#include <boost/shared_ptr.hpp>
     20#include <boost/thread/locks.hpp>
     21#include <boost/thread/recursive_mutex.hpp>
    2022
    2123#include "CodePatterns/Assert.hpp"
     
    166168    state_ptr validState;
    167169    // destroyed state is not predefined, because we rarely enter that state and never leave
     170
     171    //!> mutex to ensure access is only per-thread
     172    mutable boost::recursive_mutex ownerLock;
    168173
    169174    const Observable * const owner;
     
    195200    // we sign on with the best(=lowest) priority, so cached values are recalculated before
    196201    // anybody else might ask for updated values
     202    boost::lock_guard<boost::recursive_mutex> guard(ownerLock);
    197203    if (owner != NULL) {
    198204      if (channels.empty()) {
     
    215221  const T Cacheable<T>::operator*() const{
    216222    // we can only use the cacheable when the owner is not changing at the moment
     223    boost::lock_guard<boost::recursive_mutex> guard(ownerLock);
    217224    if ((owner == NULL) || (!owner->isBlocked())) {
    218225      return state->getValue();
     
    226233  Cacheable<T>::~Cacheable()
    227234  {
     235    boost::lock_guard<boost::recursive_mutex> guard(ownerLock);
    228236    if (owner != NULL) {
    229237      if (channels.empty()) {
     
    252260  template<typename T>
    253261  void Cacheable<T>::recieveNotification(Observable *publisher, Notification_ptr notification) {
     262    boost::lock_guard<boost::recursive_mutex> guard(ownerLock);
    254263    if (publisher == owner) {
    255264      ASSERT( !channels.empty(),
     
    266275    state_ptr destroyed = state_ptr(new DestroyedState(this));
    267276    switchState(destroyed);
     277    boost::lock_guard<boost::recursive_mutex> guard(ownerLock);
     278    const_cast<const Observable *&>(owner) = NULL;
    268279  }
    269280
Note: See TracChangeset for help on using the changeset viewer.