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 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src/bus.c') 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++; -- cgit v1.2.3-70-g09d2