2011-12-22 16:19:25 +01:00
|
|
|
/*
|
2015-08-10 13:34:16 +02:00
|
|
|
* \brief Object pool - map capabilities to objects
|
2011-12-22 16:19:25 +01:00
|
|
|
* \author Norman Feske
|
2012-12-14 10:03:55 +01:00
|
|
|
* \author Alexander Boettcher
|
2015-08-10 13:34:16 +02:00
|
|
|
* \author Stafen Kalkowski
|
2011-12-22 16:19:25 +01:00
|
|
|
* \date 2006-06-26
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 21:44:47 +01:00
|
|
|
* Copyright (C) 2006-2013 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__BASE__OBJECT_POOL_H_
|
|
|
|
#define _INCLUDE__BASE__OBJECT_POOL_H_
|
|
|
|
|
|
|
|
#include <util/avl_tree.h>
|
|
|
|
#include <base/capability.h>
|
|
|
|
#include <base/lock.h>
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
namespace Genode { template <typename> class Object_pool; }
|
2012-12-14 10:03:55 +01:00
|
|
|
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/**
|
2015-08-10 13:34:16 +02:00
|
|
|
* Map capabilities to local objects
|
2015-03-04 21:12:14 +01:00
|
|
|
*
|
|
|
|
* \param OBJ_TYPE object type (must be inherited from Object_pool::Entry)
|
|
|
|
*
|
|
|
|
* The local names of a capabilities are used to differentiate multiple server
|
|
|
|
* objects managed by one and the same object pool.
|
|
|
|
*/
|
|
|
|
template <typename OBJ_TYPE>
|
|
|
|
class Genode::Object_pool
|
|
|
|
{
|
|
|
|
public:
|
2012-12-14 10:03:55 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
class Entry : public Avl_node<Entry>
|
|
|
|
{
|
|
|
|
private:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Untyped_capability _cap;
|
2015-08-10 13:34:16 +02:00
|
|
|
Lock _lock;
|
2012-12-14 10:03:55 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
inline unsigned long _obj_id() { return _cap.local_name(); }
|
2012-12-14 10:03:55 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
friend class Object_pool;
|
|
|
|
friend class Avl_tree<Entry>;
|
2012-12-14 10:03:55 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
public:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/**
|
|
|
|
* Constructors
|
|
|
|
*/
|
2015-08-10 13:34:16 +02:00
|
|
|
Entry() { }
|
|
|
|
Entry(Untyped_capability cap) : _cap(cap) { }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/**
|
|
|
|
* Avl_node interface
|
|
|
|
*/
|
|
|
|
bool higher(Entry *e) { return e->_obj_id() > _obj_id(); }
|
|
|
|
void recompute() { } /* for gcc-3.4 compatibility */
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/**
|
|
|
|
* Support for object pool
|
|
|
|
*/
|
|
|
|
Entry *find_by_obj_id(unsigned long obj_id)
|
|
|
|
{
|
|
|
|
if (obj_id == _obj_id()) return this;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Entry *obj = this->child(obj_id > _obj_id());
|
2012-12-14 10:03:55 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
return obj ? obj->find_by_obj_id(obj_id) : 0;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
/**
|
|
|
|
* Assign capability to object pool entry
|
|
|
|
*/
|
|
|
|
void cap(Untyped_capability c) { _cap = c; }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Untyped_capability const cap() const { return _cap; }
|
2015-08-10 13:34:16 +02:00
|
|
|
Lock& lock() { return _lock; }
|
2015-03-04 21:12:14 +01:00
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
private:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
Avl_tree<Entry> _tree;
|
|
|
|
Lock _lock;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
OBJ_TYPE* _obj_by_capid(unsigned long capid)
|
|
|
|
{
|
|
|
|
Entry *ret = _tree.first() ? _tree.first()->find_by_obj_id(capid)
|
|
|
|
: nullptr;
|
|
|
|
return static_cast<OBJ_TYPE*>(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FUNC, typename RET>
|
|
|
|
struct Apply_functor
|
|
|
|
{
|
|
|
|
RET operator()(OBJ_TYPE *obj, FUNC f)
|
|
|
|
{
|
|
|
|
using Functor = Trait::Functor<decltype(&FUNC::operator())>;
|
|
|
|
using Object_pointer = typename Functor::template Argument<0>::Type;
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto ret = f(dynamic_cast<Object_pointer>(obj));
|
|
|
|
if (obj) obj->_lock.unlock();
|
|
|
|
return ret;
|
|
|
|
} catch(...) {
|
|
|
|
if (obj) obj->_lock.unlock();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename FUNC>
|
|
|
|
struct Apply_functor<FUNC, void>
|
|
|
|
{
|
|
|
|
void operator()(OBJ_TYPE *obj, FUNC f)
|
|
|
|
{
|
|
|
|
using Functor = Trait::Functor<decltype(&FUNC::operator())>;
|
|
|
|
using Object_pointer = typename Functor::template Argument<0>::Type;
|
|
|
|
|
|
|
|
try {
|
|
|
|
f(dynamic_cast<Object_pointer>(obj));
|
|
|
|
if (obj) obj->_lock.unlock();
|
|
|
|
} catch(...) {
|
|
|
|
if (obj) obj->_lock.unlock();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
bool empty()
|
|
|
|
{
|
|
|
|
Lock::Guard lock_guard(_lock);
|
|
|
|
return _tree.first() == nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
public:
|
2012-12-14 10:03:55 +01:00
|
|
|
|
2015-03-04 21:12:14 +01:00
|
|
|
void insert(OBJ_TYPE *obj)
|
|
|
|
{
|
|
|
|
Lock::Guard lock_guard(_lock);
|
|
|
|
_tree.insert(obj);
|
|
|
|
}
|
2012-12-14 10:03:55 +01:00
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
void remove(OBJ_TYPE *obj)
|
2015-03-04 21:12:14 +01:00
|
|
|
{
|
2015-08-10 13:34:16 +02:00
|
|
|
Lock::Guard lock_guard(_lock);
|
|
|
|
_tree.remove(obj);
|
2015-03-04 21:12:14 +01:00
|
|
|
}
|
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
template <typename FUNC>
|
|
|
|
auto apply(unsigned long capid, FUNC func)
|
|
|
|
-> typename Trait::Functor<decltype(&FUNC::operator())>::Return_type
|
2015-03-04 21:12:14 +01:00
|
|
|
{
|
2015-08-10 13:34:16 +02:00
|
|
|
using Functor = Trait::Functor<decltype(&FUNC::operator())>;
|
|
|
|
|
|
|
|
OBJ_TYPE * obj;
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
|
|
|
Lock::Guard lock_guard(_lock);
|
2013-09-12 21:09:05 +02:00
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
obj = _obj_by_capid(capid);
|
2015-03-04 21:12:14 +01:00
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
if (obj) obj->_lock.lock();
|
2013-09-12 21:09:05 +02:00
|
|
|
}
|
2015-03-04 21:12:14 +01:00
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
Apply_functor<FUNC, typename Functor::Return_type> hf;
|
|
|
|
return hf(obj, func);
|
2015-03-04 21:12:14 +01:00
|
|
|
}
|
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
template <typename FUNC>
|
|
|
|
auto apply(Untyped_capability cap, FUNC func)
|
|
|
|
-> typename Trait::Functor<decltype(&FUNC::operator())>::Return_type
|
2015-03-04 21:12:14 +01:00
|
|
|
{
|
2015-08-10 13:34:16 +02:00
|
|
|
return apply(cap.local_name(), func);
|
2015-03-04 21:12:14 +01:00
|
|
|
}
|
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
template <typename FUNC>
|
|
|
|
void remove_all(FUNC func)
|
2015-03-04 21:12:14 +01:00
|
|
|
{
|
2015-08-10 13:34:16 +02:00
|
|
|
for (;;) {
|
|
|
|
OBJ_TYPE * obj;
|
2015-03-04 21:12:14 +01:00
|
|
|
|
2015-08-10 13:34:16 +02:00
|
|
|
{
|
|
|
|
Lock::Guard lock_guard(_lock);
|
|
|
|
|
|
|
|
obj = (OBJ_TYPE*) _tree.first();
|
|
|
|
|
|
|
|
if (!obj) return;
|
|
|
|
|
|
|
|
{
|
|
|
|
Lock::Guard object_guard(obj->_lock);
|
|
|
|
_tree.remove(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(obj);
|
|
|
|
}
|
2015-03-04 21:12:14 +01:00
|
|
|
}
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
#endif /* _INCLUDE__BASE__OBJECT_POOL_H_ */
|