Namespace shared_memory¶
-
namespace shared_memory
All templated types in this namespaces are elementary types: int, double, float, char*, …
Typedefs
-
typedef boost::interprocess::named_condition SHMCondition
-
typedef boost::interprocess::scoped_lock<SHMMutex> SHMScopeLock
-
typedef boost::interprocess::interprocess_mutex UnamedSHMMutex
-
typedef boost::interprocess::interprocess_condition UnamedSHMCondition
-
typedef boost::interprocess::scoped_lock<UnamedSHMMutex> UnamedSHMLock
-
typedef boost::interprocess::named_mutex SHMMutex
-
typedef cereal::access private_serialization
-
typedef std::map<std::string, std::pair<void*, std::size_t>> ShmObjects
ShmObjects typedef is a simple renaming that ease the for loop writting.
-
typedef ShmTypeHelper<char>::ElemTypeAllocator ShmCharAllocator
Create a char allocator to ease the creation of strings.
-
typedef std::basic_string<char, std::char_traits<char>, ShmCharAllocator> ShmString
Create a basic_string type for the Shared Memory.
-
typedef std::map<std::string, std::unique_ptr<SharedMemorySegment>> SegmentMap
SegmentMap typedef is a simple short cut to the GLOBAL_SHM_SEGMENTS type.
Functions
-
void clear_array(std::string segment_id)
wipe the shared memory segment created by an instance of shared_memory::array, including mutexes, if any.
If there are no memory segment of this id, there will be no effect. If shared_memory::array instances are pointing to the wiped out segment, their get and set functions may hang indefinitely.
-
void set_segment_sizes(uint multiplier_1025)
sets the size of the segments that will be newly created via the set methods.
Until this function is called, segments are created with a size of 65536 bytes. This function is not interprocess : it will set the size of segments created in the current process
- Parameters:
multiplier_1025 – the size of create segment will be multiplier_1025 * 1025 bytes (because memory segment sizes have to be a multiple of 1025)
-
void set_default_segment_sizes()
set the size of segment newly created to the default size value of 65536
-
SharedMemorySegment &get_segment(const std::string &segment_id, const bool clear_upon_destruction = false, const bool create = true)
get_segment creates or give back a pointer to a SharedMemorySegment object.
- Parameters:
segment_id – is the name of the shared memory segment.
-
SegmentInfo get_segment_info(const std::string &segment_id)
performs introspection on the segment and return related information.
If the segment does not exists, creates it first.
-
bool segment_exists(const std::string &segment_id)
returns true if a segment exists under this id
- Parameters:
segment_id – is the name of the shared memory segment.
-
void delete_segment(const std::string &segment_id)
delete_segment deletes the segment of existing shared memory.
it makes sure that all element created in it is destroyed first. (is this needed? I do not know.)
- Parameters:
segment_id – is the name of the shared memory segment.
-
void delete_all_segments()
delete_all_segment delete all mapping to the shared memory used during the current process
-
void delete_all_segment()
alias for delete_all_segments (for retro compatibility)
-
template<typename ElemType>
bool delete_object(const std::string &segment_id, const std::string &object_id) delete_object deletes a particular object in the shared memory segment
- Parameters:
segment_id – [in] is the name of the shared memory segment.
- Returns:
true if everything went fine.
-
boost::interprocess::interprocess_mutex &get_segment_mutex(const std::string segment_id)
get_sgement_mutex aquiere a reference to the semgent global mutex.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
- Returns:
a reference to a boost mutex
-
void clear_shared_memory(const std::string &segment_id)
clear_shared_memory_segment destroys the shared memory
- Parameters:
segment_id – [in] is the name of the shared memory segment.
-
template<typename ElemType>
void set(const std::string &segment_id, const std::string &object_id, const ElemType &set_) set instanciates or get pointer to any elementary types in the shared memory.
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the string to be created in the shared memory
-
template<typename ElemType>
void set(const std::string &segment_id, const std::string &object_id, const ElemType *set_, const std::size_t size) set instanciates or get pointer to a fixed sized array of the templated type “T” in the shared memory.
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the pointer to the array of objects to set in the memory.
size – [in] is the array size.
-
void set(const std::string &segment_id, const std::string &object_id, const std::string &set_)
set instanciates or get pointer to a string in the shared memory.
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the string to be created in the shared memory
-
template<typename ElemType>
void set(const std::string &segment_id, const std::string &object_id, const std::vector<ElemType> &set_) set instanciates or get pointer to a std::vector<ElemType> in the shared memory.
This will translated as a fixed sized array in the shared memory
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the string to be created in the shared memory
-
template<typename ElemType>
void set(const std::string &segment_id, const std::string &object_id, const Eigen::Matrix<ElemType, Eigen::Dynamic, 1> &set_) set instanciates or get pointer to a Eigen::Matrix<ElemType, Eigen::Dynamic, 1> in the shared memory.
This will translated as a fixed sized array in the shared memory
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the string to be created in the shared memory
-
template<typename FirstType, typename SecondType>
void set(const std::string &segment_id, const std::string &object_id, const std::pair<FirstType, SecondType> &set_) set instanciates or get pointer to a std::pair<FirstType, SecondType> in the shared memory.
This is very usefull to dump maps in the shared memory
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the string to be created in the shared memory
-
template<typename KeyType, typename ValueType>
void set(const std::string &segment_id, const std::string &object_id, const std::map<KeyType, ValueType> &set_) set instanciates or get pointer to a std::vector<ElemType> or an Eigen::Matrix<ElemType, any, any> in the shared memory.
This will translated as a fixed sized array in the shared memory
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the string to be created in the shared memory
-
template<typename ElemType>
void get(const std::string &segment_id, const std::string &object_id, ElemType &get_, bool create = true) get gets a pointer to any elementary types in the shared memory.
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
get_ – [in] is the string to be created in the shared memory
-
template<typename ElemType>
void get(const std::string &segment_id, const std::string &object_id, ElemType *get_, const std::size_t expected_size, bool create = true) get gets a pointer to a fixed sized array of the templated type “T” in the shared memory.
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
get_ – [in] is the pointer to the array of objects to set in the memory.
size – [in] is the array size.
create – [in] : if false, raise a Non_existing_segment_exception if the segment does not already exist
-
void get(const std::string &segment_id, const std::string &object_id, std::string &get_, bool create = true)
get gets a pointer to a string in the shared memory.
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
get_ – [in] is the string to be created in the shared memory
create – [in] : if false, raise a Non_existing_segment_exception if the segment does not already exist
-
template<typename ElemType>
void get(const std::string &segment_id, const std::string &object_id, std::vector<ElemType> &get_, bool create = true) get gets a pointer to a std::vector<ElemType> in the shared memory.
This will translated as a fixed sized array in the shared memory
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the string to be created in the shared memory
create – [in] : if false, raise a Non_existing_segment_exception if the segment does not already exist
-
template<typename ElemType>
void get(const std::string &segment_id, const std::string &object_id, Eigen::Matrix<ElemType, Eigen::Dynamic, 1> &get_, bool create = true) get gets a pointer to a Eigen::Matrix<ElemType, Eigen::Dynamic, 1> in the shared memory.
This will translated as a fixed sized array in the shared memory
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
set_ – [in] is the string to be created in the shared memory
create – [in] : if false, raise a Non_existing_segment_exception if the segment does not already exist
-
template<typename FirstType, typename SecondType>
void get(const std::string &segment_id, const std::string &object_id, std::pair<FirstType, SecondType> &get_, bool create = true) get instanciates or get pointer to a std::pair<FirstType, SecondType> in the shared memory.
This is very usefull to dump maps in the shared memory
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
get_ – [in] is the string to be created in the shared memory
create – [in] : if false, raise a Non_existing_segment_exception if the segment does not already exist
-
template<typename KeyType, typename ValueType>
void get(const std::string &segment_id, const std::string &object_id, std::map<KeyType, ValueType> &get_, bool create = true) get gets a pointer to a std::vector<ElemType> or an Eigen::Matrix<ElemType, any, any> in the shared memory.
This will translated as a fixed sized array in the shared memory
All set functions make sure that the pointer is uniquely created to avoid useless computation time consumption.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
get_ – [in] is the string to be created in the shared memory
create – [in] : if false, raise a Non_existing_segment_exception if the segment does not already exist
-
template<class Serializable>
void serialize(const std::string &segment, const std::string &object, const Serializable &serializable) Serialize the instance into a string which is written in the shared memory.
This uses cereal for serialization, and Serializable must implement a serialize function, see: https://uscilab.github.io/cereal/
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
serializable – [in] is the instance to serialize
-
template<class Serializable>
void deserialize(const std::string &segment, const std::string &object, Serializable &serializable) Read from the memory a string that is deserialized into the passed instance of Serializable.
This assumes the serialization and writting in the shared memory has been performed using the serialize function.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
object_id – [in] is the name of the shared memory object to set.
serializable – [in] is the instance in which the string will be deserialized
-
void set_verbose(bool mode)
if verbose mode set to true (starting default is false), informaton about newly created objects will be displayed in the terminal.
Call to this function will change the verbose mode only for the current process.
-
bool wait_for_segment(const std::string &segment_id, int timeout_ms = -1)
wait until the segment is created either via call to a get or set function.
- Parameters:
segment_id – [in] is the name of the shared memory segment.
timeout_ms – [in] is a timeout in ms
- Returns:
true if the segment has been created before the timeout expired, false otherwise.
-
template<typename VectorType, typename ElemType>
void set(const std::string &segment_id, const std::string &object_id, const VectorType &set_)
Variables
-
bool VERBOSE
-
static SegmentMap GLOBAL_SHM_SEGMENTS
GLOBAL_SHARED_MEMORY_SEGMENT is global variable that acts as a a shared memory manager.
The use of the std::unique_ptr allows to delete the object and re-create at will.
-
class Allocation_exception : public exception
- #include <exceptions.hpp>
Public Functions
-
Allocation_exception(const std::string &segment_id, const std::string &object_id)
-
~Allocation_exception()
-
const char *what() const
Private Members
-
std::string error_message_
-
Allocation_exception(const std::string &segment_id, const std::string &object_id)
-
template<typename T, int SIZE = 0>
class array : public internal::array_members<T, 0> - #include <array.hpp>
Implement a shared array stored on a shared memory segment.
Items hosted by the array may be of (1) fundamental type (e.g. int, double, char), (2) array of fundamental type (e.g. int[10]); or (3) instances of a class implementing a serializable function (see shared_memory::serializer).
Public Functions
-
array(std::string segment_id, std::size_t size, bool clear_on_destruction = true, bool multiprocess_safe = true)
- Parameters:
segment_id – should be the same for all array pointing to the same shared memory segment
size – : number of elements to be stored by the array
clear_on_destruction – if true, the shared memory segment will be wiped on destruction of the array. Note that any other array pointing to this segment may hang indefinitely as a result. If no arrays pointing to the shared memory segment delete the segment, then users are expected to call shared_memory::clear_array. Failing to do so may result in new array pointing to a new memory segment of the same id to hang indefinitely at construction.
multiprocess_safe – if false, it is strongly adviced to protect accesses via a shared_memory::Mutex
-
~array()
wipe the related shared memory segment if clear_on_destruction is true (true by default)
-
array(const array<T, SIZE> &other)
this array and other array will point to the same memory segment, and will have same values for clear_on_destruction and multiprocess_safe
-
array(array<T, SIZE> &&other) noexcept
This array will point to the share memory segment pointed at by other; and will have same value for multprocess_safe and clear_on_destruction.
Warning: even if other.clear_on_destruction is true, the segment memory will not be wiped on the destruction of other. The duty of deleting the shared memory is passed to the new instance, so to speak
-
array<T, SIZE> &operator=(array<T, SIZE> &&other) noexcept
This array will point to the share memory segment pointed at by other; and will have same value for multprocess_safe and clear_on_destruction.
Warning: even if other.clear_on_destruction is true, the segment memory will not be wiped on the destruction of other. The duty of deleting the shared memory is passed to the new instance, so to speak
-
void set(uint index, const T &t)
set element t at index
-
void set(uint index, const T *t)
set element t at index
-
void get(uint index, T &t)
read element at index into t
-
void get(uint index, T *t)
read element at index into t
-
std::size_t size() const
max number of elements in the array
-
std::string get_serialized(uint index)
return the serialized string representation of the element if T is a serializable class.
Throws a logic error otherwise.
-
void print()
print in terminal info about array’s memory usage
-
void *get_raw()
Private Functions
-
void init(FUNDAMENTAL)
-
void set(uint index, const T &t, FUNDAMENTAL)
-
void get(uint index, T &t, FUNDAMENTAL)
-
std::string get_serialized(uint index, FUNDAMENTAL)
-
void init(FUNDAMENTAL_ARRAY)
-
void set(uint index, const T &t, FUNDAMENTAL_ARRAY)
-
void get(uint index, T &t, FUNDAMENTAL_ARRAY)
-
std::string get_serialized(uint index, FUNDAMENTAL_ARRAY)
-
void init(SERIALIZABLE)
-
void set(uint index, const T &t, SERIALIZABLE)
-
void get(uint index, T &t, SERIALIZABLE)
-
std::string get_serialized(uint index, SERIALIZABLE)
Private Members
-
boost::interprocess::managed_shared_memory segment_manager_
-
std::string segment_id_
-
std::size_t size_
-
bool clear_on_destruction_
-
bool multiprocess_safe_
-
shared_memory::Mutex mutex_
-
array(std::string segment_id, std::size_t size, bool clear_on_destruction = true, bool multiprocess_safe = true)
-
class ConditionVariable
- #include <condition_variable.hpp>
Public Functions
-
ConditionVariable(const std::string object_id, bool clean_memory_on_destruction)
A condition variable shared over the memory The condition variable is cleaned from the memory on destruction if clean_memory_on_destruction is set to true.
-
~ConditionVariable()
-
void notify_all()
notify_all is notifying all condition variables with the same mutex
-
void notify_one()
notify_one notifies one condition variable with the same mutex
-
bool timed_wait(Lock &lock, long wait_nano_seconds)
timed_wait wait a notify during a certain certain time and then wake up
- Parameters:
wait_duration – in microsecond
- Returns:
true: the condition variable has been notified, false otherwize
-
void wait(Lock &lock)
wait waits until another thread notifies this object
Public Static Functions
-
static void clean(const std::string object_id)
Private Members
-
std::string condition_id_
condition_id_ is the condition variable name in the shared memory
-
bool clean_memory_on_destruction_
if true (the default), clean the shared memory of the hosted mutex and condition.
-
SHMCondition *condition_variable_
condition_variable_ is the boost condition variable that is used
-
std::string mutex_id_
mutex_id_ is the mutex name in the shared memory
-
ConditionVariable(const std::string object_id, bool clean_memory_on_destruction)
-
template<class Serializable, int QUEUE_SIZE>
class Exchange_manager_consumer - #include <exchange_manager_consumer.hpp>
Public Functions
-
Exchange_manager_consumer(std::string segment_id, std::string object_id, bool leading, bool autolock = true)
An exchange_manager_consumer reads from the shared memory serialized items produced by an instance of exchange_manager_producer (which should use the same segment_id and object_id), possibly running in a separate process.
- Parameters:
segment_id – id of the shared memory segment
object_id – id of the shared memory object prefix
the – consumer is to be “permanent”, while different producers may provide data. Implies the deletion of the underlying share memory upon destruction.
mutex – locking mechanism internally managed by the producer. If false, lock has to be “manually” called. This allows for example to set several items in one shot
-
~Exchange_manager_consumer()
-
void lock()
lock the mutex required for writting in the shared memory without any collision with any producer.
Should be called before calls to “consume”. Not required if the constructor was called with autolock set to true
-
void unlock()
unlock the mutex for writting in the shared memory without any collision with any producer.
Not required if the constructor was called with autolock set to true
-
bool consume(Serializable &serializable)
read from the underlying shared memory a serialized object (set by a producer).
Should be called only if ready_to_consume returns true.
- Returns:
true if an item has been read
-
bool ready_to_consume()
returns true if a producer is also running.
‘consume’ should be called only if ready_to_consume returns true.
-
bool purge_feedbacks()
When this instance consumes an item, the item id is written in a shared queue for the producer to read (and acquire the feedback the item has been consumed).
This shared queue may get full (e.g the producer does not read it fast enough), in which case the item id is buffered in this instance. If this instance stops to consume, the buffered item ids will never be written in the shared queue, and the producer will not receive the corresponding feedback. This attempts to write the buffered ids into the queue, and returns true if the buffer is not empty after the call (i.e. some feedbacks have not been sent yet), false otherwise. Usage: to call before exit until true is returned
-
int nb_char_read()
returns the number of char that have been read from the exchange queue.
For debugging purposes
-
bool is_producer_queue_empty() const
-
bool is_consumer_queue_empty() const
Public Static Functions
-
static void clean_mutex(std::string segment_id)
-
static void clean_memory(std::string segment_id)
Private Types
-
typedef Exchange_manager_memory<Serializable, QUEUE_SIZE> Memory
-
typedef std::shared_ptr<Memory> Memory_ptr
Private Functions
-
void reset()
Private Members
-
Memory_ptr memory_
-
bool leading_
-
bool autolock_
-
std::string segment_id_
-
std::string object_id_
-
Exchange_manager_consumer(std::string segment_id, std::string object_id, bool leading, bool autolock = true)
-
template<class Serializable, int QUEUE_SIZE>
class Exchange_manager_producer - #include <exchange_manager_producer.hpp>
Public Functions
-
Exchange_manager_producer(std::string segment_id, std::string object_id, bool leading, bool autolock = true)
An exchange_manager_producer writes in the shared memory serialized items expected to be consumed by an instance of exchange_manager_consumer (which should use the same segment_id and object_id), possibly running in a separate process.
- Parameters:
segment_id – id of the shared memory segment
object_id – id of the shared memory object prefix
autolock – mutex locking mechanism internally managed by the producer. If false, lock has to be “manually” called. This allows for example to set several items in one shot
clean_memory_on_exit. – If true, the destructor will clean the underlined shared memory items.
-
~Exchange_manager_producer()
-
bool ready_to_produce()
returns true if a consumer is also running.
‘set’ should be called only if ready_to_produce returns true.
-
void lock()
lock the mutex required for writting in the shared memory without any collision with any consumer.
Should be called before calls to “set”. Not required if the constructor was called with autolock set to true
-
void unlock()
unlock the mutex for writting in the shared memory without any collision with any consumer.
Not required if the constructor was called with autolock set to true
-
bool set(const Serializable &serializable)
Set this serializable to be consumed.
Throws shared_memory::Memory_overflow_exception if the shared memory is full. Some of the shared memory should get free once items have been consumed by a consumer. This method should be called only if ‘ready_to_produce’ returns true; Returns true if all data could be written in the shared memory, false if some data required to be buffered (any following call to set, if any, will perform a new attempt to write remaining buffer to the shared memory)
-
void clear()
removed all elements from the shared queue
-
void get(std::deque<int> &get_consumed_ids)
write into get_consumed_ids the ids of serialized items that have been successfully consumed by a consumer
-
int nb_char_written()
returns the number of characters that have been serialized and written to the exchange queue.
For debug purposes.
-
void reset_char_count()
reset the count of characters written to the exchange queue to zero
-
bool producer_queue_empty() const
-
bool consumer_queue_empty() const
Public Static Functions
-
static void clean_mutex(std::string segment_id)
(unlock) and erase the mutex from the shared memory.
To be used if some executable using the exchange manager crashed without calls to destructors.
-
static void clean_memory(std::string segment_id)
wipe out the corresponding shared memory.
To be used if some executable using the exchange manager crashed without calls to destructors.
Private Types
-
typedef Exchange_manager_memory<Serializable, QUEUE_SIZE> Memory
-
typedef std::shared_ptr<Memory> Memory_ptr
Private Functions
-
void reset()
Private Members
-
Memory_ptr memory_
-
bool autolock_
-
bool leading_
-
std::string segment_id_
-
std::string object_id_
-
Exchange_manager_producer(std::string segment_id, std::string object_id, bool leading, bool autolock = true)
-
class Four_int_values
- #include <four_int_values.hpp>
Example of an instance that can be serialized.
Notice: There is a default constructor There is a serialize function It is friend to private_serialization
Public Functions
-
Four_int_values()
-
Four_int_values(int a, int b, int c, int d)
-
int get_id() const
-
void set_id(int id)
-
template<class Archive>
inline void serialize(Archive &archive)
-
bool equal(const Four_int_values &other) const
-
bool same(const Four_int_values &other) const
-
void print() const
Private Members
- friend private_serialization
-
std::vector<int> values_
-
int id_
Private Static Functions
-
static int next_id()
-
Four_int_values()
-
template<int SIZE = 10>
class Item - #include <item.hpp>
Public Functions
-
inline Item()
-
inline Item(int value)
-
inline void fill(int value)
-
inline void set(int index, int value)
-
inline int get() const
-
inline int get(int index) const
-
template<class Archive>
inline void serialize(Archive &archive)
-
inline void compact_print() const
-
inline void print() const
-
inline Item()
-
class Lock
- #include <lock.hpp>
A scope lock object for locking a shared memory mutex, to use for example with a shared memory condition variable.
The scope is unlocked on destruction.
Public Functions
-
Lock(Mutex &mutex)
lock the mutex
-
Lock(Mutex &mutex)
-
class LockedConditionVariable
- #include <locked_condition_variable.hpp>
The LockedConditionVariable class is here as a anonymous layer on top of the boost intersprocess condition variable labrary.
It creates a condition variable in a shared memory automatically.
Public Functions
-
LockedConditionVariable(const std::string object_id, bool clean_memory_on_destruction = true)
A condition variable shared over the memory The condition variable is cleaned from the memory on destruction if clean_memory_on_destruction is set to true.
Contrary to shared_memory::ConditionVariable, instances of this class manages their mutex and lock internally, with the consequence the mutex can be locked and unlocked exclusively through other instances of LockedConditionVariable.
-
~LockedConditionVariable()
-
void notify_all()
notify_all is notifying all condition variables with the same mutex
-
void notify_one()
notify_one notifies one condition variable with the same mutex
-
void wait()
wait waits until another thread notifies this object
-
bool timed_wait(long wait_nano_seconds)
timed_wait wait a notify during a certain certain time and then wake up
- Parameters:
wait_duration – in microsecond
- Returns:
true: the condition variable has been notified, false otherwize
-
bool try_lock()
try_lock Tries to acquire the lock without waiting.
- Throws :
- Returns:
true if manages to acquire the lock, false otherwise.
-
void unlock()
unlock Unlocks the lock.
- Throws :
-
bool owns()
-
void lock_scope()
lock_scope this function is used to lock the part of the code that needs protection.
It locks the mutex until unlock_scope is used
-
void unlock_scope()
unlock_scope this function unlock the mutex so remove the protection of the code
Public Members
-
SHMMutex mutex_
mutex_ is the mutex associated to the condition variable
-
SHMCondition *condition_variable_
condition_variable_ is the boost condition variable that is used
-
std::unique_ptr<SHMScopeLock> lock_
lock_ is a object that protects the codes with a mutex, see the boost documentation about “boost::interprocess::scoped_lock”
-
bool clean_memory_on_destruction_
if true (the default), clean the shared memory of the hosted mutex and condition.
Public Static Functions
-
static void clean(const std::string segment_id)
LockedConditionVariable clean their shared memory on destruction.
But the destructor may have failed to be called if for some reason the program crashed.
Private Members
-
std::string mutex_id_
mutex_id_ is the mutex name in the shared memory
-
std::string condition_id_
condition_id_ is the condition variable name in the shared memory
-
boost::interprocess::managed_shared_memory segment_manager_
shm_segment is the boost object that manages the shared memory segment
-
LockedConditionVariable(const std::string object_id, bool clean_memory_on_destruction = true)
-
class Memory_overflow_exception : public exception
- #include <exceptions.hpp>
Public Functions
-
Memory_overflow_exception(const std::string error_message)
-
~Memory_overflow_exception()
-
const char *what() const
Private Members
-
std::string error_message_
-
Memory_overflow_exception(const std::string error_message)
-
class Mutex
- #include <mutex.hpp>
Public Functions
-
Mutex(std::string mutex_id, bool clean_memory_on_destruction = true)
A Mutex accessible to several processes via the shared memory The mutex is cleaned from the shared memory on destruction if clean_memory_on_destruction is true (the default)
-
~Mutex()
-
void lock()
lock the mutex
-
void unlock()
unlock the mutex
Public Static Functions
-
static void clean(std::string mutex_id)
-
Mutex(std::string mutex_id, bool clean_memory_on_destruction = true)
-
class Non_existing_segment_exception : public exception
- #include <exceptions.hpp>
Public Functions
-
Non_existing_segment_exception(const std::string &segment_id)
-
~Non_existing_segment_exception()
-
const char *what() const
Private Members
-
std::string error_message_
-
Non_existing_segment_exception(const std::string &segment_id)
-
class Not_consumed_exception : public exception
- #include <exceptions.hpp>
Public Functions
-
Not_consumed_exception(int missed_id)
-
~Not_consumed_exception()
-
const char *what() const
Private Members
-
std::string error_message_
-
Not_consumed_exception(int missed_id)
-
class SegmentInfo
- #include <segment_info.hpp>
encapsulate information related to a shared memory segment
Public Functions
-
SegmentInfo(boost::interprocess::managed_shared_memory &msm)
introspection of the shared memory segment
-
uint get_size() const
total size of the segment
-
uint get_free_memory() const
free memory of the segment
-
uint get_used_memory() const
used memory of the segment
-
bool has_issues() const
report on the status of the internal structures of the segment
-
uint nb_objects() const
number of objects allocated in the segment
-
void print() const
print in the terminal informations about the segment
Private Members
-
uint size_
-
uint free_memory_
-
bool has_issues_
-
uint nb_objects_
-
SegmentInfo(boost::interprocess::managed_shared_memory &msm)
-
template<class Serializable>
class Serializable_exchange - #include <serializable_exchange.hpp>
Public Functions
-
Serializable_exchange(std::string segment_id, std::string object_id)
-
~Serializable_exchange()
-
void set(const Serializable &serializable)
-
void read(Serializable &serializable)
Private Members
-
std::string segment_id_
-
std::string object_id_
-
double *data_
-
Serializable_exchange(std::string segment_id, std::string object_id)
-
template<class Serializable>
class Serializer - #include <serializer.hpp>
Public Functions
-
const std::string &serialize(const Serializable &serializable)
serialize an (almost) arbitrary instance to a string.
The method uses cereal internally and the instance must implement a serialize function. See for details: https://uscilab.github.io/cereal/ Supplementary requirements:
Serializable must also have a default constructor.
All instances of Serializable must be of the same size. (e.g. vectors must be of fixed size) The generated and returned string is a private member of the Serializer instance. Successive calls to serialize overwrite this string.
- Parameters:
instance – to serialize to a string
-
void deserialize(const std::string &data, Serializable &serializable)
Restore the instance of serializable based on the string data, which should have been generated via the serialize function.
- Parameters:
the – serialized instance
instance – of Serializable to be restored
Public Static Functions
-
static int serializable_size()
Returns the serialized size (i.e.
the size of the string) of an instance of Serializable
Private Members
-
std::string data_
-
const std::string &serialize(const Serializable &serializable)
-
class SharedMemorySegment
- #include <shared_memory.hpp>
The SharedMemorySegment contains the pointers of the shared objects in on shared memrory segment.
We use unamed mutext (interprocess_mutex) and unamed condition variables (interprocess_condition) to be able to instanciate them with classic pointers
Public Functions
-
SharedMemorySegment(std::string segment_id, bool clear_upon_destruction, bool create)
SharedMemorySegment constructor.
-
inline ~SharedMemorySegment()
SharedMemorySegment destructor.
-
void clear_memory()
clear_memory free the shared memory
-
template<typename ElemType>
void get_object(const std::string &object_id, std::pair<ElemType*, std::size_t> &get_) get_object registers the object in the current struc and in the shared memory once only.
And returns the pointer to the object and its size. The size will be 1 for simple type and could greater to one for arrays.
- Parameters:
object_id – [in] the name of the object in the shared memory.
- Param :
-
void get_object(const std::string &object_id, std::string &get_)
get_object registers the object in the current struc and in the shared memory once only.
And returns the pointer to the object and its size. The size will be 1 for simple type and could greater to one for arrays.
- Parameters:
object_id – [in] the name of the object in the shared memory.
- Param :
-
template<typename ElemType>
void set_object(const std::string &object_id, const std::pair<const ElemType*, std::size_t> &set_) set_object registers the object in the current struc and in the shared memory once only.
And returns the pointer to the object and its size. The size will be 1 for simple type and could greater to one for arrays.
- Parameters:
object_id – [in] the name of the object in the shared memory.
set_ – [in] the reference to the fetched object.
-
template<typename ElemType>
bool register_object(const std::string &object_id, const std::pair<ElemType*, std::size_t> &obj_) register_object registers the object in the segment uniquely.
- Parameters:
object_id – is the name of the object to register.
obj_ – is the object to be registered.
- Returns:
true of a new object has been registered
-
template<typename ElemType>
bool register_object_read_only(const std::string &object_id) register_object_read_only registers the object in the segment uniquely.
- Parameters:
object_id – is the name of the object to register
obj_ – is the object to be registered
- Returns:
true of a new object has been registered
-
template<typename ElemType>
void delete_object(const std::string &object_id) delete_object delete and object from the shared memory.
- Parameters:
object_id – [in] the name of the object in the shared memory.
-
inline void create_mutex()
create_mutex small factory that allow to make sure that the mutex is created.
-
inline void destroy_mutex()
destroy_mutex small destructor of the mutext to make sure that it is unlock at critical time.
-
inline bool is_object_registered(const std::string &object_id)
is_object_registered used to check if the object has been registered or not.
- Parameters:
object_id – [in] the name of the object in the shared memory.
- Returns:
true if it has been registered
-
inline void set_clear_upon_destruction(const bool clear_upon_destruction)
set_clear_upon_destruction is a standard setter
- Parameters:
clear_upon_destruction – [in] is the value to set
-
inline const std::string &get_segment_id()
get_segment_id is a standard getter
- Returns:
the segment name
-
inline SegmentInfo get_info()
performs introspection on the segment and return related information
Public Members
-
boost::interprocess::interprocess_mutex *mutex_
mutex_ this mutex secure ALL the shared memory.
Private Members
-
boost::interprocess::managed_shared_memory segment_manager_
shm_segment is the boost object that manages the shared memory segment
-
ShmObjects objects_
objects_ are all the data stored in the segment.
WARNING here we use void* so the use of the set and get functions is the RESPONSABILITY of the user.
The user is to use the SAME type when calling set and get using the shared memory
-
std::string segment_id_
segment_id_ is the name of the segment inside the shared memory
-
bool clear_upon_destruction_
clear_upon_destruction_ flag decides if the segment should be cleared upon destruction.
Usage: typically only one process should set this flag to true.
-
int ravioli_
-
SharedMemorySegment(std::string segment_id, bool clear_upon_destruction, bool create)
-
template<typename ElemType>
struct ShmTypeHelper - #include <shared_memory.hpp>
ShmTypeHelper is a small struct that allow the definition of templated typedef.
Public Types
-
typedef boost::interprocess::allocator<ElemType, boost::interprocess::managed_shared_memory::segment_manager> ElemTypeAllocator
ShmemAllocator typedef allows to create std::allocator with the boost interprocess library.
-
typedef boost::container::deque<ElemType, ShmTypeHelper<ElemType>::ElemTypeAllocator> ShmDeque
-
typedef boost::container::vector<ElemType, ShmTypeHelper<ElemType>::ElemTypeAllocator> ShmVector
-
typedef boost::interprocess::allocator<ElemType, boost::interprocess::managed_shared_memory::segment_manager> ElemTypeAllocator
-
template<typename Key>
class Unexpected_map_key : public exception - #include <exceptions.hpp>
Public Functions
-
inline Unexpected_map_key(const std::string &segment_id, const std::string &object_id, Key &expected_key)
-
inline ~Unexpected_map_key()
-
inline const char *what() const
Private Members
-
std::string error_message_
-
inline Unexpected_map_key(const std::string &segment_id, const std::string &object_id, Key &expected_key)
-
class Unexpected_size_exception : public exception
- #include <exceptions.hpp>
Public Functions
-
Unexpected_size_exception(const std::string &segment_id, const std::string &object_id, int expected_size, int size_given)
-
~Unexpected_size_exception()
-
const char *what() const
Private Members
-
std::string error_message_
-
Unexpected_size_exception(const std::string &segment_id, const std::string &object_id, int expected_size, int size_given)
-
typedef boost::interprocess::named_condition SHMCondition