From dc61596aabe1d74718ba999f1002e0d7d88a944e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 27 Apr 2015 01:17:21 +0200 Subject: add nowait option to bus_write MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/bus.c | 22 +++++++++++++++++----- src/bus.h | 13 +++++++++++-- src/cmdline.c | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/bus.c b/src/bus.c index ff2abeb..0c121a6 100644 --- a/src/bus.c +++ b/src/bus.c @@ -126,6 +126,14 @@ (memcpy((bus)->message, msg, (strlen(msg) + 1) * sizeof(char))) +/** + * If `flags & (bus_flag)`, this macro evalutes to `sys_flag`, + * otherwise this macro evalutes to 0. + */ +#define F(bus_flag, sys_flag) \ + ((flags & (bus_flag)) ? sys_flag : 0) + + /** * Statement wrapper that goes to `fail` on failure @@ -607,13 +615,16 @@ fail: * @param bus Bus information * @param message The message to write, may not be longer than * `BUS_MEMORY_SIZE` including the NUL-termination + * @param flags `BUS_NOWAIT` if this function shall fail if + * another process is currently running this + * procedure * @return 0 on success, -1 on error */ -int /* TODO nonblocking */ -bus_write(const bus_t *bus, const char *message) +int +bus_write(const bus_t *bus, const char *message, int flags /* TODO document in man page */) { int state = 0, saved_errno; - if (acquire_semaphore(bus, X, SEM_UNDO) == -1) + if (acquire_semaphore(bus, X, SEM_UNDO | F(BUS_NOWAIT, IPC_NOWAIT)) == -1) return -1; t(zero_semaphore(bus, W, 0)); write_shared_memory(bus, message); @@ -708,10 +719,11 @@ bus_poll_stop(const bus_t *bus) } -const char * /* TODO nonblocking */ -bus_poll(bus_t *bus) +const char * +bus_poll(bus_t *bus, int flags) { int state = 0, saved_errno; + (void) flags; /* TODO nonblocking */ if (!bus->first_poll) { t(release_semaphore(bus, W, SEM_UNDO)); state++; t(acquire_semaphore(bus, S, SEM_UNDO)); state++; diff --git a/src/bus.h b/src/bus.h index 52d0e98..ed1c5de 100644 --- a/src/bus.h +++ b/src/bus.h @@ -57,6 +57,12 @@ */ #define BUS_INTR 4 +/** + * Function shall fail with errno set to `EAGAIN` + * if the it would block and this flag is used + */ +#define BUS_NOWAIT 1 + /** @@ -150,9 +156,12 @@ int bus_close(bus_t *bus); * @param bus Bus information * @param message The message to write, may not be longer than * `BUS_MEMORY_SIZE` including the NUL-termination + * @param flags `BUS_NOWAIT` if this function shall fail if + * another process is currently running this + * procedure * @return 0 on success, -1 on error */ -int bus_write(const bus_t *bus, const char *message); +int bus_write(const bus_t *bus, const char *message, int flags); /** * Listen (in a loop, forever) for new message on a bus @@ -181,7 +190,7 @@ int bus_read(const bus_t *bus, int (*callback)(const char *message, void *user_d int bus_poll_start(bus_t *bus); int bus_poll_stop(const bus_t *bus); -const char *bus_poll(bus_t *bus); +const char *bus_poll(bus_t *bus, int falgs); diff --git a/src/cmdline.c b/src/cmdline.c index d6df97a..03efdf3 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -148,7 +148,7 @@ main(int argc, char *argv[]) /* Broadcast a message on a bus. */ } else if ((argc == 4) && !strcmp(argv[1], "broadcast")) { t(bus_open(&bus, argv[2], BUS_WRONLY)); - t(bus_write(&bus, argv[3])); + t(bus_write(&bus, argv[3], 0)); t(bus_close(&bus)); } else -- cgit v1.2.3-70-g09d2