X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Futils%2Fchannel.c;h=c72953e956911b9e69ffc6ab44889283c5d319b7;hp=af41b477b4748252763123b8a0303aa5edc06676;hb=3d184d708cdbf864eb96c15e1a0f08b57c118009;hpb=67f92c44a462350e6bacccb353d39ed28ea4bf5e diff --git a/src/utils/channel.c b/src/utils/channel.c index af41b47..c72953e 100644 --- a/src/utils/channel.c +++ b/src/utils/channel.c @@ -25,12 +25,16 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#if HAVE_CONFIG_H +# include "config.h" +#endif /* HAVE_CONFIG_H */ + #include "utils/channel.h" #include - #include +#include #include #include @@ -56,7 +60,9 @@ struct sdb_channel { size_t head; size_t tail; - _Bool full; + bool full; + + bool shutdown; }; /* @@ -79,7 +85,7 @@ channel_write(sdb_channel_t *chan, const void *data) { assert(chan); - if (chan->full) + if (chan->full || chan->shutdown) return -1; else if (! data) return 0; @@ -199,6 +205,12 @@ sdb_channel_select(sdb_channel_t *chan, int *wantread, void *read_data, break; } + if (chan->shutdown) { + if (read_status) + status = EBADF; + break; + } + if (timeout) { struct timespec abstime; @@ -210,6 +222,11 @@ sdb_channel_select(sdb_channel_t *chan, int *wantread, void *read_data, abstime.tv_sec += timeout->tv_sec; abstime.tv_nsec += timeout->tv_nsec; + if (abstime.tv_nsec > 1000000000) { + abstime.tv_nsec -= 1000000000; + abstime.tv_sec += 1; + } + status = pthread_cond_timedwait(&chan->cond, &chan->lock, &abstime); } @@ -253,5 +270,14 @@ sdb_channel_read(sdb_channel_t *chan, void *data) return status; } /* sdb_channel_read */ +int +sdb_channel_shutdown(sdb_channel_t *chan) +{ + if (! chan) + return -1; + chan->shutdown = 1; + return 0; +} /* sdb_channel_shutdown */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */