Commit 0add2783 authored by Alessandro Rubini's avatar Alessandro Rubini

userspace: librarize (and used twice) the previous commit

Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 63dd85b0
......@@ -44,6 +44,9 @@ struct wrs_shm_head {
void *wrs_shm_get(enum wrs_shm_name name_id, char *name, unsigned long flags);
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);
/* The writer can allocate structures that live in the area itself */
void *wrs_shm_alloc(void *headptr, size_t size);
......
......@@ -113,6 +113,23 @@ 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)
{
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);
}
if (i && msg)
fprintf(msg, "\n");
}
/* The writer can allocate structures that live in the area itself */
void *wrs_shm_alloc(void *headptr, size_t size)
{
......
......@@ -26,6 +26,8 @@ static void init_shm_hal(void)
snmp_log(LOG_ERR, "unable to open shm for HAL!\n");
exit(-1);
}
wrs_shm_wait(hal_head, 500 /* ms */, 20, NULL);
/* check hal's shm version */
if (hal_head->version != HAL_SHMEM_VERSION) {
snmp_log(LOG_ERR, "unknown hal's shm version %i "
......
......@@ -99,23 +99,13 @@ void ppsi_connect_minipc()
void init_shm(void)
{
struct hal_shmem_header *h;
int retr, maxretr = 15;
hal_head = wrs_shm_get(wrs_shm_hal, "", WRS_SHM_READ);
if (!hal_head) {
fprintf(stderr, "unable to open shm for HAL!\n");
exit(1);
}
for (retr = 0; retr < maxretr && !hal_head->version; retr++) {
if (!retr)
fprintf(stderr, "Waiting for HAL..");
fprintf(stderr, ".");
sleep(1);
}
if (retr)
fprintf(stderr, "\n");
/* check hal's shm version */
wrs_shm_wait(hal_head, 500 /* ms */, 20, stderr);
if (hal_head->version != HAL_SHMEM_VERSION) {
fprintf(stderr, "wr_mon: unknown HAL's shm version %i "
"(known is %i)\n",
......@@ -146,6 +136,7 @@ void init_shm(void)
fprintf(stderr, "unable to open shm for PPSI!\n");
exit(1);
}
wrs_shm_wait(ppsi_head, 500 /* ms */, 20, stderr);
/* check hal's shm version */
if (ppsi_head->version != WRS_PPSI_SHMEM_VERSION) {
......
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