diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-04-27 01:17:21 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-04-27 01:17:24 +0200 |
commit | dc61596aabe1d74718ba999f1002e0d7d88a944e (patch) | |
tree | 791c63a21524240a918d3f25fcee7e801461cad8 /src | |
parent | m whitespace (diff) | |
download | bus-dc61596aabe1d74718ba999f1002e0d7d88a944e.tar.gz bus-dc61596aabe1d74718ba999f1002e0d7d88a944e.tar.bz2 bus-dc61596aabe1d74718ba999f1002e0d7d88a944e.tar.xz |
add nowait option to bus_write
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/bus.c | 22 | ||||
-rw-r--r-- | src/bus.h | 13 | ||||
-rw-r--r-- | src/cmdline.c | 2 |
3 files changed, 29 insertions, 8 deletions
@@ -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++; @@ -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 |