6_hooks

Store definition

// SPDX-FileCopyrightText: 2020-2023 Jochem Rutgers
//
// SPDX-License-Identifier: CC0-1.0

// This example assumes that you want to synchronize the store via other means
// than the Debugger interface. To do so, the application must track changes in
// data, and schedule these changes for synchronization.
//
// Basically, the application that wants to do synchronization must implement a
// memory (consistency) model. The stronger the model, the more guarantees the
// system gives the programmer about order of memory operations, but the
// trickier it is to implement.
//
// Sequential consistency, for example, is easy to
// understand: every memory operation appears to be executed in one single
// sequence, regardless which process in a distributed system executes the read
// or write. It is very expensive to implement such a model.
//
// The weakest possible model would give no guarantees; one would never know if
// a write to memory would be observable by others, and in which order.
// Designing hardware for such a model is easy...
//
// In my PhD thesis (Programming Models for Many-Core Architectures -- A
// Co-design Approach, 2014), I define Portable Memory Consistency, which
// strikes a balance between both. The hooks mentioned for PMC are available in
// libstored, implementation of them depends on the application. Following the
// thesis, any distributed memory model can be implemented on top of these
// hooks, including software cache coherency, and Distributed Shared Memory
// using only posted-writes.
//
// The store has two types of objects: variables and functions. Functions
// inherently have side effects. The hooks are not called for them, you should
// handle it by yourself if you need special synchronization. For variables,
// the hooks are in place.

int32 variable 1
int32=3 variable 2
(int32) some function

Application

// SPDX-FileCopyrightText: 2020-2023 Jochem Rutgers
//
// SPDX-License-Identifier: CC0-1.0

/*!
 * \file
 * \brief Example to show the get/set synchronization hooks.
 */

#include <stored>

#include "ExampleHooks.h"

#include <cstdio>

class SyncedExampleHooks : public STORE_T(SyncedExampleHooks, stored::ExampleHooksBase) {
	STORE_CLASS(SyncedExampleHooks, stored::ExampleHooksBase)
public:
	SyncedExampleHooks() {}

	void __some_function(bool set, int32_t& value)
	{
		if(!set)
			value = 42;
	}

	void __hookEntryX(stored::Type::type type, void* buffer, size_t len) noexcept
	{
		base::__hookEntryX(type, buffer, len);

		printf("entry_x(%u, %p, %zu) key=%" PRIxPTR "\n", (unsigned)type, buffer, len,
		       bufferToKey(buffer));
	}

	void __hookExitX(stored::Type::type type, void* buffer, size_t len, bool changed) noexcept
	{
		printf("exit_x(%u, %p, %zu, %schanged) key=%" PRIxPTR "\n", (unsigned)type, buffer,
		       len, changed ? "" : "un", bufferToKey(buffer));

		base::__hookExitX(type, buffer, len, changed);
	}

	void __hookEntryRO(stored::Type::type type, void* buffer, size_t len) noexcept
	{
		base::__hookEntryRO(type, buffer, len);

		printf("entry_ro(%u, %p, %zu) key=%" PRIxPTR "\n", (unsigned)type, buffer, len,
		       bufferToKey(buffer));
	}

	void __hookExitRO(stored::Type::type type, void* buffer, size_t len) noexcept
	{
		printf("exit_ro(%u, %p, %zu) key=%" PRIxPTR "\n", (unsigned)type, buffer, len,
		       bufferToKey(buffer));

		base::__hookExitRO(type, buffer, len);
	}

	void __hookChanged(stored::Type::type type, void* buffer, size_t len) noexcept
	{
		printf("changed(%u, %p, %zu) key=%" PRIxPTR "\n", (unsigned)type, buffer, len,
		       bufferToKey(buffer));

		base::__hookChanged(type, buffer, len);
	}
};

int main()
{
	SyncedExampleHooks store;

	printf("Function access (no hooks)\n");
	store.some_function.get();
	store.some_function.set(10);

	printf("\nRead-only access to typed object\n");
	store.variable_1.get();

	printf("\nRead-only access to variant object\n");
	int32_t v;
	store.find("/variable 2").get(&v, sizeof(v));

	printf("\nExclusive access to typed object\n");
	store.variable_1 = 11;

	printf("\nExclusive access to variant object\n");
	v = 3;
	store.find("/variable 2").set(&v, sizeof(v));
}

Store reference

template<typename Base_, typename Implementation_>
class ExampleHooksObjects

All ExampleHooksBase’s objects.

Subclassed by stored::ExampleHooksBase< SyncedExampleHooks >

Public Types

typedef Base_ Base
typedef Implementation_ Implementation

Public Members

impl::StoreFunction<Base, Implementation, ExampleHooksFunctionMap, 1u> some_function

some function

impl::StoreVariable<Base, Implementation, int32_t, 8u, 4> variable_1

variable 1

impl::StoreVariable<Base, Implementation, int32_t, 0u, 4> variable_2

variable 2

template<typename Implementation_>
class ExampleHooksBase : public stored::ExampleHooksObjects<ExampleHooksBase<Implementation_>, Implementation_>

Base class with default interface of all ExampleHooks implementations.

Although there are no virtual functions in the base class, subclasses can override them. The (lowest) subclass must pass the Implementation_ template paramater to its base, such that all calls from the base class can be directed to the proper overridden implementation.

The base class cannot be instantiated. If a default implementation is required, which does not have side effects to functions, instantiate stored::ExampleHooks. This class contains all data of all variables, so it can be large. So, be aware when instantiating it on the stack. Heap is fine. Static allocations is fine too, as the constructor and destructor are trivial.

