File threadsafe_object.hpp

This file declares templated container for data buffering.

Author

Manuel Wuthrich (manuel.wuthrich@gmail.com)

Author

Maximilien Naveau (maximilien.naveau@gmail.com)

Version

0.1

Date

2018-11-27

Copyright

Copyright (c) 2018

namespace real_time_tools
template<typename Type, size_t SIZE>
class SingletypeThreadsafeObject
#include <threadsafe_object.hpp>

The SingletypeThreadsafeObject is a thread safe object.

Template Parameters
  • Type: is the data type to store in the buffer.

  • SIZE: is the size of the buffer. It is better to know it at compile time to be 100% real time safe.

Public Functions

SingletypeThreadsafeObject()

Construct a new SingletypeThreadsafeObject object.

SingletypeThreadsafeObject(const std::vector<std::string> &names)

Construct a new SingletypeThreadsafeObject object.

Parameters
  • names:

void wait_for_update(const size_t &index) const

Wait until the data at the given index is modified.

Parameters
  • index:

void wait_for_update(const std::string &name) const

Wait until the data at the given name is modified.

Parameters
  • name:

size_t wait_for_update() const

Wait unitl any data has been changed and return its index.

Return

size_t

size_t size()

Getters.

get size.

Return

size_t

Type get(const size_t &index = 0) const

Get the data by its index in the buffer.

Return

Type

Parameters
  • index:

Type get(const std::string &name) const

Get the data by its name in the buffer.

Return

Type

Parameters
  • name:

template<int INDEX = 0>
Type get() const

Get the data by its index in the buffer.

Index is solved during compile time

Return

Type

Template Parameters
  • INDEX=0:

void set(const Type &datum, const size_t &index = 0)

Setters.

Set one element at a designated index.

Parameters
  • datum:

  • index:

template<int INDEX = 0>
void set(Type datum)

Set one element at a designated index.

Warning the index is resolved at compile time. This is used for backward comaptibility.

Template Parameters
  • INDEX=0:

Parameters
  • datum:

void set(const Type &datum, const std::string &name)

Set one element using at a designated name.

Internally this name is map to an index.

Parameters
  • datum:

  • name:

Private Members

std::shared_ptr<std::array<Type, SIZE>> data_

This is the data buffer.

std::shared_ptr<std::array<size_t, SIZE>> modification_counts_

This is counting the data modification occurences for each individual buffers.

std::shared_ptr<size_t> total_modification_count_

This is counting the all data modification occurences for all buffer.

/todo Can’t we just some the modification_counts_ array whenever needed?

std::map<std::string, size_t> name_to_index_

This is the map that allow to deal with data by their names.

std::shared_ptr<std::condition_variable> condition_

This condition variable is used to wait untils any data has been changed.

std::shared_ptr<std::mutex> condition_mutex_

This is the mutex of the condition varaible.

std::shared_ptr<std::array<std::mutex, SIZE>> data_mutexes_

These are the individual mutexes of each data upon setting and getting.

template<typename Type>
class ThreadsafeHistoryInterface
#include <threadsafe_object.hpp>

This is a template abstract interface class that define a data history.

This re-writting of the vector style class is thread safe. So it allows the user to use the object without having to deal with mutexes nor condition variables. This class is not used so far.

Template Parameters
  • Type: is the type of the data to store.

Private Functions

Type get_next(size_t id) const

Get the element after the one with the given id.

if there is no newer element, then wait until one arrives.

Return

Type the next element.

Parameters
  • id: is the index of the element in the buffer.

size_t get_next_id(size_t id) const = 0
Type get_newest() const

Get the newest value, this function waits if it is empty.

Return

Type the newest element.

size_t get_newest_id() const = 0

get_newest_id

Return

size_t

Type get(size_t id) const = 0

Get the value whith a specific id.

Return

Type

Parameters
  • id:

void add() = 0

I guess this is to add a value but with no argument?

template<typename ...Types>
class ThreadsafeObject
#include <threadsafe_object.hpp>

This object can have several types depending on what ones want to store.

Template Parameters
  • Types:

Public Types

using Type = typename std::tuple_element<INDEX, std::tuple<Types...>>::type

Define a specific “Type” which permit a more readable code.

Template Parameters
  • INDEX:

Public Functions

ThreadsafeObject()

Construct a new ThreadsafeObject object.

void wait_for_update(unsigned index) const

Wait until the data with the deignated index is changed.

Parameters
  • index:

template<unsigned INDEX = 0>
void wait_for_update() const

Wait until the data with the designated index is changed.

Template Parameters
  • INDEX=0:

size_t wait_for_update() const

Wait until any data has been changed.

Return

size_t

template<int INDEX = 0>
Type<INDEX> get() const

Getters.

Get the data with the designated index. The index is resolved at compile time.

Return

Type<INDEX>

Template Parameters
  • INDEX=0:

template<int INDEX = 0>
void set(Type<INDEX> datum)

Setters.

Set the data with the designated index. The index is resolved at compile time.

Template Parameters
  • INDEX=0:

Parameters
  • datum:

Public Static Attributes

const std::size_t SIZE = sizeof...(Types)

Define the size of the different types.

Private Members

std::shared_ptr<std::tuple<Types...>> data_

the actual data buffers.

std::shared_ptr<std::condition_variable> condition_

a condition variable that allow to wait until one data has been changed in the buffer.

std::shared_ptr<std::mutex> condition_mutex_

The mutex of the condition variable.

std::shared_ptr<std::array<size_t, SIZE>> modification_counts_

This is counting the data modification occurences for each individual buffers.

std::shared_ptr<size_t> total_modification_count_

This is counting the all data modification occurences for all buffer.

/todo Can’t we just some the modification_counts_ array whenever needed?

std::shared_ptr<std::array<std::mutex, SIZE>> data_mutexes_

These are the individual mutexes of each data upon setting and getting.