diff --git a/userspace/libwr/include/libwr/shmem.h b/userspace/libwr/include/libwr/shmem.h index 0ccdc9822aef93f5988af02b0936bad9b75a7b32..82e0bf0dfd89dae8ff048b5b848052ed00b9c057 100644 --- a/userspace/libwr/include/libwr/shmem.h +++ b/userspace/libwr/include/libwr/shmem.h @@ -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); diff --git a/userspace/libwr/shmem.c b/userspace/libwr/shmem.c index 3dc9e7d78bbaf5613a3f5c7421e60864d602fca1..4ab7ec305dddb1b3cf27572851418d5d04ceead4 100644 --- a/userspace/libwr/shmem.c +++ b/userspace/libwr/shmem.c @@ -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) { diff --git a/userspace/snmpd/snmp_shmem.c b/userspace/snmpd/snmp_shmem.c index d33770cfa4fb8f32c33c5d77ef7701a15744b298..52adfa2b88b1ec9d65468fa6775cc1110c6ca736 100644 --- a/userspace/snmpd/snmp_shmem.c +++ b/userspace/snmpd/snmp_shmem.c @@ -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 " diff --git a/userspace/tools/wr_mon.c b/userspace/tools/wr_mon.c index 540ba006121a9b4df91c479847c43025f0418870..d6f754322b6d32eb5a943cae9c2fd57ca56ce187 100644 --- a/userspace/tools/wr_mon.c +++ b/userspace/tools/wr_mon.c @@ -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) {