Utility for synchronizing interface calls
This commit is contained in:
parent
ba5906e425
commit
78204b8f49
66
os/include/os/synced_interface.h
Normal file
66
os/include/os/synced_interface.h
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
/*
|
||||||
|
* \brief Utility for synchronizing the access of interface functions
|
||||||
|
* \author Norman Feske
|
||||||
|
* \date 2013-05-16
|
||||||
|
*
|
||||||
|
* The 'Synced_interface' utility makes the serialization of interface
|
||||||
|
* function 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 function is executed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _INCLUDE__OS__SYNCED_INTERFACE_H_
|
||||||
|
#define _INCLUDE__OS__SYNCED_INTERFACE_H_
|
||||||
|
|
||||||
|
/* Genode includes */
|
||||||
|
#include <base/lock.h>
|
||||||
|
|
||||||
|
namespace Genode {
|
||||||
|
|
||||||
|
template <typename IF, typename LOCK = Genode::Lock>
|
||||||
|
class 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_ */
|
40
os/run/synced_interface.run
Normal file
40
os/run/synced_interface.run
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
build "core init test/synced_interface"
|
||||||
|
|
||||||
|
create_boot_directory
|
||||||
|
|
||||||
|
install_config {
|
||||||
|
<config>
|
||||||
|
<parent-provides>
|
||||||
|
<service name="ROM"/>
|
||||||
|
<service name="RAM"/>
|
||||||
|
<service name="CPU"/>
|
||||||
|
<service name="RM"/>
|
||||||
|
<service name="CAP"/>
|
||||||
|
<service name="PD"/>
|
||||||
|
<service name="SIGNAL"/>
|
||||||
|
<service name="LOG"/>
|
||||||
|
</parent-provides>
|
||||||
|
<default-route>
|
||||||
|
<any-service> <parent/> <any-child/> </any-service>
|
||||||
|
</default-route>
|
||||||
|
<start name="test-synced_interface">
|
||||||
|
<resource name="RAM" quantum="1M"/>
|
||||||
|
</start>
|
||||||
|
</config>
|
||||||
|
}
|
||||||
|
|
||||||
|
build_boot_image "core init test-synced_interface"
|
||||||
|
|
||||||
|
append qemu_args "-nographic -m 64"
|
||||||
|
|
||||||
|
run_genode_until {child exited with exit value 0} 10
|
||||||
|
|
||||||
|
grep_output {-> test-synced_interface}
|
||||||
|
|
||||||
|
compare_output_to {
|
||||||
|
[init -> test-synced_interface] [33mlock[0m
|
||||||
|
[init -> test-synced_interface] [33madding 13 + 14[0m
|
||||||
|
[init -> test-synced_interface] [33munlock[0m
|
||||||
|
[init -> test-synced_interface] [33mresult is 27[0m
|
||||||
|
}
|
||||||
|
|
49
os/src/test/synced_interface/main.cc
Normal file
49
os/src/test/synced_interface/main.cc
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* \brief Test for 'Synced_interface'
|
||||||
|
* \author Norman Feske
|
||||||
|
* \date 2013-05-16
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Genode Labs GmbH
|
||||||
|
*
|
||||||
|
* This file is part of the Genode OS framework, which is distributed
|
||||||
|
* under the terms of the GNU General Public License version 2.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Genode includes */
|
||||||
|
#include <os/synced_interface.h>
|
||||||
|
#include <base/printf.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct Adder
|
||||||
|
{
|
||||||
|
int add(int a, int b)
|
||||||
|
{
|
||||||
|
PLOG("adding %d + %d", a, b);
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Pseudo_lock
|
||||||
|
{
|
||||||
|
void lock() { PLOG("lock"); }
|
||||||
|
void unlock() { PLOG("unlock"); }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int main(int, char **)
|
||||||
|
{
|
||||||
|
using namespace Genode;
|
||||||
|
|
||||||
|
Pseudo_lock lock;
|
||||||
|
Adder adder;
|
||||||
|
|
||||||
|
Synced_interface<Adder, Pseudo_lock> synced_adder(lock, &adder);
|
||||||
|
|
||||||
|
int const res = synced_adder()->add(13, 14);
|
||||||
|
|
||||||
|
PLOG("result is %d", res);
|
||||||
|
return 0;
|
||||||
|
}
|
3
os/src/test/synced_interface/target.mk
Normal file
3
os/src/test/synced_interface/target.mk
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
TARGET = test-synced_interface
|
||||||
|
SRC_CC = main.cc
|
||||||
|
LIBS = base
|
Loading…
Reference in New Issue
Block a user