Example #1
0
  VPtr lower_bound(const K& key) {
    VPtr val;
    list<VPtr> to_release;
    {
      Mutex::Locker l(lock);
      bool retry = false;
      do {
	retry = false;
	if (weak_refs.empty())
	  break;
	typename map<K, WeakVPtr>::iterator i = weak_refs.lower_bound(key);
	if (i == weak_refs.end())
	  --i;
	val = i->second.lock();
	if (val) {
	  lru_add(i->first, val, &to_release);
	} else {
	  retry = true;
	}
	if (retry)
	  cond.Wait(lock);
      } while (retry);
    }
    return val;
  }
Example #2
0
  VPtr lookup_or_create(const K &key) {
    VPtr val;
    list<VPtr> to_release;
    {
      Mutex::Locker l(lock);
      bool retry = false;
      do {
	retry = false;
	typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
	if (i != weak_refs.end()) {
	  val = i->second.first.lock();
	  if (val) {
	    lru_add(key, val, &to_release);
	    return val;
	  } else {
	    retry = true;
	  }
	}
	if (retry)
	  cond.Wait(lock);
      } while (retry);

      V *new_value = new V();
      VPtr new_val(new_value, Cleanup(this, key));
      weak_refs.insert(make_pair(key, make_pair(new_val, new_value)));
      lru_add(key, new_val, &to_release);
      return new_val;
    }
  }
Example #3
0
  VPtr lookup(const K& key) {
    VPtr val;
    list<VPtr> to_release;
    {
      Mutex::Locker l(lock);
      ++waiting;
      bool retry = false;
      do {
	retry = false;
	typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
	if (i != weak_refs.end()) {
	  val = i->second.first.lock();
	  if (val) {
	    lru_add(key, val, &to_release);
	  } else {
	    retry = true;
	  }
	}
	if (retry)
	  cond.Wait(lock);
      } while (retry);
      --waiting;
    }
    return val;
  }
Example #4
0
  VPtr lookup(const K &key) {
    Mutex::Locker l(lock);
    while (1) {
      if (contents.count(key)) {
	VPtr retval = contents[key].lock();
	if (retval)
	  return retval;
      } else {
	break;
      }
      cond.Wait(lock);
    Mutex::Locker l(lock);
    }
    return VPtr();
  }
Example #5
0
  VPtr lookup_or_create(const K &key, const A &arg) {
    Mutex::Locker l(lock);
    while (1) {
      if (contents.count(key)) {
	VPtr retval = contents[key].lock();
	if (retval)
	  return retval;
      } else {
	break;
      }
      cond.Wait(lock);
    }
    VPtr retval(new V(arg), OnRemoval(this, key));
    contents[key] = retval;
    return retval;
  }
Example #6
0
  VPtr lookup(const K &key) {
    Mutex::Locker l(lock);
    waiting++;
    while (1) {
      typename map<K, pair<WeakVPtr, V*> >::iterator i =
	contents.find(key);
      if (i != contents.end()) {
	VPtr retval = i->second.first.lock();
	if (retval) {
	  waiting--;
	  return retval;
	}
      } else {
	break;
      }
      cond.Wait(lock);
    }
    waiting--;
    return VPtr();
  }
Example #7
0
  VPtr lookup(K key) {
    VPtr val;
    list<VPtr> to_release;
    {
      Mutex::Locker l(lock);
      bool retry = false;
      do {
	retry = false;
	if (weak_refs.count(key)) {
	  val = weak_refs[key].lock();
	  if (val) {
	    lru_add(key, val, &to_release);
	  } else {
	    retry = true;
	  }
	}
	if (retry)
	  cond.Wait(lock);
      } while (retry);
    }
    return val;
  }
Example #8
0
  VPtr lookup_or_create(const K &key, const A &arg) {
    Mutex::Locker l(lock);
    waiting++;
    while (1) {
      typename map<K, pair<WeakVPtr, V*> >::iterator i =
	contents.find(key);
      if (i != contents.end()) {
	VPtr retval = i->second.first.lock();
	if (retval) {
	  waiting--;
	  return retval;
	}
      } else {
	break;
      }
      cond.Wait(lock);
    }
    V *ptr = new V(arg);
    VPtr retval(ptr, OnRemoval(this, key));
    contents.insert(make_pair(key, make_pair(retval, ptr)));
    waiting--;
    return retval;
  }
Example #9
0
 bool Wait(unsigned timeout_ms=5000) {
   const ScopeLock protect(mutex);
   return done || (cond.Wait(mutex, timeout_ms) && done);
 }
Example #10
0
 void WaitUntilNotRunning() {
   while (running)
     cond.Wait(mutex);
 }