1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
/**
@page inside_extending Extending SimGrid
@tableofcontents
@section simgrid_dev_guide_generic_simcall The modern SimCall interface
We now have some generic simcalls which can be used to interface with the
Maestro without creating new simcalls. You might want to use them instead of
the defining additional simcalls. The long term goal is to replace most of
the simcalls with the generic ones.
For simcalls which never block, `kernelImmediate()` can be used. It takes a
C++ callback executes it in maestro. Any value returned by the callback is
returned by `kernelImmediate()`. Conversely, if the callback throws an
exception, this exception is propagated out of `kernelImmediate()`. Executing
the code in maestro enforces mutual exclusion (no other user process is running)
and enforce a deterministic order which guarantees the reproducibility of the
simulation. This call is particularly useful for implementing mutable calls:
~~~
void Host::setProperty(const char*key, const char *value){
simgrid::simix::kernelImmediate([&] {
simgrid::kernel::resource::HostImpl* host =
this->extension<simgrid::kernel::resource::HostImpl>();
host->setProperty(key,value);
});
}
~~~
If there is no blocking and no mutation involved (getters), you might consider
avoiding switching to Maestro and reading directly the data you're interested
in.
For simcalls which might block, `kernel_sync()` can be used. It takes a
C++ callback and executes it immediately in maestro. This C++ callback is
expected to return a `simgrid::kernel::Future<T>` reprensenting the operation
in the kernel. When the operations completes, the user process is waken up
with the result:
~~~
try {
std::vector<char> result = simgrid::simix::kernel_sync([&] {
// Fictional example, simgrid::kernel::readFile does not exist.
simgrid::kernel::Future<std::vector<char>> result = simgrid::kernel::readFile(file);
return result;
});
XBT_DEBUG("Finished reading file %s: length %zu", file, result.size());
}
// If the operation failed, kernel_sync() throws an exception:
catch (std::runtime_error& e) {
XBT_ERROR("Could not read file %s", file);
}
~~~
Asynchronous blocks can be implemented with `kernel_async()`. It works
like `kernel_sync()` but does not block. Instead, it returns a
`simgrid::simix::Future` representing the operation in the process:
~~~
simgrid::simix::Future<std:vector<char>> result = simgrid::simix::kernel_sync([&] {
// Fictional example, simgrid::kernel::readFile does not exist.
simgrid::kernek::Future<std::vector<char>> result = simgrid::kernel::readFile(file);
return result;
};
// Do some work while the operation is pending:
while (!result.is_ready() && hasWorkToDo())
doMoreWork();
// We don't have anything to do, wait for the operation to complete and
// get its value:
try {
std:vector<char> data = result.get();
XBT_DEBUG("Finished reading file %s: length %zu", file, data.size());
}
// If the operation failed, .get() throws an exception:
catch (std::runtime_error& e) {
XBT_ERROR("Could not read file %s", file);
}
~~~
<b>Note:</b> `kernel_sync(f)` could be implemented as `kernel_async(f).get()`.
*/
|