1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/include/villas/shmem.h

127 lines
5.2 KiB
C
Raw Permalink Normal View History

/** Shared-memory interface: The interface functions that the external program should use.
*
* @file
* @author Georg Martin Reinke <georg.reinke@rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
2017-04-17 18:50:43 +02:00
/** Interface to the shared memory node for external programs.
*
* @addtogroup shmem Shared memory interface
* @{
*/
#pragma once
2018-08-06 11:22:52 +02:00
#include <villas/pool.h>
#include <villas/queue.h>
#include <villas/queue_signalled.h>
#include <villas/sample.h>
2019-04-22 23:43:46 +02:00
#define DEFAULT_SHMEM_QUEUELEN 512u
#define DEFAULT_SHMEM_SAMPLELEN 64u
/** Struct containing all parameters that need to be known when creating a new
* shared memory object. */
struct shmem_conf {
int polling; /**< Whether to use polling instead of POSIX CVs */
int queuelen; /**< Size of the queues (in elements) */
int samplelen; /**< Maximum number of data entries in a single sample */
};
/** The structure that actually resides in the shared memory. */
struct shmem_shared {
int polling; /**< Whether to use a pthread_cond_t to signal if new samples are written to incoming queue. */
struct queue_signalled queue; /**< Queue for samples passed in both directions. */
struct pool pool; /**< Pool for the samples in the queues. */
};
2017-06-14 13:32:44 +02:00
/** Relevant information for one direction of the interface. */
struct shmem_dir {
void *base; /**< Base address of the region. */
const char *name; /**< Name of the shmem object. */
size_t len; /**< Total size of the region. */
struct shmem_shared *shared; /**< Actually shared datastructure */
};
2017-06-08 13:01:23 +02:00
/** Main structure representing the shared memory interface. */
struct shmem_int {
struct shmem_dir read, write;
2019-04-23 13:05:31 +02:00
std::atomic<int> readers, writers, closed;
};
/** Open the shared memory objects and retrieve / initialize the shared data structures.
2017-06-14 13:32:44 +02:00
* Blocks until another process connects by opening the same objects.
*
* @param[in] wname Name of the POSIX shared memory object containing the output queue.
* @param[in] rname Name of the POSIX shared memory object containing the input queue.
* @param[inout] shm The shmem_int structure that should be used for following
* calls will be written to this pointer.
* @param[in] conf Configuration parameters for the output queue.
* @retval 0 The objects were opened and initialized successfully.
* @retval <0 An error occured; errno is set accordingly.
2017-04-16 23:00:30 +02:00
*/
int shmem_int_open(const char* wname, const char* rname, struct shmem_int* shm, struct shmem_conf* conf);
/** Close and destroy the shared memory interface and related structures.
*
* @param shm The shared memory interface.
2017-04-17 18:50:43 +02:00
* @retval 0 Closing successfull.
* @retval <0 An error occurred; errno is set appropiately.
2017-04-16 23:00:30 +02:00
*/
int shmem_int_close(struct shmem_int *shm);
/** Read samples from the interface.
*
* @param shm The shared memory interface.
2017-04-17 18:50:43 +02:00
* @param smps An array where the pointers to the samples will be written. The samples
* must be freed with sample_decref after use.
2017-04-17 18:50:43 +02:00
* @param cnt Number of samples to be read.
* @retval >=0 Number of samples that were read. Can be less than cnt (including 0) in case not enough samples were available.
* @retval -1 The other process closed the interface; no samples can be read anymore.
2017-04-16 23:00:30 +02:00
*/
int shmem_int_read(struct shmem_int *shm, struct sample *smps[], unsigned cnt);
/** Write samples to the interface.
*
* @param shm The shared memory interface.
* @param smps The samples to be written. Must be allocated from shm_int_alloc.
2017-04-17 18:50:43 +02:00
* @param cnt Number of samples to write.
* @retval >=0 Number of samples that were successfully written. Can be less than cnt (including 0) in case of a full queue.
* @retval -1 The write failed for some reason; no more samples can be written.
2017-04-16 23:00:30 +02:00
*/
int shmem_int_write(struct shmem_int *shm, struct sample *smps[], unsigned cnt);
/** Allocate samples to be written to the interface.
*
* The writing process must not free the samples; only the receiving process should free them using sample_decref after use.
* @param shm The shared memory interface.
* @param smps Array where pointers to newly allocated samples will be returned.
* @param cnt Number of samples to allocate.
* @return Number of samples that were successfully allocated (may be less then cnt).
*/
int shmem_int_alloc(struct shmem_int *shm, struct sample *smps[], unsigned cnt);
2017-04-16 23:00:30 +02:00
/** Returns the total size of the shared memory region with the given size of
* the input/output queues (in elements) and the given number of data elements
* per struct sample. */
2017-07-28 12:17:24 +02:00
size_t shmem_total_size(int queuelen, int samplelen);
2017-04-17 18:50:43 +02:00
/** @} */