From: ufo2243 Date: Fri, 12 Sep 2014 03:51:59 +0000 (+0800) Subject: make bufferevent_getwatermark api more robust X-Git-Tag: release-2.1.5-beta~34^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a21e5108c2e689ae2ea8176d84f9978719e8ec36;p=libevent make bufferevent_getwatermark api more robust --- diff --git a/bufferevent.c b/bufferevent.c index 588461a3..2e6be144 100644 --- a/bufferevent.c +++ b/bufferevent.c @@ -615,25 +615,30 @@ bufferevent_setwatermark(struct bufferevent *bufev, short events, BEV_UNLOCK(bufev); } -void +int bufferevent_getwatermark(struct bufferevent *bufev, short events, size_t *lowmark, size_t *highmark) { - BEV_LOCK(bufev); if (events == EV_WRITE) { + BEV_LOCK(bufev); if (lowmark) *lowmark = bufev->wm_write.low; if (highmark) *highmark = bufev->wm_write.high; + BEV_UNLOCK(bufev); + return 0; } if (events == EV_READ) { + BEV_LOCK(bufev); if (lowmark) *lowmark = bufev->wm_read.low; if (highmark) *highmark = bufev->wm_read.high; + BEV_UNLOCK(bufev); + return 0; } - BEV_UNLOCK(bufev); + return -1; } int diff --git a/include/event2/bufferevent.h b/include/event2/bufferevent.h index 46203b04..a97030ef 100644 --- a/include/event2/bufferevent.h +++ b/include/event2/bufferevent.h @@ -535,8 +535,9 @@ void bufferevent_setwatermark(struct bufferevent *bufev, short events, size_t lowmark, size_t highmark); /** - Retrieves the watermarks for read or write events. Result is undefined if - events contains both EV_READ and EV_WRITE. + Retrieves the watermarks for read or write events. + Returns non-zero if events contains not only EV_READ or EV_WRITE. + Returns zero if events equal EV_READ or EV_WRITE @param bufev the bufferevent to be examined @param events EV_READ or EV_WRITE @@ -544,7 +545,7 @@ void bufferevent_setwatermark(struct bufferevent *bufev, short events, @param highmark receives the high watermark if not NULL */ EVENT2_EXPORT_SYMBOL -void bufferevent_getwatermark(struct bufferevent *bufev, short events, +int bufferevent_getwatermark(struct bufferevent *bufev, short events, size_t *lowmark, size_t *highmark); /** diff --git a/test/regress_bufferevent.c b/test/regress_bufferevent.c index 4ec2dccf..669efd8a 100644 --- a/test/regress_bufferevent.c +++ b/test/regress_bufferevent.c @@ -287,6 +287,9 @@ test_bufferevent_watermarks_impl(int use_pair) tt_int_op(low, ==, 100); tt_int_op(high, ==, 2000); + int r = bufferevent_getwatermark(bev1, EV_WRITE | EV_READ, &low, &high); + tt_int_op(r, !=, 0); + bufferevent_write(bev1, buffer, sizeof(buffer)); event_dispatch();