Commit fa0e4622 authored by Adam Wujek's avatar Adam Wujek 💬

userspace/libwr: add wrs_shm_get_and_check function

Open and check for data to be available.
This function is intended to replace wrs_shm_wait.
Signed-off-by: Adam Wujek's avatarAdam Wujek <adam.wujek@cern.ch>
parent e2f6609e
......@@ -46,6 +46,8 @@ int wrs_shm_put(void *headptr);
/* A reader may wait for the writer (polling on version field) */
void wrs_shm_wait(void *headptr, int msec_step, int retries, FILE *msg);
int wrs_shm_get_and_check(enum wrs_shm_name shm_name,
struct wrs_shm_head **head);
/* The writer can allocate structures that live in the area itself */
void *wrs_shm_alloc(void *headptr, size_t size);
......
......@@ -113,21 +113,32 @@ int wrs_shm_put(void *headptr)
return 0;
}
/* A reader may wait for the writer (polling on version field) */
void wrs_shm_wait(void *headptr, int msec_step, int retries, FILE *msg)
/* Open shmem and check if data is available
* return 0 when ok, otherwise error
* 1 when openning shmem failed
* 2 when version is 0 */
int wrs_shm_get_and_check(enum wrs_shm_name shm_name,
struct wrs_shm_head **head)
{
struct wrs_shm_head *head = headptr;
int i;
for (i = 0; i < retries && !head->version; i++) {
if (!i && msg)
fprintf(msg, "Waiting for my peer...");
if (msg)
fprintf(stderr, ".");
usleep(1000 * msec_step);
int ii;
int version;
/* try to open shmem */
if (!(*head) && !(*head = wrs_shm_get(shm_name, "",
WRS_SHM_READ | WRS_SHM_LOCKED))) {
return 1;
}
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;
}
if (i && msg)
fprintf(msg, "\n");
/* all ok */
return 0;
}
/* The writer can allocate structures that live in the area itself */
......
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