To inherit the base class, you can use the following template:

class ExampleHooks : public stored::store<ExampleHooks, ExampleHooksBase>::type {
    STORE_CLASS(ExampleHooks, ExampleHooksBase)
public:
    // Your class implementation, such as:
    ExampleHooks() is_default
    // ...
};

Some compilers or tools may get confused by the inheritance using stored::store or stored::store_t. Alternatively, use STORE_T(...) instead, providing the template parameters of stored::store as macro arguments.

See also

stored::ExampleHooks

See also

stored::ExampleHooksData

Subclassed by stored::ExampleHooksDefaultFunctions< ExampleHooksBase< ExampleHooks > >

Public Types

enum [anonymous]

Values:

enumerator ObjectCount

Number of objects in the store.

enumerator VariableCount

Number of variables in the store.

enumerator FunctionCount

Number of functions in the store.

enumerator BufferSize

Buffer size.

typedef Implementation_ Implementation

Type of the actual implementation, which is the (lowest) subclass.

typedef uintptr_t Key

Type of a key.

See also

bufferToKey()

typedef Map<String::type, Variant<Implementation>>::type ObjectMap

Map as generated by map().

typedef ExampleHooksObjects<ExampleHooksBase, Implementation_> Objects
typedef ExampleHooksBase root

We are the root, as used by STORE_CLASS.

typedef ExampleHooksBase self

Define self for stored::store.

Public Functions

inline ~ExampleHooksBase()
void __some_function(bool set, int32_t &value)

Callback for some function.

inline Key bufferToKey(void const *buffer) const noexcept

Converts a variable’s buffer to a key.

A key is unique for all variables of the same store, but identical for the same variables across different instances of the same store class. Therefore, the key can be used to synchronize between instances of the same store. A key does not contain meta data, such as type or length. It is up to the synchronization library to make sure that these properties are handled well.

For synchronization, when hookEntryX() or hookEntryRO() is invoked, one can compute the key of the object that is accessed. The key can be used, for example, in a key-to-Variant map. When data arrives from another party, the key can be used to find the proper Variant in the map.

This way, data exchange is type-safe, as the Variant can check if the data format matches the expected type. However, one cannot process data if the key is not set yet in the map.

inline Type::type bufferToType(void const *buffer) noexcept

Return the type of the variable, given its buffer.

inline Variant<Implementation> find(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept

Finds an object with the given name.

Returns:

the object, or an invalid stored::Variant if not found.

template<typename T>
inline Function<T, Implementation> function(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept

Finds a function with the given name.

The function, when it exists, must have the given (fixed) type.

inline Implementation const &implementation() const noexcept

Returns the reference to the implementation.

inline Implementation &implementation() noexcept

Returns the reference to the implementation.

template<typename F>
inline void list(F &&f) noexcept

Calls a callback for every object in the longDirectory().

See also

stored::list()

template<typename F>
inline void list(F f, void *arg, char const *prefix, String::type *nameBuffer) noexcept

Calls a callback for every object in the longDirectory().

See also

stored::list()

template<typename F>
inline void list(F f, void *arg, char const *prefix = nullptr) noexcept

Calls a callback for every object in the longDirectory().

See also

stored::list()

inline uint8_t const *longDirectory() const noexcept

Retuns the long directory.

When not available, the short directory is returned.

inline ObjectMap map(char const *prefix = nullptr)

Create a name to Variant map for the store.

Generating the map may be expensive and the result is not cached.

inline char const *name() const noexcept

Returns the name of store, which can be used as prefix for stored::Debugger.

inline uint8_t const *shortDirectory() const noexcept

Returns the short directory.

template<typename T>
inline Variable<T, Implementation> variable(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept

Finds a variable with the given name.

The variable, when it exists, must have the given (fixed) type.

Public Members

impl::StoreFunction<Base, Implementation, ExampleHooksFunctionMap, 1u> some_function

some function

impl::StoreVariable<Base, Implementation, int32_t, 8u, 4> variable_1

variable 1

impl::StoreVariable<Base, Implementation, int32_t, 0u, 4> variable_2

variable 2

Public Static Functions

template<typename T>
static inline constexpr FreeFunction<T, Implementation> freeFunction(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept

Finds a function with the given name.

The function, when it exists, must have the given (fixed) type. It is returned as a free function; it is not bound yet to a specific store instance. This function is constexpr for C++14.

template<typename T>
static inline constexpr FreeVariable<T, Implementation> freeVariable(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept

Finds a variable with the given name.

The variable, when it exists, must have the given (fixed) type. It is returned as a free variable; it is not bound yet to a specific store instance. This function is constexpr for C++14.

static inline constexpr char const *hash() noexcept

Returns a unique hash of the store.

Friends

friend class impl::StoreFunction
friend class impl::StoreVariable
friend class impl::StoreVariantF
friend class impl::StoreVariantV
friend class stored::FreeVariable
friend class stored::Variant< void >
class SyncedExampleHooks : public stored::ExampleHooksBase<SyncedExampleHooks>

Public Functions

inline SyncedExampleHooks()
inline void __hookChanged(stored::Type::type type, void *buffer, size_t len) noexcept
inline void __hookEntryRO(stored::Type::type type, void *buffer, size_t len) noexcept
inline void __hookEntryX(stored::Type::type type, void *buffer, size_t len) noexcept
inline void __hookExitRO(stored::Type::type type, void *buffer, size_t len) noexcept
inline void __hookExitX(stored::Type::type type, void *buffer, size_t len, bool changed) noexcept
inline void __some_function(bool set, int32_t &value)