genode/repos/os/include/os/synced_interface.h
Norman Feske eaac3cc1bd Revised API documentation
This patch curates the API documentation to become suitable for the
functional specificaton, which is partially generated from the header
files.
2015-04-09 16:04:46 +02:00

73 lines
1.5 KiB
C++

/*
* \brief Utility for synchronizing the access of interface methods
* \author Norman Feske
* \date 2013-05-16
*/
#ifndef _INCLUDE__OS__SYNCED_INTERFACE_H_
#define _INCLUDE__OS__SYNCED_INTERFACE_H_
/* Genode includes */
#include <base/lock.h>
namespace Genode {
template <typename, typename LOCK = Genode::Lock> class Synced_interface;
}
/*
* Utility for synchronizing the access of interface methods
*
* The 'Synced_interface' utility makes the serialization of interface
* method calls easy. The 'Synced_interface' is a functor that takes a lock
* and a pointer to an interface as arguments. When called, the functor
* returns a smart pointer to the interface. When this smart pointer gets
* dereferenced, the smart pointer takes care of acquiring and releasing
* the lock while the interface method is executed.
*/
template <typename IF, typename LOCK>
class Genode::Synced_interface
{
public:
class Guard
{
private:
LOCK &_lock;
IF *_interface;
Guard(LOCK &lock, IF *interface)
: _lock(lock), _interface(interface)
{
_lock.lock();
}
friend class Synced_interface;
public:
~Guard() { _lock.unlock(); }
IF *operator -> () { return _interface; }
};
private:
LOCK &_lock;
IF *_interface;
public:
Synced_interface(LOCK &lock, IF *interface)
: _lock(lock), _interface(interface) { }
Guard operator () ()
{
return Guard(_lock, _interface);
}
};
#endif /* _INCLUDE__OS__SYNCED_INTERFACE_H_ */