From 86bddfa83a285149bb531780f51b3ed3cb833798 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 15 Jun 2017 15:07:19 +0200 Subject: [PATCH] shmem: whitespace cleanup I like a bit more whitespace for readability --- include/villas/nodes/shmem.h | 10 +++++----- include/villas/shmem.h | 27 +++++++++++++++------------ lib/nodes/shmem.c | 8 ++++++++ lib/shmem.c | 13 +++++++++++-- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/include/villas/nodes/shmem.h b/include/villas/nodes/shmem.h index fa697bbd8..c001d1d22 100644 --- a/include/villas/nodes/shmem.h +++ b/include/villas/nodes/shmem.h @@ -40,11 +40,11 @@ * @see node_type */ struct shmem { - const char* out_name; /**< Name of the shm object for the output queue. */ - const char* in_name; /**< Name of the shm object for the input queue. */ - struct shmem_conf conf; /**< Interface configuration struct. */ - char **exec; /**< External program to execute on start. */ - struct shmem_int intf; /**< Shmem interface */ + const char* out_name; /**< Name of the shm object for the output queue. */ + const char* in_name; /**< Name of the shm object for the input queue. */ + struct shmem_conf conf; /**< Interface configuration struct. */ + char **exec; /**< External program to execute on start. */ + struct shmem_int intf; /**< Shmem interface */ }; /** @see node_type::print */ diff --git a/include/villas/shmem.h b/include/villas/shmem.h index e69dbd5d6..0171402d1 100644 --- a/include/villas/shmem.h +++ b/include/villas/shmem.h @@ -52,26 +52,24 @@ union shmem_queue { /** 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 */ + 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. */ - - union shmem_queue queue; /**< Queues for samples passed in both directions. */ - - struct pool pool; /**< Pool for the samples in the queues. */ + int polling; /**< Whether to use a pthread_cond_t to signal if new samples are written to incoming queue. */ + union shmem_queue queue; /**< Queues for samples passed in both directions. */ + struct pool pool; /**< Pool for the samples in the queues. */ }; /** 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 */ + 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 */ }; /** Main structure representing the shared memory interface. */ @@ -81,6 +79,7 @@ struct shmem_int { /** Open the shared memory objects and retrieve / initialize the shared data structures. * 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 @@ -92,6 +91,7 @@ struct shmem_int { 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. * @retval 0 Closing successfull. * @retval <0 An error occurred; errno is set appropiately. @@ -99,6 +99,7 @@ int shmem_int_open(const char* wname, const char* rname, struct shmem_int* shm, int shmem_int_close(struct shmem_int *shm); /** Read samples from the interface. + * * @param shm The shared memory interface. * @param smps An array where the pointers to the samples will be written. The samples * must be freed with sample_put after use. @@ -109,6 +110,7 @@ int shmem_int_close(struct shmem_int *shm); 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. * @param cnt Number of samples to write. @@ -118,6 +120,7 @@ int shmem_int_read(struct shmem_int *shm, struct sample *smps[], unsigned cnt); 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_put after use. * @param shm The shared memory interface. diff --git a/lib/nodes/shmem.c b/lib/nodes/shmem.c index e19c32196..c7fffc112 100644 --- a/lib/nodes/shmem.c +++ b/lib/nodes/shmem.c @@ -42,12 +42,16 @@ int shmem_parse(struct node *n, config_setting_t *cfg) if (!config_setting_lookup_string(cfg, "out_name", &shm->out_name)) cerror(cfg, "Missing shared memory output queue name"); + if (!config_setting_lookup_string(cfg, "in_name", &shm->in_name)) cerror(cfg, "Missing shared memory input queue name"); + if (!config_setting_lookup_int(cfg, "queuelen", &shm->conf.queuelen)) shm->conf.queuelen = DEFAULT_SHMEM_QUEUELEN; + if (!config_setting_lookup_int(cfg, "samplelen", &shm->conf.samplelen)) shm->conf.samplelen = DEFAULT_SHMEM_SAMPLELEN; + if (!config_setting_lookup_bool(cfg, "polling", &shm->conf.polling)) shm->conf.polling = false; @@ -111,14 +115,18 @@ int shmem_read(struct node *n, struct sample *smps[], unsigned cnt) /* This can only really mean that the other process has exited, so close * the interface to make sure the shared memory object is unlinked */ shmem_int_close(&shm->intf); + if (recv <= 0) return recv; sample_copy_many(smps, shared_smps, recv); sample_put_many(shared_smps, recv); + struct timespec ts_recv = time_now(); + for (int i = 0; i < recv; i++) smps[i]->ts.received = ts_recv; + return recv; } diff --git a/lib/shmem.c b/lib/shmem.c index c22e54a5b..fe9b5c8ab 100644 --- a/lib/shmem.c +++ b/lib/shmem.c @@ -64,6 +64,7 @@ int shmem_int_open(const char *wname, const char* rname, struct shmem_int *shm, sem_own = sem_open(wname, O_CREAT, 0600, 0); if (sem_own == SEM_FAILED) return -1; + sem_other = sem_open(rname, O_CREAT, 0600, 0); if (sem_other == SEM_FAILED) return -1; @@ -72,12 +73,15 @@ int shmem_int_open(const char *wname, const char* rname, struct shmem_int *shm, fd = shm_open(wname, O_RDWR|O_CREAT|O_EXCL, 0600); if (fd < 0) return -1; + len = shmem_total_size(conf->queuelen, conf->queuelen, conf->samplelen); if (ftruncate(fd, len) < 0) return -1; + base = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (base == MAP_FAILED) return -1; + close(fd); manager = memtype_managed_init(base, len); @@ -117,10 +121,13 @@ int shmem_int_open(const char *wname, const char* rname, struct shmem_int *shm, fd = shm_open(rname, O_RDWR, 0); if (fd < 0) return -1; + if (fstat(fd, &stat_buf) < 0) return -1; + len = stat_buf.st_size; base = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (base == MAP_FAILED) return -1; @@ -133,6 +140,7 @@ int shmem_int_open(const char *wname, const char* rname, struct shmem_int *shm, /* Unlink the semaphores; we don't need them anymore */ sem_unlink(wname); + return 0; } @@ -146,17 +154,18 @@ int shmem_int_close(struct shmem_int *shm) munmap(shm->read.base, shm->read.len); munmap(shm->write.base, shm->write.len); shm_unlink(shm->write.name); + return 0; } int shmem_int_read(struct shmem_int *shm, struct sample *smps[], unsigned cnt) { return shm->read.shared->polling ? queue_pull_many(&shm->read.shared->queue.q, (void **) smps, cnt) - : queue_signalled_pull_many(&shm->read.shared->queue.qs, (void **) smps, cnt); + : queue_signalled_pull_many(&shm->read.shared->queue.qs, (void **) smps, cnt); } int shmem_int_write(struct shmem_int *shm, struct sample *smps[], unsigned cnt) { return shm->write.shared->polling ? queue_push_many(&shm->write.shared->queue.q, (void **) smps, cnt) - : queue_signalled_push_many(&shm->write.shared->queue.qs, (void **) smps, cnt); + : queue_signalled_push_many(&shm->write.shared->queue.qs, (void **) smps, cnt); }