Commit 96a78e15 authored by Adam Wujek's avatar Adam Wujek 💬

Merge branch 'adam-shmem'

Uplift shmem from wr-switch-sw repo.
Signed-off-by: Adam Wujek's avatarAdam Wujek <adam.wujek@cern.ch>
parents ab7106d2 d5e619c5
......@@ -32,7 +32,8 @@ struct wrs_shm_head {
int pid; /* The current pid owning the area */
unsigned pidsequence; /* Each new pid must increments this */
unsigned sequence; /* If we need consistency, this is it */
unsigned sequence; /* If we need consistency, this is it. LSB bit
* informs whether shmem is locked already */
unsigned version; /* Version of the data structure */
unsigned data_size; /* Size of it (for binary dumps) */
};
......@@ -42,6 +43,14 @@ struct wrs_shm_head {
#define WRS_SHM_WRITE 0x0001
#define WRS_SHM_LOCKED 0x0002 /* at init time: writers locks, readers wait */
#define WRS_SHM_LOCK_MASK 0x0001
/* return values of wrs_shm_get_and_check */
#define WRS_SHM_OPEN_OK 0x0001
#define WRS_SHM_OPEN_FAILED 0x0001
#define WRS_SHM_WRONG_VERSION 0x0002
#define WRS_SHM_INCONSISTENT_DATA 0x0003
/* Set custom path for shmem */
void wrs_shm_set_path(char *new_path);
......@@ -69,7 +78,11 @@ void *wrs_shm_follow(void *headptr, void *ptr);
/* Before and after writing a chunk of data, act on sequence and stamp */
#define WRS_SHM_WRITE_BEGIN 1
#define WRS_SHM_WRITE_END 0
extern void wrs_shm_write(void *headptr, int flags);
/* A helper to pass the name of caller function */
#define wrs_shm_write(headptr, flags) wrs_shm_write_caller(headptr, flags, \
__func__)
extern void wrs_shm_write_caller(void *headptr, int flags, const char *caller);
/* A reader can rely on the sequence number (in the <linux/seqlock.h> way) */
extern unsigned wrs_shm_seqbegin(void *headptr);
......
/*
* File to provide printout interfaces used in libwr of wrs-switch-sw
*/
#ifndef __WRS_MSG_H__
#define __WRS_MSG_H__
#include <ppsi/ppsi.h>
/* And shortands for people using autocompletion -- but not all levels */
#define pr_error(...) pp_error(__VA_ARGS__)
#define pr_err(...) pr_error(__VA_ARGS__)
#define pr_warning(...)
#define pr_warn(...)
#define pr_info(...)
#define pr_debug(...)
#endif /* __WRS_MSG_H__ */
......@@ -12,6 +12,7 @@
#include <libwr/shmem.h>
#include <libwr/util.h>
#include <libwr/wrs-msg.h>
#define SHM_LOCK_TIMEOUT_MS 50 /* in ms */
......@@ -140,25 +141,33 @@ int wrs_shm_put(void *headptr)
/* Open shmem and check if data is available
* return 0 when ok, otherwise error
* 1 when openning shmem failed
* 2 when version is 0 */
* 2 when version is 0
* 3 when data in shmem is inconsistent, function shall be called again
*/
int wrs_shm_get_and_check(enum wrs_shm_name shm_name,
struct wrs_shm_head **head)
{
int ii;
int version;
int ret;
/* try to open shmem */
if (!(*head) && !(*head = wrs_shm_get(shm_name, "",
WRS_SHM_READ | WRS_SHM_LOCKED))) {
return 1;
return WRS_SHM_OPEN_FAILED;
}
ii = wrs_shm_seqbegin(*head);
/* read head version */
version = (*head)->version;
if (wrs_shm_seqretry(*head, ii) || !version) {
/* data in shmem available and version not zero */
return 2;
ret = wrs_shm_seqretry(*head, ii);
if (ret) {
/* inconsistent data in shmem */
return WRS_SHM_INCONSISTENT_DATA;
}
if (!version) {
/* data in shmem available and version is zero */
return WRS_SHM_WRONG_VERSION;
}
/* all ok */
......@@ -197,15 +206,29 @@ void *wrs_shm_follow(void *headptr, void *ptr)
}
/* Before and after writing a chunk of data, act on sequence and stamp */
void wrs_shm_write(void *headptr, int flags)
void wrs_shm_write_caller(void *headptr, int flags, const char *caller)
{
struct wrs_shm_head *head = headptr;
head->sequence++;
pr_debug("caller: %s\n", caller);
if (flags == WRS_SHM_WRITE_END) {
/* At end-of-writing update the timestamp too */
head->stamp = get_monotonic_sec();
if (head->sequence & 1)
pr_error("On the shmem write end the sequence number "
"(%d) is even (should be odd). The caller of"
" wrs_shm_write is %s\n",
head->sequence, caller);
} else {
if (!(head->sequence & 1))
pr_error("On the shmem write begin the sequence number"
" (%d) is odd (should be even). The caller of"
" wrs_shm_write is %s\n",
head->sequence, caller);
}
head->sequence++;
return;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment