Merge pull request #168 from ufo2243/master

make bufferevent_getwatermark api more robust
This commit is contained in:
Nick Mathewson 2014-09-18 11:44:11 -04:00
commit c243dbf4c8
3 changed files with 15 additions and 6 deletions

View File

@ -615,25 +615,30 @@ bufferevent_setwatermark(struct bufferevent *bufev, short events,
BEV_UNLOCK(bufev); BEV_UNLOCK(bufev);
} }
void int
bufferevent_getwatermark(struct bufferevent *bufev, short events, bufferevent_getwatermark(struct bufferevent *bufev, short events,
size_t *lowmark, size_t *highmark) size_t *lowmark, size_t *highmark)
{ {
BEV_LOCK(bufev);
if (events == EV_WRITE) { if (events == EV_WRITE) {
BEV_LOCK(bufev);
if (lowmark) if (lowmark)
*lowmark = bufev->wm_write.low; *lowmark = bufev->wm_write.low;
if (highmark) if (highmark)
*highmark = bufev->wm_write.high; *highmark = bufev->wm_write.high;
BEV_UNLOCK(bufev);
return 0;
} }
if (events == EV_READ) { if (events == EV_READ) {
BEV_LOCK(bufev);
if (lowmark) if (lowmark)
*lowmark = bufev->wm_read.low; *lowmark = bufev->wm_read.low;
if (highmark) if (highmark)
*highmark = bufev->wm_read.high; *highmark = bufev->wm_read.high;
BEV_UNLOCK(bufev);
return 0;
} }
BEV_UNLOCK(bufev); return -1;
} }
int int

View File

@ -535,8 +535,9 @@ void bufferevent_setwatermark(struct bufferevent *bufev, short events,
size_t lowmark, size_t highmark); size_t lowmark, size_t highmark);
/** /**
Retrieves the watermarks for read or write events. Result is undefined if Retrieves the watermarks for read or write events.
events contains both EV_READ and EV_WRITE. 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 bufev the bufferevent to be examined
@param events EV_READ or EV_WRITE @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 @param highmark receives the high watermark if not NULL
*/ */
EVENT2_EXPORT_SYMBOL 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); size_t *lowmark, size_t *highmark);
/** /**

View File

@ -287,6 +287,9 @@ test_bufferevent_watermarks_impl(int use_pair)
tt_int_op(low, ==, 100); tt_int_op(low, ==, 100);
tt_int_op(high, ==, 2000); 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)); bufferevent_write(bev1, buffer, sizeof(buffer));
event_dispatch(); event_dispatch();